-
-
Save SangramBarge/f4c6078778e62ad988342f5c7aaf407a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division # only for Python 2 | |
from sklearn import datasets | |
from sklearn import svm | |
from sklearn import tree | |
from sklearn.ensemble import RandomForestClassifier | |
from sklearn.model_selection import train_test_split as tts | |
from sklearn.metrics import accuracy_score | |
wine = datasets.load_wine() | |
features = wine.data | |
labels = wine.target | |
# split the data into training and testing | |
train_feats, test_feats, train_labels, test_labels = tts(features, labels, test_size=0.2) | |
# SVM with RBF kernel. Default setting of SVM. | |
#clf = svm.SVC() | |
# SVM with linear kernel | |
#clf = svm.SVC(kernel='linear') | |
# Decision Tree Classifier | |
#clf = tree.DecisionTreeClassifier() | |
# Random Forest Classifier | |
clf = RandomForestClassifier() | |
# print the details of the Classifier used | |
print "Using", clf | |
# training | |
clf.fit(train_feats, train_labels) | |
# predictions | |
predictions = clf.predict(test_feats) | |
print "\nPredictions:", predictions | |
score = 0 | |
for i in range(len(predictions)): | |
if predictions[i] == test_labels[i]: | |
score += 1 | |
print "Accuracy:", (score / len(predictions)) * 100, "%" | |
# or, just do this for accuracy | |
# print accuracy_score(test_labels, predictions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment