#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 16:37:12 2017
@author: vicky
"""
import numpy as npy
from sklearn import linear_model, datasets
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.metrics import accuracy_score, classification_report
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
Y = iris.target
for n in range(5):
print(X[n], Y[n])
#利用train_test_split函数来对原始数据集进行分类采样,取其中20%作为测试数据集,取其中80%作为训练数据集。
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
#利用LogisticRegression函数来训练一个分类器
logreg = linear_model.LogisticRegression(C=1e5,solver='lbfgs', multi_class='multinomial')
logreg.fit(X_train, y_train)
#利用已经得到的分类器来对测试数据集进行预测
prediction = logreg.predict(X_test)
print("accuracy score: ")
print(accuracy_score(y_test, prediction))
print(classification_report(y_test, prediction))
#还可以利用predict_proba()函数和predict()函数来逐条检视一下Logistic Regression的分类判别结果,请看下面的示例代码:
logreg_proba = logreg.predict_proba(X_test)
logreg_pred = logreg.predict(X_test)
for index in range (5):
print(logreg_proba[index])
print("Predict label:", logreg_pred[index])
print("Correct label:", y_test[index])