import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import classification_report
path='data1.txt'
data=pd.read_csv(path,header=None,names=['exam 1 score', 'exam 2 score', 'admitted'])
data.head(5)
#正负样本分类
positive=data[data.admitted.isin([1])]
negative=data[data.admitted.isin([0])]
#数据可视化
fig,ax=plt.subplots(figsize=(10,8))#设置图形大小
ax.scatter(positive["exam 1 score"],positive["exam 2 score"],s=100,c="blue",marker="s",label="admitted") #设置点坐标、大小、颜色、图形、标签
ax.scatter(negative["exam 1 score"],negative["exam 2 score"],s=100,c="red",marker="x",label="Not admitted")
ax.legend(loc=2)#图例放在左上角
ax.set_xlabel("exam 1 score")
ax.set_ylabel("exam 2 score")
plt.show()
#sigmoid函数定义
def sigmoid(x):
return 1/ (1 + np.exp(-x))
#sigmoid函数验证
fig,ax=plt.subplots(figsize=(10,8))
a=np.arange(-5,5,step=0.01)
ax.plot(a,sigmoid(a))
plt.show()
#损失函数
def cost1(x,y,theta):
x=np.matrix(x)
y=np.matrix(y)
theta=np.matrix(theta)
first=np.multiply(-y,np.log(sigmoid(x.dot(theta.T))))
second=np.multiply(1-y,np.log(1-sigmoid((x.dot(theta.T)))))
return np.sum(first-second)/len(x)
#插入偏置项
data.insert(0, 'Ones', 1)
cols = data.shape[1]
x = data.iloc[:, 0:cols - 1]
y = data.iloc[:, cols - 1:cols]
#逻辑回归:theta参数初始化为0
theta = np.zeros(3)
x = np.matrix(x)
y = np.matrix(y)
theta = np.matrix(theta)
cost1(x,y,theta)
#梯度下降
def gradientdescent(X, y, theta, rate, iters):
temp = np.matrix(np.zeros(theta.shape))
cost = np.zeros(iters)
for i in range(iters):
A = sigmoid(np.dot(X, theta.T))
temp = theta - (rate / len(X)) * (A - y).T * X
theta = temp
cost[i] = cost1(X, y, theta)
return theta, cost
#参数训练
rate = 0.005
iters = 1000000
origin_cost = cost1(x, y, theta)
final_theta, cost = gradientdescent(x, y, theta, rate, iters)
print(cost)
print(final_theta)
#准确率
def predict(theta, X):
probability = sigmoid(np.dot(X, theta.T))
return [1 if x >= 0.5 else 0 for x in probability]
predictions = predict(final_theta, x)
print(classification_report(predictions, y))
#损失函数迭代可视化
fig, ax = plt.subplots(figsize=(8,5))
ax.plot(np.arange(iters), cost, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
plt.show()
#决策边界
x1 = np.arange(100, step=1)
x2 = -(final_theta[0, 0] + x1 * final_theta[0, 1]) / final_theta[0, 2]
fig, ax = plt.subplots(figsize=(8, 5))
ax.scatter(positive['exam 1 score'], positive['exam 2 score'], c='b', label='Admitted')
ax.scatter(negative['exam 1 score'], negative['exam 2 score'], c='r', marker='x', label='Not Admitted')
ax.plot(x1, x2)
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.legend(loc=2)
ax.set_title('Decision Boundary')
plt.show()