我学习的b站中机器学习的教程(线性回归和逻辑回归(点击学习))
此处csdn中含有上条链接中逻辑回归和线性回归的讲解实例代码
在视频学习中结合csdn文字版理解逻辑回归线性回归的教程理解教程(逻辑特别清楚)
癌症肿瘤良性、恶性预测 数据集获取
我张贴的代码的数据集下载为以下百度云链接
链接:https://pan.baidu.com/s/1dSGioVcbPKoJUS8ta7TteQ
提取码:h5jh
复制这段内容后打开百度网盘手机App,操作更方便哦
代码如下使用时提前进行相关库的下载和安装,同时更改代码中数据集的地址
import numpy.random
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing as pp
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, SGDRegressor, LogisticRegression
def logistic():
path = r'D:\小美元\代码\ml-机器学习\data\breast-cancer-wisconsin.csv'#更改此处地址
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(path, names=column)
print(data)
# 缺失值进行数据处理
data = data.replace(['?'],np.nan)
print(data)
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()# 数据标准化
print('be4',x_train,x_test)
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
print('After',x_train, x_test)
# 逻辑回归开始预测
lg = LogisticRegression(C=1.0)
lg.fit(x_train, y_train)
print(lg.coef_) # 十个特征值的参数
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=["良性", "恶性"]))
return None
if __name__ == "__main__":
logistic()
模型建立成功后模型准确率和召回率如下