机器学习—Logistic Regression

Posted 杰哥哥是谁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了机器学习—Logistic Regression相关的知识,希望对你有一定的参考价值。

一、一般模型

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from sklearn.datasets import load_iris
%matplotlib inline
#载入数据
iris = load_iris()
x = iris.data
y = iris.target
x_train,x_test,y_train,y_test = train_test_split(x,y,train_size=0.7,random_state=0)
#数据标准化
sc = StandardScaler()
x_train_std = sc.fit_transform(x_train)
x_test_std = sc.transform(x_test)
#建立模型
lr = LogisticRegression()
lr.fit(x_train_std,y_train)
y_pred = lr.predict(x_test_std)
#检验模型
accuracy_score = metrics.accuracy_score(y_test,y_pred)   #错误率,也就是np.average(y_test==y_pred)
accuracy_score

结果是:0.82222222222222219

二、加入正则项:

from sklearn.linear_model import RidgeClassifierCV
alpha = np.logspace(-3,2,10)
ridge_model = RidgeClassifierCV(alphas=alpha,cv=5)
ridge_model.fit(x_train_std,y_train)
ridge_model.alpha_
y_pred_ridge = ridge_model.predict(x_test_std)
accuracy_score = metrics.accuracy_score(y_test,y_pred_ridge)
accuracy_score

结果是:0.77777777777777779

以上是关于机器学习—Logistic Regression的主要内容,如果未能解决你的问题,请参考以下文章

机器学习实战python3 Logistic Regression

机器学习实战四(Logistic Regression)

机器学习——逻辑回归(Logistic Regression)

coursera 机器学习 logistic regression 逻辑回归的项目

机器学习系列-Logistic Regression

机器学习Octave 实现逻辑回归 Logistic Regression