码迷,mamicode.com
首页 > 其他好文 > 详细

机器学习—Logistic Regression

时间:2017-10-06 13:25:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:core   fit   plot   stand   mod   cal   orm   panda   repr   

一、一般模型

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

标签:core   fit   plot   stand   mod   cal   orm   panda   repr   

原文地址:http://www.cnblogs.com/jiegege/p/7629992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!