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

Sklearn——逻辑回归

时间:2017-07-02 18:29:44      阅读:843      评论:0      收藏:0      [点我收藏+]

标签:logistic   dict   set   targe   类型   mac   positive   error   rate   

#获取数据

from sklearn.datasets import load_iris

iris=load_iris()

data=iris.data

target=iris.target

#选定训练集和测试集

from sklearn.cross_validation import train_test_split

X_train,X_test,Y_train,Y_test=train_test_split(data,target,test_size=0.33)#

#建立模型

from sklearn.linear_model import LogisticRegression

classifier=LogsisticRegression()#建立实例

classifier.fit(sample,target)#训练模型

predictions=classifier.predict(X_test)#预测结果

#评估模型

##混淆矩阵(二分类)

from sklear.metrics import confusion_matrix

matrix=confusion_matrix(Y_test,predictions)

plt.matshow(matrix)

plt.title("混淆矩阵")

plt.colorbar()

plt.ylabel("实际类型")

plt.xlabel("预测类型")

plt.show()

#准确率

from sklearn.cross_validation import cross_val_score

scores=cross_val_score(classifier,X_train,Y_train,cv=5)

np.mean(scores)

#精准率,召回率

scores=cross_val_score(classifier,X_train,Y_train,cv=5,scoring="accuracy")

scores=cross_val_score(classifier,X_train,Y_train,cv=5,scoring="recall_weighted")

Valid options are [‘accuracy‘, ‘adjusted_rand_score‘, ‘average_precision‘, ‘f1‘, ‘f1_macro‘, ‘f1_micro‘, ‘f1_samples‘, ‘f1_weighted‘, ‘log_loss‘, ‘mean_absolute_error‘, ‘mean_squared_error‘, ‘median_absolute_error‘, ‘precision‘, ‘precision_macro‘, ‘precision_micro‘, ‘precision_samples‘, ‘precision_weighted‘, ‘r2‘, ‘recall‘, ‘recall_macro‘, ‘recall_micro‘, ‘recall_samples‘, ‘recall_weighted‘, ‘roc_auc‘]

#ROC,AUC

from sklearn.metrics import roc_curve,auc

predictions=classifier.predict_proba(X_test)#每一类的概率

false_positive_rate, recall, thresholds = roc_curve(Y_test, predictions[:, 1])#有些问题

roc_auc=auc(false_positive_rate,recall)

 

Sklearn——逻辑回归

标签:logistic   dict   set   targe   类型   mac   positive   error   rate   

原文地址:http://www.cnblogs.com/liuting1990/p/7106364.html

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