Skip to content

Instantly share code, notes, and snippets.

@albrzykowski
Last active August 24, 2018 01:09
Show Gist options
  • Save albrzykowski/5ec785850ded0da8b303e991f8ec62e0 to your computer and use it in GitHub Desktop.
Save albrzykowski/5ec785850ded0da8b303e991f8ec62e0 to your computer and use it in GitHub Desktop.
'''
Classification of Autistic Spectrum Disorder based on:
https://archive.ics.uci.edu/ml/datasets/Autistic+Spectrum+Disorder+Screening+Data+for+Children++ dataset.
Only 10 answears are taking account as features.
'''
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.model_selection import GridSearchCV
from sklearn.decomposition import PCA
import numpy as np
from sklearn.metrics import make_scorer
from sklearn.metrics import accuracy_score
'''
Using PCA: https://www.quora.com/Is-it-worth-trying-PCA-on-your-data-before-feeding-to-SVM
'''
df = pd.DataFrame.from_csv('train.csv')
pca = PCA(n_components=3)
principal_components = pca.fit_transform(df.iloc[:,0:9])
'''
Plot principal components to select number of them to use
print(pca.explained_variance_ratio_)
plt.plot(pca.explained_variance_ratio_)
plt.show()
'''
pca_df = pd.DataFrame(data = principal_components
, columns = ['principal component 1', 'principal component 2', 'principal component 3'])
parameters = [
{'kernel':['poly', 'rbf', 'sigmoid'], 'degree':[1,3], 'gamma':[2**-5, 2^1], 'C': [1,10]},
]
X = pca_df.iloc[:,0:3]
cols = df['Class/ASD']
y = pd.factorize(df['Class/ASD'])[0] + 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2)
svc = SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(confusion_matrix(y_test,y_pred))
print(clf.best_params_)
print(clf.cv_results_)
print(classification_report(y_test,y_pred))
'''
Output:
precision recall f1-score support
1 0.99 0.96 0.97 90
2 0.88 0.97 0.92 30
avg / total 0.96 0.96 0.96 120
{'C': 10, 'degree': 1, 'gamma': 3, 'kernel': 'rbf'}
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment