<jupyter_start><jupyter_text>**Perceptron**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.linear_model import Perceptron
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
#Loading the dataset
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 the Perceptron Learning classifier on the data by selecting the best parameters through grid search with cross validation<jupyter_code>kf = KFold(n_splits=9, random_state=None, shuffle=False)
params = {'penalty':['l1', 'l2', 'elasticnet'], 'alpha': [0.1, 0.05, 0.001], 'eta0':[0.5, 1, 5]}
clf = Perceptron()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#Results of grid search with cross validation
gs.cv_results_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the model with best parameters
bestclf = Perceptron(alpha= 0.1, eta0= 0.5, penalty= 'l1')
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.8837859614199726<jupyter_text>Implementing Perceptron Learning classifier on Normalized data<jupyter_code>#Normalizing the linear dataset
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 = {'penalty':['l1', 'l2', 'elasticnet'], 'alpha': [0.1, 0.05, 0.001], 'eta0':[0.5, 1, 5]}
clf = Perceptron()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#Results of grid search with cross validation
gs.cv_results_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the classifier with best parameters
bestclf = Perceptron(alpha= 0.001, eta0= 5, penalty= 'l1')
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.8935020617090857<jupyter_text>Implementing the Perceptron Learning classifier on the expanded dataset<jupyter_code>from sklearn.feature_selection import RFE, RFECV
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV, KFold, train_test_split
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 = {'penalty':['l1', 'l2', 'elasticnet'], 'alpha': [0.1, 0.05, 0.001], 'eta0':[0.5, 1, 5]}
clf = Perceptron()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train, y_train)
#Results of grid search with cross validation
gs.cv_results_
#Best estimator
gs.best_estimator_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the model with best parameters
bestclf = Perceptron(alpha= 0.001, eta0= 0.5, penalty= 'l1')
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.8715104981278734<jupyter_text>Implementing the Perceptron Learning algorithm on the Optimal features selected from the expanded dataset<jupyter_code>kf = KFold(n_splits=9, random_state=None, shuffle=False)
model = Perceptron()
rfecv = RFECV(estimator=model, cv=kf,scoring='accuracy')
rfecv.fit(X_train, y_train)

print("Optimal number of features : %d" % rfecv.n_features_)
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()
selected = rfecv.support_
num = 0
todelete = []
for i in selected:
  if i ==False:
    todelete.append(num)
    num += 1
X_train_selected = X_train.drop(X_train.columns[todelete], axis=1)
X_test_selected = X_test.drop(X_test.columns[todelete], axis=1)
kf = KFold(n_splits=9, random_state=None, shuffle=False)
params = {'penalty':['l1', 'l2', 'elasticnet'], 'alpha': [0.1, 0.05, 0.001], 'eta0':[0.5, 1, 5]}
clf = Perceptron()
gs = GridSearchCV(clf, cv=kf, param_grid=params, return_train_score=True)

gs.fit(X_train_selected, y_train)
#Results of grid search with cross validtion
gs.cv_results_
#Best estimator
gs.best_estimator_
#Best parameters
gs.best_params_
#Best score
gs.best_score_
#Training the model with best parameters
bestclf = Perceptron(alpha= 0.001, eta0= 0.5, penalty= 'l1')
bestclf.fit(X_train_selected, y_train)
tr_p = bestclf.predict(X_train_selected)
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_selected)
print(accuracy_score(y_test, predictions))
confusion_matrix(y_test, predictions)<jupyter_output>0.7256742025688421
{'fraction_non_alphanumeric': 0.11937440153207789, 'fraction_numerical': 0.031918289179699966, 'mean_word_length': 7.412080536912752, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '527180', 'license': 'MIT', 'n_tokens_mistral': 2456, 'n_tokens_neox': 2305, 'n_words': 523, 'path': 'PerceptronLearningClassifier.ipynb', 'repo_name': 'AnjanaNiranjan/HandPostureRecognition', 'size': '6266'}