python机器学习入门上课代码记录4

 逻辑回归

from sklearn.datasets import load_breast_cancer
dataset = load_breast_cancer() # 导入乳腺癌数据
print(dataset.data.shape)      # dataset['data']
print(dataset.target.shape)
print(dataset.feature_names)
print(dataset.target_names)    #['malignant'恶性的 'benign'良性的# ]
# 对数据集进行拆分
from sklearn.model_selection import train_test_split
train_x, test_x, train_y, test_y = train_test_split(dataset.data, dataset.target, random_state=0)
from sklearn.linear_model import LogisticRegression
from sklearn import metrics # 使用metrics显示算法效果
# 建立逻辑回归模型
clf = LogisticRegression(random_state=0, solver='lbfgs', max_iter=4000, multi_class='multinomial')
clf.fit(train_x, train_y)
print('coef:\n', clf.coef_) # 查看参数
print('intercept:\n', clf.intercept_)
predict_y = clf.predict(test_x) # 预测
print(clf.score(test_x, test_y))
print('Accuracy on training set: {:.2f}'.format(clf.score(train_x, train_y)))
print('Accuracy on test set: {:.2f}'.format(clf.score(test_x, test_y)))

你可能感兴趣的:(python,python,sklearn)