赞
踩
逻辑回归(Logistic Regression)虽然叫回归,其实是分类模型。逻辑回归用于解决二分类问题。
逻辑回归的原理:
输入:(线性回归的输出)
h
(
w
)
=
w
1
x
1
+
w
2
x
2
+
w
3
x
3
…
+
b
h(w)=w_{1} x_{1}+w_{2} x_{2}+w_{3} x_{3} \ldots+\mathrm{b}
h(w)=w1x1+w2x2+w3x3…+b
激活函数(sigmoid函数,用于将线性回归的输出映射到[0,1]上):
g
(
w
T
,
x
)
=
1
1
+
e
−
h
(
w
)
=
1
1
+
e
−
w
T
x
g\left(w^{T}, x\right)=\frac{1}{1+e^{-h(w)}}=\frac{1}{1+e^{-w^{T} x}}
g(wT,x)=1+e−h(w)1=1+e−wTx1
流程:
逻辑回归的损失和优化:
损失:
cost
(
h
θ
(
x
)
,
y
)
=
{
−
log
(
h
θ
(
x
)
)
if
y
=
1
−
log
(
1
−
h
θ
(
x
)
)
if
y
=
0
\operatorname{cost}\left(h_{\theta}(x), y\right)=\left\{−log(hθ(x)) if y=1−log(1−hθ(x)) if y=0
优化:
LogisticRegression⽅法相当于 SGDClassifier(loss=“log”, penalty=" "),SGDClassifier实现了⼀个普通的随机梯度下降学习。⽽使⽤LogisticRegression(实现了SAG)
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
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# 1.获取数据
names = ['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("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",names=names)
data.head()
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 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1000025 | 5 | 1 | 1 | 1 | 2 | 1 | 3 | 1 | 1 | 2 |
1 | 1002945 | 5 | 4 | 4 | 5 | 7 | 10 | 3 | 2 | 1 | 2 |
2 | 1015425 | 3 | 1 | 1 | 1 | 2 | 2 | 3 | 1 | 1 | 2 |
3 | 1016277 | 6 | 8 | 8 | 1 | 3 | 4 | 3 | 7 | 1 | 2 |
4 | 1017023 | 4 | 1 | 1 | 3 | 2 | 1 | 3 | 1 | 1 | 2 |
# 2.基本数据处理
# 2.1 缺失值处理
data = data.replace(to_replace="?", value=np.NaN)
data = data.dropna()
# 2.2 确定特征值,⽬标值
x = data.iloc[:, 1:10] #特征值包括第一列到倒数第二列
y = data["Class"] #目标值是最后一列
# 2.3 分割数据
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22)
# 3.特征⼯程(标准化)
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.机器学习(逻辑回归)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)
# 5.模型评估
y_predict = estimator.predict(x_test)
y_predict
print("模型评估:",estimator.score(x_test, y_test))
模型评估: 0.9766081871345029
注意:
除了准确率,还有以下的分类评估方法;
在分类任务下,预测结果(Predicted Condition)与正确标记(True Condition)之间存在四种不同的组合,构成混淆矩阵(适⽤于多分类)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qgIiNmCu-1660046313392)(attachment:image.png)]
from sklearn.metrics import classification_report
ret = classification_report(y_test, y_predict, labels=(2,4), target_names=("良性", "恶性"))
print(ret)
precision recall f1-score support
良性 0.98 0.98 0.98 111
恶性 0.97 0.97 0.97 60
accuracy 0.98 171
macro avg 0.97 0.97 0.97 171
weighted avg 0.98 0.98 0.98 171
这个指标主要⽤于评价不平衡的⼆分类问题(例如训练样本中正例的比例远远大于反例)
from sklearn.metrics import roc_auc_score
# 0.5~1之间,越接近于1越好
#先将数据化为0和1
y_test = np.where(y_test > 2.5, 1, 0)
print("AUC指标:", roc_auc_score(y_test, y_predict))
AUC指标: 0.9743243243243243
ROC曲线的横轴就是FPRate,纵轴就是TPRate,当⼆者相等时,表示的意义则是:对于不论真实类别是1还是0的样本,分类器预测为1的概率是相等的,此时AUC为0.5(即纯属瞎猜)
其实AUC的意思是——Area Under roc Curve,就是ROC曲线的积分,也是ROC曲线下⾯的⾯积。
绘制ROC曲线的意义很明显,不断地把可能分错的情况扣除掉,从概率最⾼往下取的点,每有⼀个是负样本,就会导致分错排在它下⾯的所有正样本,所以要把它下⾯的正样本数扣除掉(1-TPR,剩下的正样本的⽐例)。总的ROC曲线绘制出来了,AUC就定了,分对的概率也能求出来了。
关于类别不平衡的问题,主要有两种处理方式:
b站 黑马程序员 机器学习
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。