python中的sklearn_Python中的机器学习包 sklearn (scikit)

Python中的机器学习包 sklean (scikit)

sklean: Machine Learning in Python

======= 安装=========

0.安装scikit-learn 之前,你可能需要安装python, NumPy, SciPy, Matplotlib等。

========使用=========

# -*- coding: utf-8 -*-

"""

Created on Thu Jan 2 14:34:20 2014

example of sklearn

@author: cliff

"""

print(__doc__)

#使用SVM 分类器

from sklearn import svm

from sklearn.datasets import load_svmlight_file

#将数据集分为训练集、检验集

from sklearn.cross_validation import train_test_split

from sklearn import cross_validation

#引入评价指标

from sklearn.metrics import confusion_matrix #计算混淆矩阵

from sklearn.metrics import matthews_corrcoef #计算MCC

from sklearn.metrics import roc_auc_score

#计算MCC 只对二分类可以计算

from sklearn.metrics import accuracy_score

#计算ACC

#引入数据

fr_n="/path/your_svm_file"

X,y=load_svmlight_file(fr_n)

# Run classifier

print("===cross validation===")

clf = svm.SVC(kernel='rbf')

scores=cross_validation.cross_val_score(clf,X,y,cv=5,scoring="accuracy")

print(scores,scores.mean())

print("===performance on TEST===")

# Split the data into a training set and a test set; 分为训练集 检验集

X_train, X_test, y_train, y_test = train_test_split(X, y,

random_state=0)

clf= svm.SVC(kernel='rbf')

clf.fit(X_train,y_train)

y_pred =clf.predict(X_test)

# 计算混淆 矩阵 Compute confusion matrix

cm = confusion_matrix(y_test, y_pred)

print(cm)

#计算准确率,MCC等

print("MCC: %f " %matthews_corrcoef(y_test,y_pred))

print( "ACC: %f "

�curacy_score(y_test,y_pred))

print("===compute auc ===")

#compute the auc

classifier = svm.SVC(kernel='rbf',probability=True)

model=classifier.fit(X_train,y_train)

y_prob =classifier.predict_proba(X_test)[:,1]

#get the probability of positive

print(y_test)

print(y_prob)

print( "AUC: %f "

%roc_auc_score(y_test,y_prob))

========

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值