线性可分
#线性可分
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def sigmoid(z):
return 1/(1+np.exp(-z))
def costFunction(X,Y,theta):
inner = sigmoid(X@theta)
first = Y * np.log(inner)
second = (1-Y) * np.log(1-inner)
return -np.sum(first+second)/len(X)
def gradientDescent(X,Y,theta,alpha,iters):
costs = []
for i in range(iters):
theta = theta - (alpha/len(X)) * X.T @ (sigmoid(X@theta) - Y)
cost = costFunction(X,Y,theta)
costs.append(cost)
return costs,theta
def get_XY(data):
data.insert(0,'ones',1)
x=data.iloc[:,0:-1]
y=data.iloc[:,-1]
X=np.array(x)
Y=np.array(y)
Y=Y.reshape(len(Y),1)
return X,Y
data = pd.read_csv("D:\桌面\Coursera-ML-AndrewNg-Notes-master\code\ex2-logistic regression\ex2data1.txt",names=["exam1","exam2","Accepted"])
#print(data.head())
X,Y=get_XY(data)
#print(X.shape)
#print(Y.shape)
theta = np.zeros((3,1))
cost_init = costFunction(X,Y,theta)
#print(cost_init)
alpha = 0.004
iters = 200000
costs,theta_final = gradientDescent(X,Y,theta,alpha,iters)
#print(theta_final)
c1 = -theta_final[0][0]/theta_final[2][0]#绘制决策边界直线
c2 = -theta_final[1][0]/theta_final[2][0]
#数据可视化
fig,ax=plt.subplots()
x=np.linspace(20,100,2)
f = c1+c2 * x
ax.plot(x,f,c='g')
ax.scatter(data[data['Accepted']==0]['exam1'],data[data['Accepted']==0]['exam2'],c='r',marker='x',label='y=0')
ax.scatter(data[data['Accepted']==1]['exam1'],data[data['Accepted']==1]['exam2'],c='b',marker='o',label='y=1')
ax.legend()
ax.set(xlabel='exam1',ylabel='exam2')
plt.show()
线性不可分
#线性不可分
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
def feature_mapping(x1,x2,power):
data = {}#字典 保存特征多项式
for i in np.arange(power+1):
for j in np.arange(i+1):
data['F{}{}'.format(i-j,j)] = np.power(x1,i-j) * np.power(x2,j)
return pd.DataFrame(data)
def sigmoid(z):
return 1/(1+np.exp(-z))
def costFunction(X,Y,theta,lamda):
inner = sigmoid(X @ theta)
first = Y * np.log(inner)
second = (1-Y) * np.log(1-inner)
reg = np.sum(np.power(theta[1:],2)) * (lamda / (2 * len(X)))
return -np.sum(first + second)/len(X) + reg
def gradientDescent(X,Y,theta,alpha,iters,lamda):
costs = []
for i in range(iters):
reg = alpha * theta[1:] * (lamda/len(X))
reg = np.insert(reg,0,values=0,axis=0)
theta = theta - (X.T @ (sigmoid(X @ theta) - Y))* alpha/len(X) - reg
cost = costFunction(X,Y,theta,lamda)
costs.append(cost)
if i%10000 == 0:
print(cost)
return theta,costs
data = pd.read_csv("D:\桌面\Coursera-ML-AndrewNg-Notes-master\code\ex2-logistic regression\ex2data2.txt",names=["test1","test2","Accepted"])
x1 = data['test1']
x2 = data['test2']
power = 6
data_map = feature_mapping(x1,x2,power)
X = data_map.values#构建数据集
Y = data.iloc[:,-1].values
Y = Y.reshape(len(Y),1)
theta = np.zeros((28,1))
alpha = 0.001
iters = 200000
lamda = 1
theta_final,costs = gradientDescent(X,Y,theta,alpha,iters,lamda)
fig,ax = plt.subplots()
ax.scatter(data[data['Accepted']==0]['test1'],data[data['Accepted']==0]['test2'],c='r',marker='x',label='y=0')
ax.scatter(data[data['Accepted']==1]['test1'],data[data['Accepted']==1]['test2'],c='b',marker='o',label='y=1')
#绘制决策边界
x = np.linspace(-1.2,1.2,200)
xx,yy = np.meshgrid(x,x)#生成200×200的网络采样点
z = feature_mapping(xx.ravel(),yy.ravel(),6).values#特征映射得到多维特征z,ravel()将多维数组转换成一维数组
zz = z @ theta_final
zz = zz.reshape(xx.shape)
ax.legend()
ax.set(xlabel='test1',ylabel='test2')
plt.contour(xx,yy,zz,0)
plt.show()