5.逻辑回归
什么是逻辑回归应用场景逻辑回归的原理掌握逻辑回归,必须掌握以下两点逻辑回归中,其输入值是什么如何判断逻辑回归的输出输入激活函数衡量损失损失优化API肿瘤预测案例数据介绍代码实现import pandas as pdimport numpy as npfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocess
·
什么是逻辑回归
应用场景
逻辑回归的原理
掌握逻辑回归,必须掌握以下两点
- 逻辑回归中,其输入值是什么
- 如何判断逻辑回归的输出
输入
激活函数
衡量损失
损失
优化
API
肿瘤预测案例
数据介绍
代码实现
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
# 获取数据
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()
# 数据基本处理
# 缺失值处理
data = data.replace(to_replace="?",value=np.nan)
data = data.dropna()
# 确定特征值,目标值
x = data.iloc[:,1:-1]
y = data["Class"]
# 分割数据
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=22,test_size=0.2)
# 特征工程 标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 机器学习
estmator = LogisticRegression()
estmator.fit(x_train,y_train)
# 模型评估
print("准确率:\n",estmator.score(x_test,y_test))
print("预测值:\n",estmator.predict(x_test))
分类评估方法
准确率与召回率
混淆矩阵
我们之前使用的准确率公式为:(TP+TN)/(TP+Fn+FP+TN)
精确率(Precision)与召回率(Recall)
精确率:(TP)/(TP+FP)
召回率:(TP)/(TP+FN)
F1-score
分类评估报告api
from sklearn.metrics import classification_report
y_pre = estmator.predict(x_test)
ret = classification_report(y_test,y_pre,labels=(2,4),target_names=("良性","恶性"))
print(ret)
ROC曲线与AUC指标
TPR与FPR
ROC曲线
AUC指标
AUC计算API
from sklearn.metrics import roc_auc_score
y_test = np.where(y_test>3,1,0)
roc_auc_score(y_test,y_pre)
解决类别不平衡问题
pip3 install imbalanced-learn
准备类别不平衡数据
from sklearn.datasets import make_classification
import matplotlib.pylab as plt
from collections import Counter
X,Y = make_classification(n_samples=5000,
n_features=2, # 特征个数= n_informative()+ n_redundant()+ n_repeated()
n_informative=2,# 多信息特征的个数
n_redundant=0,# 冗余信息,informative特征的随机线性组合
n_repeated=0,# 重复信息,随机提取n_informative和n_redundant特征
n_classes=3,# 分类类别
n_clusters_per_class=1,# 某一个类别是由几个cluster构成的
weights=[0.01,0.05,0.94],# 列表类型,权重比
random_state=0)
X,Y,X.shape
Counter(y)
# 数据可视化
plt.scatter(X[:,0],X[:,1],c=Y)
plt.show()
解决办法
过采样方法
随机过采样方法
from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(random_state=0)
X_resampled,Y_resampled = ros.fit_resample(X,Y)
Counter(Y_resampled)
# 数据可视化
plt.scatter(X_resampled[:,0],X_resampled[:,1],c=Y_resampled)
plt.show()
过采样代表性算法-SMOTE
from imblearn.over_sampling import SMOTE
X_resampled,Y_resampled = SMOTE().fit_resample(X,Y)
Counter(Y_resampled)
# 数据可视化
plt.scatter(X_resampled[:,0],X_resampled[:,1],c=Y_resampled)
plt.show()
欠采样方法
随机欠采样方法
from imblearn.under_sampling import RandomUnderSampler
rus = RandomUnderSampler(random_state=0)
X_resampled,Y_resampled = rus.fit_resample(X,Y)
Counter(Y_resampled)
# 数据可视化
plt.scatter(X_resampled[:,0],X_resampled[:,1],c=Y_resampled)
plt.show()
更多推荐
已为社区贡献7条内容
所有评论(0)