forked from jasmeet0915/Interactive-Harry-Potter-Wand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Training_SVM.py
46 lines (39 loc) · 1.55 KB
/
Training_SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import joblib
print("....Reading DataSet and Creating Pandas DataFrame....")
alphabet_data = pd.read_csv("A_ZHandwrittenData.csv")
print("...DataFrame Created...")
print("...Slicing and creating initial training and testing set...")
# Dataset of letter A containing features
X_Train_A = alphabet_data.iloc[:13869, 1:]
# Dataset of letter A containing labels
Y_Train_A = alphabet_data.iloc[:13869, 0]
# Dataset of letter C containing features
X_Train_C = alphabet_data.iloc[22537:45946, 1:]
# Dataset of letter C containing labels
Y_Train_C = alphabet_data.iloc[22537:45946, 0]
# Joining the Datasets of both letters
X_Train = pd.concat([X_Train_A, X_Train_C], ignore_index=True)
Y_Train = pd.concat([Y_Train_A, Y_Train_C], ignore_index=True)
print("...X_Train and Y_Train created...")
train_features, test_features, train_labels, test_labels = train_test_split(X_Train, Y_Train, test_size=0.25, random_state=0)
print(type(test_features))
# SVM classifier created
clf = SVC(kernel='linear')
print("")
print("...Training the Model...")
clf.fit(train_features, train_labels)
print("...Model Trained...")
labels_predicted = clf.predict(test_features)
print(test_labels)
print(labels_predicted)
accuracy = accuracy_score(test_labels, labels_predicted)
print("")
print("Accuracy of the model is: ")
print(accuracy)
print("...Saving the trained model...")
# joblib.dump(clf, "alphabet_classifier.joblib", compress=3)
print("...Model Saved...")