Part1
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv(“StudentSummerProgramData_Numeric_3_cont.csv”)
df.head()
x1 = df[‘TestScore’]
x2 = df[‘WritingScore’]
y = df[‘GPA’]
x1 =(x1 -x1.mean())/ x1.std()
x2 =(x2 -x2.mean())/ x2.std()
x = np.c_[x1,x2,np.ones(x1.shape[0])]
learning_rate = 0.01
epochs = 500
N = y.size
np.random.seed(123)
coeff = np.random.rand(3)
print(“The initial values of the coefficient ie the weights are”, coeff)
def gradient_descent(x,y,coeff,epochs,learning_rate):
past_costs =[]
past_coeff =[coeff]
for i in range(epochs):
prediction = np.dot(x, coeff)
error = prediction – y
cost = 1/(2*N) * np.dot(error.T,error)
past_costs.append(cost)
der = (1/N) * learning_rate * np.dot(x.T, error)
coeff = coeff – der
past_coeff.append(coeff)
return past_coeff, past_costs
past_coeff, past_costs = gradient_descent(x,y,coeff,epochs,learning_rate)
coeff = past_coeff[-1]
print(“Final value of coefficients”, coeff)
plt.title(“Loss Function”)
plt.xlabel(“Number of iterations”)
plt.ylabel(“Cost”)
plt.plot(past_costs)
plt.show()
Part2
y_binary=[]
for i in y:
if i >= 2.9:
i=1
y_binary.append(i)
else:
i=0
y_binary.append(i)
print(y_binary)
df[‘Outcome’] = y_binary
df.drop(labels=[‘GPA’], axis=1, inplace =True)
Looking at unique values
df[‘Outcome’].unique()
X = df.loc[:,[‘TestScore’, ‘WritingScore’]]
Y = df.loc[:,[‘Outcome’]]
from tensorflow import keras
from keras.layers import Dense
from keras.models import Sequential
model = Sequential()
model.add(Dense(1, input_dim =2, activation =”sigmoid”))
model.compile(loss =”binary_crossentropy” , optimizer =’rmsprop’, metrics = [‘accuracy’])
history = model.fit(X,Y, epochs = 1000 , verbose =1)
Plotting the loss function
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epochs’)
plt.plot(history.history[‘loss’])
def sigmoid(x):
return 1/(1+ np.exp(-x))
def gradient_descent1(x,y,coeff,epochs,learning_rate):
past_costs =[]
past_coeff =[coeff]
for i in range(epochs):
prediction = sigmoid(np.dot(x, coeff))
error = prediction – y
cost = 1/(2*N) * np.dot(error.T,error)
past_costs.append(cost)
der = (1/N) * learning_rate * np.dot(x.T, error)
coeff = coeff – der
past_coeff.append(coeff)
return past_coeff, past_costs
past_coeff, past_costs = gradient_descent1(x,y,coeff,epochs,learning_rate)
coeff = past_coeff[-1]
print(“Final value of coefficients”, coeff)
plt.title(“Loss Function”)
plt.xlabel(“Number of iterations”)
plt.ylabel(“Cost”)
plt.plot(past_costs)
plt.show()
OUTPUT : LINK
