逻辑回归
模型的优劣势:
优点:实现简单,易于理解和实现;计算代价不高,速度很快,存储资源低;
缺点:容易欠拟合,分类精度可能不高
查看其对应模型的w
print(‘the weight of Logistic Regression:’,lr_clf.coef_)
查看其对应模型的w0
print(‘the intercept(w0) of Logistic Regression:’,lr_clf.intercept_)
可视化决策边界
plt.figure()
plt.scatter(x_fearures[:,0],x_fearures[:,1], c=y_label, s=50, cmap=‘viridis’)
plt.title(‘Dataset’)
nx, ny = 200, 100
x_min, x_max = plt.xlim()
y_min, y_max = plt.ylim()
x_grid, y_grid = np.meshgrid(np.linspace(x_min, x_max, nx),np.linspace(y_min, y_max, ny))
z_proba = lr_clf.predict_proba(np.c_[x_grid.ravel(), y_grid.ravel()])
z_proba = z_proba[:, 1].reshape(x_grid.shape)
plt.contour(x_grid, y_grid, z_proba, [0.5], linewidths=2., colors=‘blue’)
plt.show()
基于鸢尾花(iris)数据集的逻辑回归分类实践
Step1:库函数导入
Step2:数据读取/载入
Step3:数据信息简单查看
利用.info()查看数据的整体信息
进行简单的数据查看,我们可以利用 .head() 头部.tail()尾部
利用value_counts函数查看每个类别数量
对于特征进行一些统计描述describe()
Step4:可视化描述
合并标签和特征信息
iris_all = iris_features.copy() 进行浅拷贝,防止对于原始数据的修改
iris_all[‘target’] = iris_target
特征与标签组合的散点可视化
sns.pairplot(data=iris_all,diag_kind=‘hist’, hue= ‘target’)
plt.show()
Step5:利用 逻辑回归模型 在二分类上 进行训练和预测
from sklearn import metrics
1、利用accuracy(准确度)【预测正确的样本数目占总预测样本数目的比例】评估模型效果
print(‘The accuracy of the Logistic Regression is:’,metrics.accuracy_score(y_train,train_predict))
print(‘The accuracy of the Logistic Regression is:’,metrics.accuracy_score(y_test,test_predict))
2、查看混淆矩阵 (预测值和真实值的各类情况统计矩阵)
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
print(‘The confusion matrix result:\n’,confusion_matrix_result)
3、利用热力图对于结果进行可视化
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix_result, annot=True, cmap=‘Blues’)
plt.xlabel(‘Predicted labels’)
plt.ylabel(‘True labels’)
plt.show()