-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0ed1ae
commit a088209
Showing
2 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
project_17_compare_classification_algorithms/compare_classification_algos.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Dec 19 17:30:09 2018 | ||
@author: omairaasim | ||
""" | ||
|
||
# Step 1 - Load Data | ||
import pandas as pd | ||
dataset = pd.read_csv("iphone_purchase_records.csv") | ||
X = dataset.iloc[:,:-1].values | ||
y = dataset.iloc[:, 3].values | ||
|
||
# Step 2 - Convert Gender to number | ||
from sklearn.preprocessing import LabelEncoder | ||
labelEncoder_gender = LabelEncoder() | ||
X[:,0] = labelEncoder_gender.fit_transform(X[:,0]) | ||
|
||
|
||
# Step 3 - Feature Scaling | ||
from sklearn.preprocessing import StandardScaler | ||
sc = StandardScaler() | ||
X = sc.fit_transform(X) | ||
|
||
# Step 4 - Compare Classification Algorithms | ||
from sklearn.model_selection import KFold | ||
from sklearn.model_selection import cross_val_score | ||
from sklearn.linear_model import LogisticRegression | ||
from sklearn.tree import DecisionTreeClassifier | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.naive_bayes import GaussianNB | ||
from sklearn.svm import SVC | ||
|
||
classification_models = [] | ||
classification_models.append(('Logistic Regression', LogisticRegression(solver="liblinear"))) | ||
classification_models.append(('K Nearest Neighbor', KNeighborsClassifier(n_neighbors=5, metric="minkowski",p=2))) | ||
classification_models.append(('Kernel SVM', SVC(kernel = 'rbf',gamma='scale'))) | ||
classification_models.append(('Naive Bayes', GaussianNB())) | ||
classification_models.append(('Decision Tree', DecisionTreeClassifier(criterion = "entropy"))) | ||
classification_models.append(('Random Forest', RandomForestClassifier(n_estimators=100, criterion="entropy"))) | ||
|
||
for name, model in classification_models: | ||
kfold = KFold(n_splits=10, random_state=7) | ||
result = cross_val_score(model, X, y, cv=kfold, scoring='accuracy') | ||
print("%s: Mean Accuracy = %.2f%% - SD Accuracy = %.2f%%" % (name, result.mean()*100, result.std()*100)) |
Oops, something went wrong.