Sklearn——逻辑回归
Posted 湘雨jay
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sklearn——逻辑回归相关的知识,希望对你有一定的参考价值。
#获取数据
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——逻辑回归的主要内容,如果未能解决你的问题,请参考以下文章