<jupyter_start><jupyter_text>Human Activity Recognition Using Smartphones Data Setdataset :- [link](http://archive.ics.uci.edu/ml/datasets/human+activity+recognition+using+smartphones)<jupyter_code>from sklearn.model_selection import KFold, train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, cross_val_score, ShuffleSplit
from sklearn.svm import SVC 
from sklearn.metrics import confusion_matrix, mean_squared_error
from sklearn.model_selection import train_test_split 
from numpy import loadtxt
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
import xgboost as xgb
import matplotlib.pyplot as plt
import torch
import sklearn
import random
import pickle<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>x_train = pd.read_csv("X_train.txt" ,sep=" ",header=None)
y_train = pd.read_csv("Y_train.txt" ,sep=" ",header=None)
x_train.describe()
y_train[0].value_counts().plot.bar()
plot = x_train.iloc[:, :80].plot(kind='box', figsize=[16,5])
plt.show(plot)
# plot correlation matrix
correlations = x_train.corr()
fig = plt.figure(figsize=[10,10])
ax = fig.add_subplot(111)
cax = ax.matshow(correlations, vmin=-1, vmax=1)
fig.colorbar(cax)
plt.show()
X_train, X_test, Y_train, Y_test = train_test_split(x_train.values, y_train.values, test_size=0.10, random_state=int(random.random()*100))
Y_train = (Y_train - 1).flatten()
Y_test = (Y_test - 1).flatten()
print(X_train.shape)
print(Y_train.shape)<jupyter_output>(8662, 561)
(8662,)<jupyter_text>Neural Network<jupyter_code>X_trainc = torch.tensor(X_train,dtype=torch.float).cuda()
Y_trainc = torch.tensor(Y_train,dtype=torch.long).cuda() 
X_testc  = torch.tensor(X_test,dtype=torch.float).cuda()
Y_testc  = torch.tensor(Y_test,dtype=torch.long).cuda() 
class Net(torch.nn.Module):
    def __init__(self, D_in, D_out):
        super(Net, self).__init__()
        self.linear1 = torch.nn.Linear(D_in, 1000)
        self.linear2 = torch.nn.Linear(1000, 2000)
        self.linear3 = torch.nn.Linear(2000, D_out)
        self.drop1 = torch.nn.Dropout(p=0.45, inplace=False)
        self.drop2 = torch.nn.Dropout(p=0.45, inplace=False)
        self.relu = torch.nn.ReLU()
    def forward(self, x):
        x = self.relu(self.linear1(x))
        x = self.drop1(x)
        x = self.relu(self.linear2(x))
        x = self.drop2(x)
        y_pred = self.linear3(x)
        return y_pred
model = Net(561,6).cuda()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
step = torch.optim.lr_scheduler.StepLR(optimizer,100, gamma = 0.5, last_epoch=-1)
def acc(model): #evaluates the models and prints accuracy
    model.eval()
    y = torch.argmax(model(X_testc),dim = 1)
    y = y.cpu().detach().numpy()
    x = np.equal(y, Y_testc.cpu().detach().numpy())
    sum = np.sum(x)
    print("Test accuracy is ",end=" ")
    print( (100 * sum) / y.shape[0], end= " ")
    y = torch.argmax(model(X_trainc),dim = 1)
    y = y.cpu().detach().numpy()
    x = np.equal(y, Y_trainc.cpu().detach().numpy())
    sum = np.sum(x)
    print("Train accuracy is ",end=" ")
    print( (100 * sum) / y.shape[0])
model.train()
for i in range(1501):
    if i < 510: step.step()
    model.train()
    optimizer.zero_grad()
    y_pred = model(X_trainc).cuda()
    loss = criterion(y_pred, Y_trainc)
    loss.backward()
    optimizer.step()
    if i%300 == 0: acc(model)<jupyter_output>Test accuracy is  19.833852544132917 Train accuracy is  19.41814823366428
Test accuracy is  95.74247144340602 Train accuracy is  99.58439159547449
Test accuracy is  95.84631360332295 Train accuracy is  99.88455322096513
Test accuracy is  95.95015576323988 Train accuracy is  99.97691064419303
Test accuracy is  95.95015576323988 Train accuracy is  100.0
Test accuracy is  95.95015576323988 Train accuracy is  100.0<jupyter_text>Our neural network achived around **96%** test accuracy.  Random Forest Classifier<jupyter_code>rf = RandomForestClassifier(n_estimators = 500, n_jobs = 4, random_state = 112)

rf.fit(X_train, Y_train);

predictions = rf.predict(X_test)

errors = np.count_nonzero(predictions - Y_test)

print('Mean Absolute accuracy:', ((Y_test.shape[0] - errors)/Y_test.shape[0] ) * 100 , '%')<jupyter_output>Mean Absolute accuracy: 95.22326064382139 %<jupyter_text>Our Random Forest Classifier  achived around **95.22%** test accuracy.  SVM<jupyter_code>clf = SVC(decision_function_shape='ovo', gamma='scale', kernel='rbf', max_iter=2000 )

clf.fit(X_train, Y_train) 

y_pred = clf.predict(X_test)

predictions = [round(value) for value in y_pred]

accuracy = accuracy_score(Y_test, predictions)

print("Accuracy: %.2f%%" % (accuracy * 100.0))<jupyter_output>Accuracy: 92.63%<jupyter_text>Simple SVM got around **92.63%** accuracy  XGBoost **GRID SEARCH WITH CROSS VALIDATION**<jupyter_code>print("Grid Search for parameters")
xgb_model = xgb.XGBRegressor()
clf = GridSearchCV(xgb_model, {'max_depth': [4,5,6,7],'n_estimators': [50,100,150,200,250]}, verbose=1, n_jobs = 4,cv=cv)
clf.fit(X_train, Y_train)
print(clf.best_score_)
print(clf.best_params_)<jupyter_output>Grid Search for parameters
Fitting 2 folds for each of 20 candidates, totalling 40 fits<jupyter_text>**K Fold**<jupyter_code>kf = KFold(n_splits=3, shuffle=True, random_state=42)
for train_index, test_index in kf.split(X_train):
    xgb_model = xgb.XGBClassifier(max_depth = 7, n_estimators = 200, n_classes_ = 6, nthread=8).fit(X_train[train_index], Y_train[train_index]) 
    predictions = xgb_model.predict(X_train[test_index])
    actuals = Y_train[test_index]
    print("result on test part of KFolded dataset")
    print(confusion_matrix(actuals, predictions))
    predictions = [round(value) for value in predictions]
    accuracy = accuracy_score(actuals, predictions)
    print("Accuracy test: %.2f%%" % (accuracy * 100.0))
    
    print("result on full training dataset")
    predictions = xgb_model.predict(X_train)
    actuals = Y_train
    print(confusion_matrix(actuals, predictions))
    predictions = [round(value) for value in predictions]
    accuracy = accuracy_score(actuals, predictions)
    print("Accuracy full train: %.2f%%" % (accuracy * 100.0))
    
    print("result on full test dataset")
    predictions = xgb_model.predict(X_test)
    actuals = Y_test
    print(confusion_matrix(actuals, predictions))
    predictions = [round(value) for value in predictions]
    accuracy = accuracy_score(actuals, predictions)
    print("Accuracy full test: %.2f%%" % (accuracy * 100.0))<jupyter_output>result on test part of KFolded dataset
[[488   7   8   0   0   0]
 [  5 428   2   0   0   5]
 [  6  13 389   1   1   1]
 [  0   2   0 495  25   5]
 [  0   0   2  22 493   0]
 [  0   2   0   1   0 487]]
Accuracy test: 96.26%
result on full training dataset
[[1481    7    8    0    0    0]
 [   5 1323    2    0    0    5]
 [   6   13 1209    1    1    1]
 [   0    2    0 1543   25    5]
 [   0    0    2   22 1557    0]
 [   0    2    0    1    0 1441]]
Accuracy full train: 98.75%
result on full test dataset
[[156   5   4   0   0   0]
 [  3 131   2   0   0   2]
 [  4   8 137   0   0   0]
 [  0   0   0 167  10   1]
 [  0   0   1   7 160   0]
 [  0   2   0   0   0 163]]
Accuracy full test: 94.91%
result on test part of KFolded dataset
[[484   5   8   0   1   0]
 [  9 422   7   0   0   3]
 [  9   9 393   0   2   1]
 [  0   2   0 480  28   3]
 [  2   0   1  18 510   0]
 [  0   2   0   0   0 488]]
Accuracy test: 96.19%
result on full training dataset
[[1482    5    8    0    1    0]
 [   9 131[...]<jupyter_text>**Final model**<jupyter_code>xgb_model = xgb.XGBClassifier(max_depth = 7, n_estimators = 250, n_classes_ = 6,nthreads= 8).fit(X_train,Y_train)
predictions = xgb_model.predict(X_train)
actuals = Y_train
print(confusion_matrix(actuals, predictions))
predictions = [round(value) for value in predictions]
accuracy = accuracy_score(actuals, predictions)
print("Accuracy train: %.2f%%" % (accuracy * 100.0))
predictions = xgb_model.predict(X_test)
actuals = Y_test
print(confusion_matrix(actuals, predictions))
predictions = [round(value) for value in predictions]
accuracy = accuracy_score(actuals, predictions)
print("Accuracy test: %.2f%%" % (accuracy * 100.0))
xgb.plot_importance(xgb_model)
plt.figure(figsize = (160,100))
plt.show()
plo = xgb.to_graphviz(xgb_model)
plo.render('model_graph.gv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12636449928808732, 'fraction_numerical': 0.07427622211675368, 'mean_word_length': 3.996443390634262, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, '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': '615604', 'license': 'MIT', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3175, 'n_words': 746, 'path': 'Human Activity Recognition/notebook.ipynb', 'repo_name': 'dhananjayraut/ML_projects', 'size': '8428'}