<jupyter_start><jupyter_text>**Support vector machine**Thanks to the sklearn website for examples on the functions used in this code.Thanks to https://stackoverflow.com/questions/51194627/python-naive-bayes-with-cross-validation-using-gaussiannb-classifier for helping with cross-validation.<jupyter_code>#imports
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV, KFold
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
#Loading the linear data
train = pd.read_csv('/content/drive/My Drive/Colab Notebooks/mpr/linearTrain.csv')
test = pd.read_csv('/content/drive/My Drive/Colab Notebooks/mpr/linearTest.csv')
X_train = train.iloc[:, 1:14]
y_train = train.iloc[:,14]
X_test = test.iloc[:, 1:14]
y_test = test.iloc[:, 14]<jupyter_output><empty_output><jupyter_text>Implementing SVM classifier on linear dataset with grid search and cross validation<jupyter_code>kf = KFold(n_splits=9, random_state=None, shuffle=False)
params = {'kernel': ['linear', 'poly', 'rbf', 'sigmoid'], 'decision_function_shape':['ovo', 'ovr']}
clf = SVC()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#grid search results
gs.cv_results_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the classifier with best parameters
bestclf = SVC(decision_function_shape= 'ovo', kernel= 'rbf')
bestclf.fit(X_train, y_train)
tr_p = bestclf.predict(X_train)
print(accuracy_score(y_train, tr_p))
confusion_matrix(y_train, tr_p)
#Running the model on test set
predictions = bestclf.predict(X_test)
print(accuracy_score(y_test, predictions))
confusion_matrix(y_test, predictions)<jupyter_output>0.9133608227878098<jupyter_text>Implementing the SVM classifier on normalized linear dataset<jupyter_code>#Normalizing the data
from sklearn import preprocessing
scalar = preprocessing.StandardScaler()
scalar.fit(X_train)
X_train = scalar.transform(X_train)
X_test = scalar.transform(X_test)
kf = KFold(n_splits=9, random_state=None, shuffle=False)
params = {'kernel': ['linear', 'poly', 'rbf', 'sigmoid'], 'decision_function_shape':['ovo', 'ovr']}
clf = SVC()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#Visualizing the results of grid search
gs.cv_results_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the model with the best parameters
bestclf = SVC(decision_function_shape= 'ovo', kernel= 'rbf')
bestclf.fit(X_train, y_train)
tr_p = bestclf.predict(X_train)
print(accuracy_score(y_train, tr_p))
confusion_matrix(y_train, tr_p)
#Running the model on the test set
predictions = bestclf.predict(X_test)
print(accuracy_score(y_test, predictions))
confusion_matrix(y_test, predictions)<jupyter_output>0.9434096402673112<jupyter_text>Implelemting the SVM classifier on the normalized expanded dataset<jupyter_code>import pandas as pd
import numpy as np
from sklearn.feature_selection import RFE, RFECV
from sklearn.linear_model import SGDClassifier
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV, KFold, train_test_split
#Loading the expanded dataset
train = pd.read_csv('/content/drive/My Drive/Colab Notebooks/mpr/linearTrainexpanded.csv')
test = pd.read_csv('/content/drive/My Drive/Colab Notebooks/mpr/linearTestexpanded.csv')
X_train = train.iloc[:, 1:60]
y_train = train.iloc[:,60]
X_test = test.iloc[:, 1:60]
y_test = test.iloc[:, 60]
#Normalizing the data
from sklearn import preprocessing
scalar = preprocessing.StandardScaler()
scalar.fit(X_train)
X_train = scalar.transform(X_train)
X_test = scalar.transform(X_test)
X_train = pd.DataFrame(X_train)
X_test = pd.DataFrame(X_test)
kf = KFold(n_splits=9, random_state=None, shuffle=False)
params = {'kernel': ['linear', 'poly', 'rbf', 'sigmoid'], 'decision_function_shape':['ovo', 'ovr']}
clf = SVC()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#Visualizing the results of grid search
gs.cv_results_
#Best estimator
gs.best_estimator_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the model with the best parameters
bestclf = SVC(decision_function_shape= 'ovo', kernel= 'rbf')
bestclf.fit(X_train, y_train)
tr_p = bestclf.predict(X_train)
print(accuracy_score(y_train, tr_p))
confusion_matrix(y_train, tr_p)
#Running the model on the test set
predictions = bestclf.predict(X_test)
print(accuracy_score(y_test, predictions))
confusion_matrix(y_test, predictions)<jupyter_output>0.9062040855016825
{'fraction_non_alphanumeric': 0.11222707423580786, 'fraction_numerical': 0.017903930131004366, 'mean_word_length': 7.843629343629344, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '269485', 'license': 'MIT', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1623, 'n_words': 374, 'path': 'SVM.ipynb', 'repo_name': 'AnjanaNiranjan/HandPostureRecognition', 'size': '4580'}