分类算法-----逻辑回归
逻辑回归主要是一个二分类问题,是解决二分类的重要工具。
同样的都是回归性质,它本身也存在损失问题-----损失函数
损失函数: 均方误差(线性回归)
对数似然损失(逻辑回归)
解决方法:
均利用梯度下降法求解即可。
API:
•
sklearn.linear_model.LogisticRegression
解释:
•
sklearn.linear_model.LogisticRegression
(
penalty=‘l2’,
C = 1.0
)
•
•
Logistic
回归分类器
•
coef
_
:回归系数
肿瘤测试案例:
代码:
import numpy as np
import pandas as pd
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 logistic():
"""逻辑回归:关于癌症检测的二分问题"""
# 构造标签
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("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data", names=column)
# 处理数据 : 数据中存在? 用 np.nan 代替
data = data.replace(to_replace="?", value=np.nan)
# 讲这些违法数据全部删除
data.dropna()
# 数据分割
x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
# 数据的标准化处理
sd = StandardScaler()
sd.fit_transform(x_train)
sd.transform(x_test)
# 逻辑回归预测
lg = LogisticRegression(C=1.0)
lg.fit(x_train, x_test)
# 得出预测结果
y_predict = lg.predict(y_train)
# 准确率
zq = lg.score(x_test, y_test)
print("准确率:", zq)
# 召回率
zh = classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"])
def main():
logistic()
if __name__ == '__main__':
main()
pands使用:
•pd.read_csv(’’,names=column_names)
•column_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']
'Single Epithelial Cell Size','Bare Nuclei','Bland Chromatin','Normal Nucleoli','Mitoses','Class']
•return:数据
•
•replace(to_replace=’’,value=):返回数据
•dropna():返回数据
总结:
应用:广告点击率预测、电商购物搭配推荐
优点:适合需要得到一个分类概率的场景
缺点:当特征空间很大时,逻辑回归的性能不是很好
(看硬件能力)