import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import classification_report
from sklearn import preprocessing
scale = False
data = np.genfromtxt("ex2data1.txt",delimiter = ",")
x = data[:,0:2]
y = data[:,2]
def plot():
x0 = []
y0 = []
x1 = []
y1 = []
for i in range(len(x)):
if(y[i]==0):
x0.append(x[i,0])
y0.append(x[i,1])
else:
x1.append(x[i,0])
y1.append(x[i,1])
scatter0 = plt.scatter(x0,y0,c='b',marker = 'o')
scatter1 = plt.scatter(x1,y1,c='r',marker = 'x')
plt.legend(handles = [scatter0,scatter1],labels = ['label0','label1'],loc = 'best')
plot()
plt.show()
y = y.reshape(-1,1)
X = np.concatenate((np.ones((100,1)),x),axis =1)
theta = np.zeros(3).reshape(3,1)
def sigmoid(x):
return 1.0/(1+np.exp(-x))
def cost(xMat,yMat,theta):
left = np.multiply(yMat,np.log(sigmoid(xMat*theta)))
right = np.multiply(1-yMat,np.log(1-sigmoid(xMat*theta)))
return np.sum(left+right)/-(len(xMat))
def gradscent(xArr,yArr,theta):
if scale==True:
xArr = preprocessing.scale(xArr)
yMat = np.mat(yArr)
xMat = np.mat(xArr)
lr = 0.004
epochs = 200000
costlist = []
m,n = np.shape(xMat)
theta = np.mat(theta)
for i in range(epochs+1):
h = sigmoid(xMat*theta)
theta_grad = xMat.T*(h-yMat)/m
theta = theta-lr*theta_grad
costlist.append(cost(xMat,yMat,theta))
return theta,costlist
theta,costlist = gradscent(X,y,theta)
print(theta)
if scale == False:
plot()
x_test = [[30],[100]]
y_test = (-theta[0] - x_test*theta[1])/theta[2]
plt.plot(x_test,y_test,'k')
def predict(x, theta):
if scale == True:
x = preprocessing.scale(x)
xMat = np.mat(x)
theta = np.mat(theta)
return [1 if x >= 0.5 else 0 for x in sigmoid(xMat*theta)]
y_pre = np.array(predict(X, theta))
y_pre=y_pre.reshape(len(y_pre),1)
print(np.mean(y_pre==y))
|