机器学习☞☞逻辑回归

介绍

逻辑回归是机器学习的一种分类模型

 损失及优化
损失

逻辑回归的损失,叫做对数似然损失

 所以要让损失函数越小越好。

当y = 1,h (x)越大越好

当y = 0,h (x)越小越好

 优化

使用梯度下降优化算法,让损失函数越小越好,所以y=1时,增大h(x)让它更接近1,即提升原本属于1类别的概率;y=0时,减小h(x)让它更接近0,即降低原本属于0类别的概率

 逻辑回归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

# 数据:"https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data"
# 获取数据
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("breast-cancer-wisconsin.data", names=names)
# 基本数据处理
data.replace(to_replace="?", value=np.NaN, inplace=True)
data.dropna(inplace=True)

x = data.iloc[:, 1:10]
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.fit_transform(x_test)

# 机器学习(逻辑回归)
estimator = LogisticRegression()
estimator.fit(x_train, y_train)

# 模型评估
ret = estimator.score(x_test, y_test)
print("准确率:",ret)

y_pre = estimator.predict(x_test)
print("预测值:",y_pre)
分类评估方法
混淆矩阵

 精确率与召回率

精确率:预测结果为正例样本中真实为正例的比例

召回率:真实为正例的样本中预测为正例的比例

F1-score

衡量模型的稳健性

分类评估报告api 
# 精确率召回率指标评价
r = classification_report(y_test,y_pre,labels=(2, 4),target_names=("良性","恶性"))
print(r)
ROC曲线和AUC指标

TPR:就是刚才说的召回率

FPR:所有真实类别为0的样本中,预测类别为1的比例

ROC曲线是以FPRate为横轴、TPRate为纵轴的曲线,当两者相等时,即y = x说明在乱猜,y > x越大说明猜的越对;反之,猜的越反

ROC形成一个指标AUC

# auc指标计算
y_test = np.where(y_test>3,1,0)

roc_auc_score(y_test, y_pre)
处理类别不平衡

lmblearn包,专门处理数据比例失衡的问题

conda install imbalanced-learn
过采样

增加数量较少的那一类样本的数量

随机过采样

from sklearn.datasets import make_classification
import matplotlib.pyplot as plt
from collections import Counter
from imblearn.over_sampling import RandomOverSampler

# 使⽤make_classification⽣成样本数据
# 准备类别不平衡数据
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)
print(Counter(y))

ros = RandomOverSampler(random_state=0)

x_resampled, y_resampled = ros.fit_resample(X,y)
print(Counter(y_resampled))

随机过采样缺点:

     造成模型训练复杂度加大

     容易造成模型的过拟合问题 

为了解决随机过采样中造成的模型过拟合问题,又能实现数据集均衡的目的,出现了过采样法代表性的算法smote算法

SMOTE算法

原理:找出少数类样本的K个近邻,随机从这K个近邻中选出⼀个样本,在少数类样本和被选中的这个近邻样本之间的连线上,随机找⼀点。这个点就是人工合成的新的样本点。

from imblearn.over_sampling import SMOTE

x_resampled, y_resampled = SMOTE().fit_resample(X, y)
print(Counter(y_resampled))
欠采样

减少数量较多的那一类样本的数量

随机欠采样

from imblearn.under_sampling import RandomUnderSampler

rus = RandomUnderSampler(random_state=0)
X_resampled, y_resampled = rus.fit_resample(X, y)
Counter(y_resampled)

 缺点:会造成信息缺失,将多数样本删除可能会导致丢失有关多数类的重要信息

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值