导入包
import pandas as pd import numpy as np from sklearn.datasets import load_boston from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error, classification_report from sklearn.externals import joblib
构造列标签名字
column = ['Sample code number','Clump Thickness', 'Uniformity of Cell Size','Uniformity of Cell Shape','Marginal Adhesion', 'Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
读取数据
data = pd.read_csv("breast-cancer-wisconsin.csv", names=column) data.head()
缺失值进行处理
data = data.replace(to_replace='?', value=np.nan) data = data.dropna()
数据的分割
x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
标准化处理
std = StandardScaler() x_train = std.fit_transform(x_train) x_test = std.transform(x_test)
逻辑回归预测
lg = LogisticRegression(C=1.0) lg.fit(x_train, y_train) print(lg.coef_)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True, intercept_scaling=1, max_iter=100, multi_class='warn', n_jobs=None, penalty='l2', random_state=None, solver='warn', tol=0.0001, verbose=0, warm_start=False)
[[ 1.60392495 -0.11066665 0.93702846 1.01160157 -0.31111269 1.20876603 1.20701977 1.04581779 0.81269039]]
y_predict = lg.predict(x_test) print("准确率:", lg.score(x_test, y_test)) print("召回率:", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))