首页 > 代码库 > Sklearn——逻辑回归

Sklearn——逻辑回归

#获取数据

from sklearn.datasets import load_iris

iris=load_iris()

data=http://www.mamicode.com/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——逻辑回归