Here is the code for logistic regression:
import numpy as np
import pandas as pd
import scipy.optimize as opt
import matplotlib.pyplot as plt
data = pd.read_csv(‘ex2data1.txt’,header = None,names=[‘Exam 1’, ‘Exam 2’, ‘Admitted’])
print(data.shape)
x=np.array(data[ : ])
xs=x[:,[0,1]]ys=x[:,[2]]positive = data[data[‘Admitted’].isin([1])]negative = data[data[‘Admitted’].isin([0])]fig, ax = plt.subplots(figsize=(6,4))
ax.scatter(positive[‘Exam 1’], positive[‘Exam 2′],s=50, c=’b’, marker=’o’,label=’Admitted’)
ax.scatter(negative[‘Exam 1’], negative[‘Exam 2′],s=50, c=’r’, marker=’x’,label=’Not Admitted’)
ax.legend()
ax.set_xlabel(‘Exam 1 Score’)
ax.set_ylabel(‘Exam 2 Score’)
plt.show()
theta = np.zeros(3)
m=len(ys)
xs=np.insert(xs,0,1,axis=1)
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def Gradient(theta,xs,ys):
theta = np.matrix(theta)
params=theta.shape[1]grad = np.zeros(params)
error = sigmoid(xs * theta.T) – ys
for i in range(params):
term = np.multiply(error,xs[:,[i]])
grad[i] = np.sum(term) /len(ys)
return(grad)
def cost(theta,xs,ys):
theta = np.matrix(theta)
first = np.multiply((0-(ys)),np.log(sigmoid((xs * theta.T))))
second = np.multiply((1-(ys)),np.log(1 – sigmoid((xs * theta.T))))
print(‘J’,np.sum(first – second) / (len(ys)))
return np.sum(first – second) /(len(ys))
result = opt.fmin_tnc(func=cost, x0=theta, fprime=Gradient, args=(xs, ys))
optimal_theta=result[0]print(‘optimal theta’,optimal_theta)
def predict(opt_theta,xs):
m=xs.shape[0]p=np.zeros(m)
p1=np.dot(xs,opt_theta.T)
p = np.round(sigmoid(p1));
return(p)
opt_theta=np.matrix(optimal_theta)
p=predict(opt_theta,xs)
print(‘Training Accuracy:’, (np.mean((p == ys))) * 100);
predicty=np.array([1,45,85])
te=np.dot(predicty ,optimal_theta)
prob = sigmoid(te);
print(‘probability of getting into university’,prob)
def plotData(X,y):
pos = X[np.where(y==1,True,False).flatten()]neg = X[np.where(y==0,True,False).flatten()]plt.plot(pos[:,0], pos[:,1], ‘+’, markersize=7,markeredgecolor=’black’,markeredgewidth=2)
plt.plot(neg[:,0], neg[:,1], ‘o’, markersize=7,markeredgecolor=’black’,markerfacecolor=’yellow’)
plt.figure()
plotData(xs[:,1:], ys)
#plt.hold(True)
plot_x=(np.min(x[:,[0]])-2,np.max(x[:,[0]])+2)
t1=np.multiply(optimal_theta[1],plot_x)
t2=t1+optimal_theta[0]t3=(-1.0/optimal_theta[2])
plot_y = np.multiply(t3,t2)
plt.plot(plot_x, plot_y,color=’r’,label=’Decision Boundary’)
plt.show()
Code for logistic regression?
1 Answers