一、原理
结果数值仅有0和1,无法使用直接线性模型
公式原型:(由 和 组合而成)
曲线表示概率
二、Python实现
①工作路径设置
②数据预处理
③逻辑回归模型的创建和拟合
from sklearn.Linear_model import logisticcRegression --分属线性回归模型
classifier = logisticRegression(random_state = 0)
calssifier.fit(x_train,y_train) --拟合
④预测与评估(混淆矩阵)
y_pred = classifier.predict(x_test) --预测结果
from sklearn.merics import confusion_matrix --导入混淆矩阵
cm = confusion_matrix(y_test,y_pred) --真实分类,预测结果
--绘制图像
from matplotlib,colors import ListedColormap --引入图像类
x_set,y_set = x_train,y_train --临时变量
x1,x2 = np.meshgrid(np.arange(start = x_set[:0],min()-1,stop=x_set[:,0].max()+1,setp=0.01),
np.arange(start = x_set[:1],min()-1,stop=x_set[:,1].max()+1,setp=0.01))
--设置画布,-1和+1是为了图像边缘留白
plt.contourf(x1,x2,classifier.predict(np.arry([x1.ravel(),x2.ravel()].T).reshape(x1.shape),
alpha=0.75,camp=ListedColormap('red','green')))
--将分类器着色(按照0,1)
plt.xlim(x.min(),x.max())
plt.ylim(y.min(),y.max())
--标注最大最小值
for i,j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set==j,0],x_set[y_set==j,1],c=ListedColormap(('orange','bule'))(i),
label=j)
--循环画点
plt.title('逻辑回归图像')
plt.xlabel='年龄'
plt.ylabel='收入'
plt.legend()
plt.show()
此情况实际是非线性的,故使用线性分类器会造成部分样本的错误分类。
相关阅读
发表评论