第十五周python作业:sklearn练习

Assignment

In the second ML assignment you have to compare theperformance of three different classification algorithms, namely Naive Bayes,SVM, and Random Forest. For this assignment you need to generate a randombinary classification problem, and then train and test (using 10-fold crossvalidation) the three algorithms. For some algorithms inner cross validation(5-fold) for choosing the parameters is needed. Then, show the classificationperformace (per-fold and averaged) in the report, and briefly discussing the results.

Steps

Create a classification dataset (n samples 1000, nfeatures 10)

Split the dataset using 10-fold cross validation

Train the algorithms

     GaussianNB

     SVC (possible C values [1e-02, 1e-01, 1e00, 1e01, 1e02],RBF kernel)

     RandomForestClassifier (possible n estimators values [10,100, 1000])

Evaluate the cross-validated performance

     Accuracy

     F1-score

     AUC ROC


代码:

from sklearn import datasets
from sklearn import cross_validation
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics

dataset = datasets.make_classification(n_samples=2000, n_features=10,
			n_informative=2, n_redundant=2, n_repeated=0, n_classes=2)

# spilt using 10-fold
kf = cross_validation.KFold(1000, n_folds=10, shuffle=True)
for train_index, test_index in kf:
	X_train, y_train = dataset[0][train_index], dataset[1][train_index]
	X_test, y_test = dataset[0][test_index], dataset[1][test_index]

# Gaussian Naive Bayes
clf = GaussianNB()
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print("GaussianNB:")
print("pred: \n", pred)
print("y_test: \n", y_test)
# Evaluate the cross-validated performance
print("Evaluate the cross-validated performance:")
acc = metrics.accuracy_score(y_test, pred)
print("Accuracy: ", acc)
f1 = metrics.f1_score(y_test, pred)
print("F1-score: ",f1)
auc = metrics.roc_auc_score(y_test, pred)
print("AUC ROC: ", auc)
print("\n")

# SVC
clf = SVC(C=1e-01, kernel='rbf', gamma=0.1)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print("SVC: ")
print("pred: \n", pred)
print("y_test: \n", y_test)
# Evaluate the cross-validated performance
print("Evaluate the cross-validated performance:")
acc = metrics.accuracy_score(y_test, pred)
print("Accuracy: ", acc)
f1 = metrics.f1_score(y_test, pred)
print("F1-score: ",f1)
auc = metrics.roc_auc_score(y_test, pred)
print("AUC ROC: ", auc)
print("\n")

# Random Forest
clf = RandomForestClassifier(n_estimators=6)
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print("RandomForestClassifier: ")
print("pred: \n", pred)
print("y_test: \n", y_test)
# Evaluate the cross-validated performance
print("Evaluate the cross-validated performance:")
acc = metrics.accuracy_score(y_test, pred)
print("Accuracy: ", acc)
f1 = metrics.f1_score(y_test, pred)
print("F1-score: ",f1)
auc = metrics.roc_auc_score(y_test, pred)
print("AUC ROC: ", auc)
print("\n")


运行的结果:

GaussianNB:
pred: 
 [0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0
 1 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 0
 1 1 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0]
y_test: 
 [0 0 1 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 1 1 1 0 0
 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 0 0 1
 1 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0]
Evaluate the cross-validated performance:
Accuracy:  0.88
F1-score:  0.8799999999999999
AUC ROC:  0.8814102564102564


SVC: 
pred: 
 [0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 1 1 1 0 0
 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 0
 1 1 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0]
y_test: 
 [0 0 1 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 1 1 1 0 0
 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 0 0 1
 1 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0]
Evaluate the cross-validated performance:
Accuracy:  0.9
F1-score:  0.8979591836734695
AUC ROC:  0.9022435897435899


RandomForestClassifier: 
pred: 
 [0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 1 1 1 0 0
 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 1 1 0 0 1 1 1 1 0 0 1
 1 1 1 0 0 1 1 0 1 1 0 1 0 0 1 1 1 1 0 1 1 0 0 1 0 0]
y_test: 
 [0 0 1 1 1 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 1 1 1 0 0
 0 1 1 1 0 1 1 0 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 0 0 1
 1 1 1 0 0 1 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0]
Evaluate the cross-validated performance:
Accuracy:  0.94
F1-score:  0.9400000000000001
AUC ROC:  0.9415064102564101


由三组性能结果可以看出,在这组数据时,RandomForestClassifier性能在Accuracy,F1-score,AUC ROC中都较其他两种算法要好。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你已经安装了sklearn,但是仍然出现“ModuleNotFoundError: No module named 'sklearn'”的错误,可能是因为你的Python环境没有正确配置。你可以尝试以下几种方法来解决这个问题: 1. 确认sklearn已经正确安装。你可以在终端中输入以下命令来检查是否已经安装了sklearn: ```shell pip show scikit-learn ``` 如果sklearn已经正确安装,你应该能够看到sklearn的版本信息。 2. 确认你的Python环境已经正确配置。你可以在终端中输入以下命令来检查Python环境: ```shell python -c "import sklearn; print(sklearn.__version__)" ``` 如果你的Python环境已经正确配置,你应该能够看到sklearn的版本信息。 3. 确认你的Python环境和你的代码使用的Python环境一致。你可以在终端中输入以下命令来检查Python环境: ```shell which python ``` 然后,在你的代码中添加以下代码来确认你的代码使用的Python环境: ```python import sys print(sys.executable) ``` 如果你的Python环境和你的代码使用的Python环境不一致,你需要在你的代码中指定正确的Python环境。 4. 如果你使用的是Anaconda,你可以尝试在Anaconda Prompt中输入以下命令来安装sklearn: ```shell conda install scikit-learn ``` 如果你已经安装了sklearn,你可以尝试更新sklearn: ```shell conda update scikit-learn ``` 如果你仍然无法解决这个问题,你可以尝试在Stack Overflow等技术论坛上寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值