<jupyter_start><jupyter_text>Importing all the liberaries required<jupyter_code># libraries
import pandas as pd
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import scale
# importing dataset
hand_digit_recog = pd.read_csv("train.csv")
hand_digit_recog.head()
hand_digit_recog.info()
hand_digit_recog.columns
#checking the number of columns in the given dataset.....
len(hand_digit_recog.columns)<jupyter_output><empty_output><jupyter_text>since there are a lot of columns it looks like 785 to be exact; now we can confirm that the columns pixel values right from 0 to 783 and there columns like serial number and pixels.<jupyter_code>hand_digit_recog.describe()
#lets try to make the reason with the dataset; lets look at what actaully is there; lets try to get the first row and try to visualise it
zero = hand_digit_recog.iloc[1,1:]
zero.shape
#reshapig and getting the image printed with all the pixels into a grid of 28X28 pixels.... as shown below
zero = zero.values.reshape(28, 28)
plt.imshow(zero, cmap='gray')

hand_digit_recog.shape<jupyter_output><empty_output><jupyter_text>could have portriated the heatmap and all means and all, but since the dataset is qiute heavy looking at the shape of it, its scary huge, so skpipping that and continuing with other summarising and all<jupyter_code>#looking up for the categorical labels in the data set
cat_count = hand_digit_recog.label.astype('category').value_counts()
# looking at the percentage of the categorical data
(round(cat_count/len(hand_digit_recog.index), 4))*100
#checking the dataset for any missing values...
hand_digit_recog.isnull().sum()<jupyter_output><empty_output><jupyter_text>looks like there are no missing values,,,,,, thats super:)  Data preparation stepSInce there is quite a lot of data as mentioned earleir looking at the shape of it, we are supposed to make ready the data, else it will take days to build a model using this...<jupyter_code># TEST Train creation by splitting it using following 
X = hand_digit_recog.iloc[:, 1:] #skipping the 1st entry

Y = hand_digit_recog.iloc[:, 0]


#Scaling of features
from sklearn.preprocessing import scale

X = scale(X)


#Test Train split using the ablove statements; splitting it with 20% train data; since the data is huge we are using only 20% of it.
x_train, x_test, y_train, y_test = train_test_split(X, Y, train_size=0.20, random_state=100)
x_train.shape<jupyter_output><empty_output><jupyter_text>Model Building using the above prepared dataFirst building the Linear model using the hyperparameters  Linear Model -- next<jupyter_code>from sklearn import metrics
from sklearn import svm

#linear kernel to be used 
Lin_SVM = svm.SVC(kernel='linear')

Lin_SVM.fit(x_train, y_train)
# fitting it too...
# as fit, we are going for prediction staright forward
y_prediction = Lin_SVM.predict(x_test)
y_prediction[:10]
# here is the measure to be calculated -- i.e. the accuracy to tally amoung the models
metrics.accuracy_score(y_true=y_test, y_pred=y_prediction)<jupyter_output><empty_output><jupyter_text>Non-Linear ModelGoing next with the non linear SVM model using 'rbf' kernel<jupyter_code>#modeling 
NonLin_SVM = svm.SVC(kernel='rbf')
NonLin_SVM.fit(x_train, y_train)
#fitting done right............
#testing the model, over the test set;  predcting further
y_prediction = NonLin_SVM.predict(x_test)
# accuracy for the nonlinear model..............
metrics.accuracy_score(y_true=y_test, y_pred=y_prediction)
#trying to get rid of garbage objects using this library to make things go faster.... as it takes a lot of time to process and i have a low config machine:()
import gc
gc.collect()<jupyter_output><empty_output><jupyter_text>Grid Search tuning............... is next<jupyter_code># creating a KFold object with 5 splits 
#gc.collect()
folds = KFold(n_splits = 5, shuffle = True, random_state = 101)
n_jobs=-1


hyper_params = {'C':[1], 
             'gamma': [0.001]}


#hyper_params = {'C':[1, 10, 100], 
#             'gamma': [0.01, 0.001,0.0001]}

# specifying the model
model = SVC(kernel="rbf")

# set up the GridSearchCV()
model_cv = GridSearchCV(estimator = model, param_grid = hyper_params, scoring= 'accuracy', cv = folds, 
                        verbose = 1,return_train_score=True)      

# fit the model
model_cv.fit(x_train, y_train) 
# results being shown below.......
cv_results = pd.DataFrame(model_cv.cv_results_)
cv_results<jupyter_output><empty_output><jupyter_text>Finally going for the final model<jupyter_code>#Choosing the below mentioned values as the optimal ones,.....
C_value = 1
gamma_value = 0.001

#modeling it to the best ones i.e. the non linear one from above observation as they have the best accurancey....
final_SVM = svm.SVC(kernel='rbf', C=C_value, gamma=gamma_value)

#fitting model
final_SVM.fit(x_train, y_train)
# read the dataset
final_testing_digit_recog = pd.read_csv("test.csv")
final_testing_digit_recog.describe()
#well predeicting the final set..........
y_prediction = final_SVM.predict(final_testing_digit_recog)
#final shape of the predcited set
y_prediction.shape
final_result = pd.DataFrame({'Label': y_prediction})
final_result.head()
#looks like it works...................<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09540954095409541, 'fraction_numerical': 0.012781278127812781, 'mean_word_length': 5.092105263157895, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '253098', 'license': 'MIT', 'n_tokens_mistral': 1859, 'n_tokens_neox': 1756, 'n_words': 603, 'path': 'SVM+Assignment.ipynb', 'repo_name': 'saurabdongre/Support-Vector-Machine', 'size': '5555'}