基于逻辑回归实现乳腺癌预测
将乳腺癌数据集拆分成训练集和测试集,搭建一个逻辑回归模型,对训练集进行训练,然后分别对训练集和测试集进行预测。输出以下结果:
该模型在训练集上的准确率,在测试集上的准确率、召回率和精确率。
源码
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import recall_score,precision_score,classification_report,accuracy_scorecancer = load_breast_cancer()
x_train,x_test,y_train,y_test = train_test_split(cancer.data,cancer.target,test_size=0.2)
model = LogisticRegression(max_iter=10000)
model.fit(x_train,y_train)
train_score = model.score(x_train,y_train)
test_score = model.score(x_test,y_test)print("1 基于逻辑回归实现乳腺癌预测")
print("李思强 20201107148")
print("训练集")
print("准确率:",train_score)y_pred = model.predict(x_test)
accuracy_score_value = accuracy_score(y_test,y_pred)
recall_score_value = recall_score(y_test,y_pred)
precision_score_value = precision_score(y_test,y_pred)print("测试集")
print("准确率:",accuracy_score_value)
print("召回率:",recall_score_value)
print("精确率:",precision_score_value)