Machine learning code related to logistic regression using python?

All QuestionsCategory: Artificial Intelligence, Machine Learning, & Deep LearningMachine learning code related to logistic regression using python?
Chetan Shidling Staff asked 5 years ago

I need code.

1 Answers
Chetan Shidling Staff answered 5 years ago

Code:

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]]#fetching 1st 2 columns
ys = x[:,[2]]#fetching last column
#print(ys)
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 sigmaoid(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))
#applying minimize function
result = opt.fmin_tnc(func=cost, x0=theta, fprime=Gradient, args=(xs, ys))
optimal_theta=result[0] print(‘optimal theta’, optimal_theta)
#calculation of training accuracy
def predict(opt_theta, xs):
m = xs.shape[0] p = np.zeros(m)
p1 = np.dot(xs, opt_theta.T)
#print(‘p1’, p1)
p = np.round(sigmoid(p1));
#print(‘p’,p)
return(p)
opt_theta = np.matrix(optimal_theta)
p = predict(opt_theta, xs)
print(‘Training Accuarcy:’, (np.mean((p == ys))) * 100);
#Predict whether student gets admitted into University
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.show()
plt.figure()
plotData(xs[:, 1:], ys)
plt.hold(True)
#Plotting of Desicion Boundary
#only neec 2 points to define a line, so choose 2
plot_x = (np.min(x[:, [0]])-2, np.max(x[:, [0]])+2)
t1 = np.numtiply(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()