当前位置:   article > 正文

逻辑回归-二分类问题_excel的logist二分类回归

excel的logist二分类回归

 

目录

1.原理

1.1输入

1.2激活函数(sigmoid函数 )

2.损失

 3.案例


1.原理

1.1输入

1.2激活函数(sigmoid函数 )

                         

判断标准

回归的结果输⼊到sigmoid函数当中

输出结果:[0, 1]区间中的⼀个概率值,默认为0.5为阈值,其图像如下

 

逻辑回归最终的分类是通过属于某个类别的概率值来判断是否属于某个类别,并且这个类别默认标记为1(正例), 另外的⼀个类别会标记为0(反例)

2.损失

 

 当y=1时,我们希望h (x)值越⼤越好

 当y=0时,我们希望h (x)值越⼩越好

 

 -log(P), P值越⼤,结果越⼩

 优化:提升原本属于1类别的概率,降低原本是0类别的概率。

 对于⼩数据集来说,“liblinear”是个不错的选择,⽽“sag”和'saga'对于⼤型数据集会更快。

 3.案例

  1. import pandas as pd
  2. import numpy as np
  3. from sklearn.model_selection import train_test_split
  4. from sklearn.preprocessing import StandardScaler
  5. from sklearn.linear_model import LogisticRegression
  6. from sklearn.metrics import classification_report,roc_auc_score
  7. # 1.获取数据
  8. names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
  9. 'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin',
  10. 'Normal Nucleoli', 'Mitoses', 'Class']
  11. data=pd.read_csv("D:/迅雷下载/demo/machineLearnCode/LogisticRegressionTest//breast-cancer-wisconsin.data",names=names)
  12. # 2.基本数据处理
  13. # 2.1 缺失值处理
  14. data=data.replace(to_replace="?",value=np.nan) #把问号替换为nan
  15. data=data.dropna()
  16. # 2.2 确定特征值,⽬标值
  17. x=data.iloc[:,1:-1]
  18. y=data["Class"]
  19. # 2.3 分割数据
  20. x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=22,test_size=0.2)
  21. # 3.特征⼯程(标准化)
  22. transfer=StandardScaler()
  23. x_train=transfer.fit_transform(x_train)
  24. x_test=transfer.transform(x_test)
  25. # 4.机器学习(逻辑回归)
  26. estimator=LogisticRegression()
  27. estimator.fit(x_train,y_train)
  28. # 5.模型评估
  29. y_predict=estimator.predict(x_test) #准确率
  30. print(y_predict)
  31. print(estimator.score(x_test,y_test))
  32. # 5.1精确率和召回率
  33. ret=classification_report(y_test,y_predict,labels=(2,4),target_names=("良性","恶性"))
  34. print(ret)
  35. # 5.2auc指标计算
  36. y_test=np.where(y_test>3,1,0)
  37. print(roc_auc_score(y_test,y_predict))

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/333634
推荐阅读
相关标签
  

闽ICP备14008679号