-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First attempt on vehicle dataset with a random forest classifier #13
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ae1c5f
First attempt on vehicle data with a random forest calssifier
Sidrah-Madiha f320c86
minor changes
Sidrah-Madiha 9e3aeba
Comparative model evaluation for vehicle dataset
Sidrah-Madiha a7acad0
first attempt for implementing task 7
Sidrah-Madiha 4d94959
fixes #8
Sidrah-Madiha c253921
fixes #4, attempt 1
Sidrah-Madiha 2a54a7d
implemeneted all change requests
Sidrah-Madiha 7121a90
formatted code for all helper files
Sidrah-Madiha 6ca9c1d
minor fix
Sidrah-Madiha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,81 @@ | ||
import pandas as pd | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn import metrics | ||
from math import sqrt | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import seaborn as sn | ||
from sklearn.metrics import confusion_matrix, classification_report | ||
|
||
|
||
|
||
def data_stats(dataset): | ||
''' Shows some basic stats of the dataset''' | ||
print("=========== SOME STATS of Dataset ===========") | ||
print('Shape of the dataset: ' + str(dataset.shape) + "\n") | ||
print('List of attribute columns' , list(dataset.columns)) | ||
print("\n") | ||
list_cat = dataset.Class.unique() | ||
print('List of Categories ' , list_cat , "\n" ) | ||
|
||
|
||
def tokenize_target_column(dataset): | ||
''' tokenize the Class column values to numeric data''' | ||
factor = pd.factorize(dataset['Class']) | ||
dataset.Class = factor[0] | ||
definitions = factor[1] | ||
print("Updated tokenize 'Class' column - first 5 values") | ||
print(dataset.Class.head()) | ||
print("Distinct Tokens used for converting Class column to integers") | ||
print(definitions) | ||
return definitions | ||
def train_data_test_data_split(dataset): | ||
X = dataset.iloc[:, 0:-1].values | ||
y = dataset.iloc[:,-1].values | ||
# print(X[0]) | ||
# print(y[0]) | ||
# print(X.shape) | ||
# print(y.shape) | ||
# print('the data attributes columns') | ||
# print(X[:5,:]) | ||
# print('The target variable: ') | ||
# print(y[:5]) | ||
# Split dataset into training set and test set | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2 ,random_state = 21) | ||
return X_train, X_test, y_train, y_test | ||
|
||
def train(X_train, y_train): | ||
''' training model on train data''' | ||
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 42) | ||
classifier.fit(X_train, y_train) | ||
return classifier | ||
|
||
def test(classifier, X_test): | ||
''' testing model on test data''' | ||
y_pred=classifier.predict(X_test) | ||
return y_pred | ||
|
||
def untokenizing_testdata_prediction(y_test, y_pred, definitions): | ||
'''Converting numeric target and predict values back to original labels''' | ||
reversefactor = dict(zip(range(4),definitions)) | ||
y_test = np.vectorize(reversefactor.get)(y_test) | ||
y_pred = np.vectorize(reversefactor.get)(y_pred) | ||
return y_test, y_pred | ||
|
||
|
||
def create_confusion_matrix_class_report(y_test, y_pred): | ||
''' Creates Cinfusion Matrix and summary of evaluation metric ''' | ||
|
||
labels = ["van" , "saab" ,"bus" , "opel"] | ||
cm = confusion_matrix(y_test, y_pred, labels=labels) | ||
df_cm = pd.DataFrame(cm, index=labels, columns=labels) | ||
|
||
sn.heatmap(df_cm, annot=True, fmt='d') | ||
plt.xlabel('Real Vehicle Category') | ||
plt.ylabel('Predicted Vehicle Category') | ||
print("============== Summary of all evaluation metics ===============") | ||
print(classification_report(y_test,y_pred)) | ||
print ("====================== Confusion Matrix=====================") | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
0.2
test proportion does not match the comment in the notebook. In general, how did decide on this test set size? I would be good to include a comment about this in the notebook.