机器学习笔记08:模型的保存与逻辑回归
程序员文章站
2022-06-18 19:18:11
机器学习笔记08:模型的保存与逻辑回归文章目录机器学习笔记08:模型的保存与逻辑回归1.模型的保存与加载2.逻辑回归:分类算法1.模型的保存与加载API:from sklearn.externals import joblibjoblib.dump(lr,"目录xxxxx.pkl")# 使用model = joblib.load("目录xxxxx.pkl")y_predict = std_y.inverse_transform(model.predict(x_test))2.逻辑回归...
机器学习笔记08:模型的保存与逻辑回归
1.模型的保存与加载
- API:
from sklearn.externals import joblib
joblib.dump(lr,"目录xxxxx.pkl")
# 使用
model = joblib.load("目录xxxxx.pkl")
y_predict = std_y.inverse_transform(model.predict(x_test))
2.逻辑回归:分类算法
- 回归的输入------>sigmoid------>概率值
- 损失函数:
- 梯度下降局部最优的问题:
- 多次随机初始化
- 求解过程中,调整学习率
- API:
sklearn.linear_model.LogisticRegression(penalty=‘l2’, C = 1.0)
自带正则化 只能解决二分类问题
Logistic回归分类器
coef_:回归系数 - 哪一个类别的样本数量少,判定概率值的值就针对这个类别。
# 逻辑回归
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression,SGDRegressor,LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error,classification_report
import pandas as pd
import numpy as np
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("breast-cancer-wisconsin.data",names = column)
# 缺失值处理
data = data.replace(to_replace = '?',value = np.nan)
data = data.dropna()
# 进行数据的分割
x_train,x_test,y_train,y_test = train_test_split(data[column[1:10]],data[column[10]],test_size = 0.25)
# 进行标准化处理
std = StandardScaler()
# 对特征值进行标准化
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
# 逻辑回归
lg = LogisticRegression(C = 1.0)
lg.fit(x_train,y_train)
print(lg.coef_)
print("准确率:",lg.score(x_test,y_test))
# 召回率
y_predict = lg.predict(x_test)
print("召回率",classification_report(y_test,y_predict,labels = [2,4],target_names =['良性','恶性']) )
return None
if __name__ == "__main__":
logistic()
- 多分类问题:在神经网络内
- 判别模型和生成模型
本文地址:https://blog.csdn.net/fafagege11520/article/details/109627200