逻辑回归
笛卡尔积:
两集合X和Y的笛卡尔积(Cartesian product),又称直积,表示为X × Y。
第一个对象是X的成员,第二个对象是Y的所有可能有序对的其中一个成员。
混淆矩阵:
热力图:
1.绘制散点图:
plt.scatter()
import matplotlib.pyplot as plt
plt.scatter(x, y, s=None, c=None, marker=None, cmap=None,
norm=None, vmin=None, vmax=None, alpha=None,
linewidths=None, verts=None, edgecolors=None, *,
data=None, **kwargs)
2.划分数据集:
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(iris_features_part, iris_target_part, test_size = 0.2, random_state = 2020)
3.导入逻辑回归模型:
from sklearn.linear_model import LogisticRegression
## 定义 逻辑回归模型
clf = LogisticRegression(random_state=0, solver='lbfgs')
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
penalty='l2', random_state=0, solver='lbfgs', tol=0.0001,
verbose=0, warm_start=False)
4.训练模型:clf.fit()
# 在训练集上训练逻辑回归模型
clf.fit(x_train, y_train)
print(clf.coef_)
print(clf.intercept_)
5.预测:
clf.predict()
## 在训练集和测试集上分布利用训练好的模型进行预测
train_predict = clf.predict(x_train)
test_predict = clf.predict(x_test)
6.预测精确度:
metrics.accuracy_score()
##训练集和测试集上的预测精度
from sklearn import metrics
print(metrics.accuracy_score(y_train,train_predict))
print(metrics.accuracy_score(y_test,test_predict))
7.混淆矩阵:
metrics.confusion_matrix()
confusion_matrix_result = metrics.confusion_matrix(test_predict,y_test)
8.热力图可视化:
sns.heatmap()
import seaborn as sns
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix_result, annot=True, cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.show()