赞
踩
逻辑回归的输入是线性回归的结果:
回归的结果输入到sigmod函数当中
逻辑回归的最终分类是通过某个类别的概率来判断是否属于某个类别,并且这个类别默认标记为1(正例),另一个标记为0(反例)。默认目标值少的为正例。
逻辑回归的损失,称之为对数似然损失,公式如下:
如上可知,降低损失需要(正例减少sigmoid返回结果,反例增加sigmod返回结果)
同样使用梯度下降优化算法,去减少损失函数的值,这样去更新逻辑回归前面对应算法的权重参数,提升原本属于1类别的概率,降低原本为0类别的概率。
sklearn.linear_model.LogisticRegression(solver=‘liblinear’,penalty=‘i2’,c=1.0)
默认将类别数量少的当正例
数据源:https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression def logisticregression(): '''逻辑回归癌症预测''' # 确定数据columns数值 columns = ["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.data",names=columns) # 去掉缺失值 data.replace(to_replace="?",value=np.nan,inplace=True) data.dropna(axis=0,inplace=True,how="any") # 提取目标值 target = data["Class"] # 提取特征值 data = data.drop(["Sample code number"],axis=1).iloc[:,:-1] # 切割训练集和测试集 x_train,x_test,y_train,y_test = train_test_split(data,target,test_size=0.3) # 进行标准化 std = StandardScaler() x_train = std.fit_transform(x_train) x_test = std.fit_transform(x_test) # 逻辑回归进行训练和预测 lr = LogisticRegression() lr.fit(x_train,y_train) print("逻辑回归权重:",lr.coef_) print("逻辑回归偏置:",lr.intercept_) # 逻辑回归测试集预测结果 pre_result = lr.predict(x_test) print(pre_result) # 逻辑回归预测准确率 sore = lr.score(x_test,y_test) print(sore) if __name__ == '__main__': logisticregression()
预测结果为正例样本中真是为整理的比例(查的准)
真是为正例的样本中预测结果为正例的比例(查的全,对正样本的区分能力)
反应了模型的稳健型
sklearn.metrics.classification_report(y_true,y_pred,target_names=None)
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report def logisticregression(): '''逻辑回归癌症预测''' # 确定数据columns数值 columns = ["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.data",names=columns) # 去掉缺失值 data.replace(to_replace="?",value=np.nan,inplace=True) data.dropna(axis=0,inplace=True,how="any") # 提取目标值 target = data["Class"] # 提取特征值 data = data.drop(["Sample code number"],axis=1).iloc[:,:-1] # 切割训练集和测试集 x_train,x_test,y_train,y_test = train_test_split(data,target,test_size=0.3) # 进行标准化 std = StandardScaler() x_train = std.fit_transform(x_train) x_test = std.fit_transform(x_test) # 逻辑回归进行训练和预测 lr = LogisticRegression() lr.fit(x_train,y_train) # 得到训练集返回数据 # print("逻辑回归权重:",lr.coef_) # print("逻辑回归偏置:",lr.intercept_) # 逻辑回归测试集预测结果 pre_result = lr.predict(x_test) # print(pre_result) # 逻辑回归预测准确率 sore = lr.score(x_test,y_test) print(sore) # 精确率(Precision)与召回率(Recall) report = classification_report(y_test,pre_result,target_names=["良性","恶性"]) print(report) if __name__ == '__main__': logisticregression()
问题:如何衡量样本不均衡下的评估?
ROC曲线的横轴就是FPRate,纵轴就是TPRate,当二者相等时,表示的意义则是:对于不论真实类别时1还是0的样本,分类器预测为1的概率是相等的,此时AUC为0.5 。
最终AUC的范围在[0.5,1],并且越接近1越好。
from sklearn.metrics import roc_auc_score
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report,roc_auc_score def logisticregression(): '''逻辑回归癌症预测''' # 确定数据columns数值 columns = ["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.data",names=columns) # 去掉缺失值 data.replace(to_replace="?",value=np.nan,inplace=True) data.dropna(axis=0,inplace=True,how="any") # 提取目标值 target = data["Class"] # 提取特征值 data = data.drop(["Sample code number"],axis=1).iloc[:,:-1] # 切割训练集和测试集 x_train,x_test,y_train,y_test = train_test_split(data,target,test_size=0.3) # 进行标准化 std = StandardScaler() x_train = std.fit_transform(x_train) x_test = std.fit_transform(x_test) # 逻辑回归进行训练和预测 lr = LogisticRegression() lr.fit(x_train,y_train) # 得到训练集返回数据 # print("逻辑回归权重:",lr.coef_) # print("逻辑回归偏置:",lr.intercept_) # 逻辑回归测试集预测结果 pre_result = lr.predict(x_test) # print(pre_result) # 逻辑回归预测准确率 sore = lr.score(x_test,y_test) print(sore) # 精确率(Precision)与召回率(Recall) report = classification_report(y_test,pre_result,target_names=["良性","恶性"]) print(report) # 查看AUC指标 y_test = np.where(y_test>2.5,1,0) print(y_test) auc_score = roc_auc_score(y_test,pre_result) print(auc_score) if __name__ == '__main__': logisticregression()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。