Skip to content

Instantly share code, notes, and snippets.

@sencagri
Created May 14, 2018 18:18
Show Gist options
  • Save sencagri/894fe544687993109d412a3fabec1d23 to your computer and use it in GitHub Desktop.
Save sencagri/894fe544687993109d412a3fabec1d23 to your computer and use it in GitHub Desktop.
from numpy import genfromtxt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets, svm
from sklearn.ensemble import GradientBoostingClassifier
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
def main():
features_train = genfromtxt("Abalone_train.csv", delimiter=',' ,usecols = (0,1,2,3,4,5,6,7), skip_header=1)
labels_train = genfromtxt("Abalone_train.csv", delimiter=',' ,usecols = (8), skip_header=1)
features_test = genfromtxt("Abalone_test.csv", delimiter=',' ,usecols = (0,1,2,3,4,5,6,7), skip_header=1)
labels_test = genfromtxt("Abalone_test.csv", delimiter=',' ,usecols = (8), skip_header=1)
## SUPPORT VECTOR MACHINE
print("Training SVM...")
clfSVM = svm.SVC(kernel='rbf', gamma=0.5, C=100)
clfSVM.fit(features_train, labels_train)
print("Training SVM completed. Now testing.")
svmPredict = clfSVM.predict(features_test)
svmConfusion = confusion_matrix(labels_test, svmPredict)
svmAccuracy = accuracy_score(labels_test, svmPredict)
print("Accuracy : " + str(svmAccuracy))
print(svmConfusion)
plt.matshow(svmConfusion)
plt.title("SVM Confusion Matrix")
plt.show()
## ENSEMBLE LEARNING
print("Training ensemble learning...")
clfEnsemble = GradientBoostingClassifier()
clfEnsemble.fit(features_train, labels_train)
print("Training ensemble completed. Now testing.")
ensemblePredict = clfEnsemble.predict(features_test)
ensembleConfusion = confusion_matrix(labels_test, ensemblePredict)
ensembleAccuracy = accuracy_score(labels_test, ensemblePredict)
print("Accuracy : " + str(ensembleAccuracy))
print(ensembleConfusion)
plt.matshow(ensembleConfusion)
plt.title("Ensemble Confusion Matrix")
plt.show()
## DECISION TREE CLASSIFIER
print("Training tree classifier...")
clfTree = tree.DecisionTreeClassifier()
clfTree.fit(features_train, labels_train)
print("Training tree classifier completed. Now testing")
treePredict = clfTree.predict(features_test)
treeConfusion = confusion_matrix(labels_test, treePredict)
treeAccuracy = accuracy_score(labels_test, treePredict)
print("Accuracy : " + str(treeAccuracy))
print(treeConfusion)
plt.matshow(treeConfusion)
plt.title("Tree Confusion Matrix")
plt.show()
## RANDOM FOREST CLASSIFIER
print("Training random forest classifier")
clfRandom = RandomForestClassifier()
clfRandom.fit(features_train, labels_train)
print("Training random forest classifier completed. Now testing")
randomPredict = clfRandom.predict(features_test)
randomConfusion = confusion_matrix(labels_test, randomPredict)
randomAccuracy = accuracy_score(labels_test, randomPredict)
print("Accuracy : " + str(randomAccuracy))
print(randomConfusion)
plt.matshow(randomConfusion)
plt.title("Random Forest Confusion Matrix")
plt.show()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment