01.数据集ex2data1.txt,前两列是两个特征(第一列成绩1,第二列成绩2),最后一列是类别(0代表不被录取,1代表被录取),
利用线性逻辑回归算法训练数据集,画出决策边界.
注:逻辑回归算法也可以直接调用sklearn里的逻辑回归算法。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn import linear_model
data=np.genfromtxt("ex2data1.txt",delimiter=",")
x_data=data[:,:-1]
y_data=data[:,-1]
def plot ():
x0=[]
x1=[]
y0=[]
y1=[]
for i in range(len(x_data)):
if y_data[i]==0:
x0.append(x_data[i,0])
y0.append(x_data[i,1])
else:
x1.append(x_data[i,0])
y1.append(x_data[i,1])
plt.scatter(x0,y0,marker="o",color="b",label="label0",s=40)
plt.scatter(x1,y1,marker="*",color="y",label="label1",s=60)
plt.legend()
plot()
plt.show()
logistic=linear_model.LogisticRegression()
logistic.fit(x_data,y_data)
print('截距',logistic.intercept_)
print('系数',logistic.coef_)
print(logistic.coef_[0])
print(logistic.coef_[0][0])
plot()
x_test=np.array([[30],[100]])
y_test=(-logistic.intercept_-x_test*logistic.coef_[0][0])/logistic.coef_[0][1]
plt.plot(x_test,y_test,'r')
plt.show