代码
from sklearn.datasets import load_boston
from sklearn.linear_model import 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
import pandas as pd
import numpy as np
from sklearn.externals import joblib
def charge_data():
colums=["colun1","colum2","colum3","colum4","colum5","colum6","colum7","colum8","colum9","colum10","TARGET"]
data=pd.read_csv("./breast-cancer-wisconsin.data",names=colums)
data=data.replace(to_replace="?",value=np.nan)
data=data.dropna()
x_train,x_text,y_train,y_text=train_test_split(data[colums[1:10]],data[colums[10]],test_size=0.25)
std=StandardScaler()
std.fit_transform(x_train)
std.transform(x_text)
lg=LogisticRegression(C=1.0)
lg.fit(x_train,y_train)
print("回归参数:",lg.coef_)
pre=lg.predict(x_train)
print("预测值",pre)
print("准确率:",lg.score(x_text,y_text))
print("召回率:\n",classification_report(y_train,pre,labels=[2,4],target_names=["良性","恶性"]))
return None
def get_train():
colums = ["colun1", "colum2", "colum3", "colum4", "colum5", "colum6", "colum7", "colum8", "colum9", "colum10",
"TARGET"]
data = pd.read_csv("./breast-cancer-wisconsin.data", names=colums)
data = data.replace(to_replace="?", value=np.nan)
data = data.dropna()
x_train, x_text, y_train, y_text = train_test_split(data[colums[1:10]], data[colums[10]], test_size=0.25)
std = StandardScaler()
std.fit_transform(x_train)
std.transform(x_text)
lg = LogisticRegression(C=1.0)
lg.fit(x_train, y_train)
joblib.dump(lg,filename="text.pkl")
print("保存成功")
return None
if __name__ == '__main__':
colums = ["colun1", "colum2", "colum3", "colum4", "colum5", "colum6", "colum7", "colum8", "colum9", "colum10",
"TARGET"]
data=pd.read_csv("./breast-cancer-wisconsin.data",names=colums)
data=data.replace(to_replace="?",value=np.nan)
data=data.dropna()
x_train,x_text,y_train,y_text=train_test_split(data[colums[1:10]],data[colums[10]],test_size=0.25)
lg=joblib.load("text.pkl")
pre=lg.predict(x_text)
print("预测数据是",pre)
print("预测准确率",lg.score(x_train,y_train))