<jupyter_start><jupyter_text><NAME> - 18BIS0043 Mock Lab FAT, L53+L54 ECE3502 - IoT Domain Analyst Submitted to: Prof. <NAME>  AimIn the given dataset, use decision tree and predict the online shopping website for - F,34,4900 (Reg.No: 18BEC2008-18BIS0057)  Algorithm ExplanationA decision tree is a flowchart-like framework in which each internal node represents a "test" on an attribute, each branch represents the result of the test, and each leaf node represents a class name (decision taken after computing all attributes). Classification laws are described by the pathways from root to leaf.A decision tree and its closely associated effect diagram are used in decision research as a visual and empirical decision support method for calculating the predicted values (or expected utility) of competing alternatives.A decision tree is made up of three distinct forms of nodes:Nodes of decision – usually denoted by squaresNodes of chance – usually denoted by circlesEnd nodes – commonly denoted by trianglesThe decision tree is the most effective and widely used classification and prediction method available. A decision tree is a tree structure resembling a flowchart, in which each internal node represents a test on an attribute, each branch represents the test's result, and each leaf node (terminal node) represents a class name.Decision trees identify instances by sorting them down the tree from the root to a leaf node that corresponds to the instance's classification. As seen in the preceding diagram, an instance is identified by beginning at the root node of the tree, evaluating the attribute defined by this node, and then going down the tree branch corresponding to the value of the attribute. This procedure is then replicated for the new node's subtree.*Source: Wikipedia, GeeksForGeeks*  Code and Result  Import Libraries<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.metrics import accuracy_score, log_loss, confusion_matrix, f1_score 
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.model_selection import StratifiedKFold, GridSearchCV, KFold 
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>allen = pd.read_csv("Dataset.csv")
allen['Online shopping website'] = allen['Online shopping website'].str.upper()
allen.head()<jupyter_output><empty_output><jupyter_text>Label Encoding<jupyter_code>l1 = preprocessing.LabelEncoder()
f1 = l1.fit_transform(allen['Gender']) 
f1 = pd.DataFrame(data=f1, columns=['Gender'])
allen['Gender'] = f1['Gender']
print(list(l1.inverse_transform([0,1])))
l2 = preprocessing.LabelEncoder()
f2 = l2.fit_transform(allen['Online shopping website']) 
f2 = pd.DataFrame(data=f2, columns=['Online shopping website'])
allen['Online shopping website'] = f2['Online shopping website']
print(list(l2.inverse_transform([0,1,2,3,4,5,6])))
allen.head()<jupyter_output><empty_output><jupyter_text>Train-test-split<jupyter_code>x = allen.drop(['Online shopping website','S.No'],axis=1)
y = allen['Online shopping website']
xtrain, xtest, ytrain, ytest = train_test_split(x,y,test_size=0.1,random_state=21)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>model0 = DecisionTreeClassifier()
model0.fit(xtrain,ytrain)
p0 = model0.predict(xtest)<jupyter_output><empty_output><jupyter_text>Prediction (18BIS0043)<jupyter_code>allen2 = pd.DataFrame(columns=['Gender','Age','Amount spend on shopping','Online shopping website'])
init = {"Gender": 0, 
        "Age": 34,
        "Amount spend on shopping": 4900,
}
allen2 = allen2.append(init, ignore_index=True)
pred = model0.predict(allen2.drop(['Online shopping website'],axis=1))
allen2['Online shopping website']=pred
allen2.head()
print(list(l2.inverse_transform([allen2['Online shopping website']])))<jupyter_output>['NYKAA']<jupyter_text>InferencesUsing the given dataset, we have successfully classified the users based on their shopping patterns to predict the Online Shopping websites they use. The classification model is based on the ML Algorithm - **Decision Tree** with accuracies upto 36%. Availability of training data, and its quality is the primary reason that determines the accuracy. Lower dataset quality indirectly implies a lower accuracy for the model.We have also taken the example of a female user, 34 years old with a spending amount of Rs. 4900, and predicted the online shopping website as "NYKAA" based on the learning from the dataset.<jupyter_code>print(classification_report(ytest.values.ravel(),p0,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.17      0.20      0.18         5
           1       0.17      0.25      0.20         4
           2       0.40      0.40      0.40         5
           3       0.80      0.50      0.62         8
           4       0.60      0.43      0.50         7
           5       0.29      0.25      0.27         8
           6       0.20      0.50      0.29         2

    accuracy                           0.36        39
   macro avg       0.37      0.36      0.35        39
weighted avg       0.43      0.36      0.38        39<jupyter_text>VivaWhich ML algorithm is the most effective for this application given the same dataset. Justify your choice of ML algorithm.  Random Forest<jupyter_code>model1 = RandomForestClassifier()
model1.fit(xtrain,ytrain)
p1 = model1.predict(xtest)
print(confusion_matrix(ytest,p1))
print(classification_report(ytest.values.ravel(),p1,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.29      0.40      0.33         5
           1       0.14      0.25      0.18         4
           2       0.50      0.20      0.29         5
           3       0.60      0.38      0.46         8
           4       0.50      0.29      0.36         7
           5       0.33      0.12      0.18         8
           6       0.09      0.50      0.15         2

    accuracy                           0.28        39
   macro avg       0.35      0.31      0.28        39
weighted avg       0.40      0.28      0.30        39<jupyter_text>Decision Tree<jupyter_code>model2 = DecisionTreeClassifier()
model2.fit(xtrain,ytrain)
p2 = model2.predict(xtest)
print(confusion_matrix(ytest,p2))
print(classification_report(ytest.values.ravel(),p2,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.17      0.20      0.18         5
           1       0.17      0.25      0.20         4
           2       0.40      0.40      0.40         5
           3       0.56      0.62      0.59         8
           4       0.75      0.43      0.55         7
           5       0.40      0.25      0.31         8
           6       0.25      0.50      0.33         2

    accuracy                           0.38        39
   macro avg       0.38      0.38      0.37        39
weighted avg       0.43      0.38      0.39        39<jupyter_text>Naive Bayes<jupyter_code>model3 = GaussianNB()
model3.fit(xtrain,ytrain)
p3 = model3.predict(xtest)
print(confusion_matrix(ytest,p3))
print(classification_report(ytest.values.ravel(),p3,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.00      0.00      0.00         5
           1       0.08      0.25      0.12         4
           2       0.67      0.80      0.73         5
           3       0.57      0.50      0.53         8
           4       0.25      0.14      0.18         7
           5       1.00      0.25      0.40         8
           6       0.00      0.00      0.00         2

    accuracy                           0.31        39
   macro avg       0.37      0.28      0.28        39
weighted avg       0.46      0.31      0.33        39<jupyter_text>K-Nearest Neighbour<jupyter_code>model4 = KNeighborsClassifier()
model4.fit(xtrain,ytrain)
p4 = model4.predict(xtest)
print(confusion_matrix(ytest,p4))
print(classification_report(ytest.values.ravel(),p4,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.00      0.00      0.00         5
           1       0.11      0.25      0.15         4
           2       0.17      0.20      0.18         5
           3       0.20      0.12      0.15         8
           4       0.25      0.14      0.18         7
           5       1.00      0.12      0.22         8
           6       0.00      0.00      0.00         2

    accuracy                           0.13        39
   macro avg       0.25      0.12      0.13        39
weighted avg       0.32      0.13      0.15        39<jupyter_text>Logistic Regression<jupyter_code>model5 = LogisticRegression()
model5.fit(xtrain,ytrain)
p5 = model5.predict(xtest)
print(confusion_matrix(ytest,p5))
print(classification_report(ytest.values.ravel(),p5,target_names=['0','1','2','3','4','5','6']))<jupyter_output>precision    recall  f1-score   support

           0       0.12      0.20      0.15         5
           1       0.00      0.00      0.00         4
           2       0.00      0.00      0.00         5
           3       0.25      0.62      0.36         8
           4       0.00      0.00      0.00         7
           5       0.00      0.00      0.00         8
           6       0.00      0.00      0.00         2

    accuracy                           0.15        39
   macro avg       0.05      0.12      0.07        39
weighted avg       0.07      0.15      0.09        39<jupyter_text>K-Fold Analysis<jupyter_code>kfold = StratifiedKFold(n_splits=6, shuffle=True, random_state=21)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>acc = np.mean(cross_val_score(model1, xtrain, ytrain, scoring='accuracy', cv=kfold))
allen11 = pd.DataFrame({'Model':'Random Forest',
                      'Accuracy':[acc]})<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>acc = np.mean(cross_val_score(model2, xtrain, ytrain, scoring='accuracy', cv=kfold))
allen12 = pd.DataFrame({'Model':'Decision Tree',
                      'Accuracy':[acc]})<jupyter_output><empty_output><jupyter_text>Naive Bayes<jupyter_code>acc = np.mean(cross_val_score(model3, xtrain, ytrain, scoring='accuracy', cv=kfold))
allen13 = pd.DataFrame({'Model':'Naive Bayes',
                      'Accuracy':[acc]})<jupyter_output><empty_output><jupyter_text>K-Nearest Neighbour<jupyter_code>acc = np.mean(cross_val_score(model4, xtrain, ytrain, scoring='accuracy', cv=kfold))
allen14 = pd.DataFrame({'Model':'K-Nearest Neighbour',
                      'Accuracy':[acc]})<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>acc = np.mean(cross_val_score(model5, xtrain, ytrain, scoring='accuracy', cv=kfold))
allen15 = pd.DataFrame({'Model':'Logistic Regression',
                      'Accuracy':[acc]})<jupyter_output><empty_output><jupyter_text>Concat<jupyter_code>al = pd.concat([allen11,allen12,allen13,allen14,allen15],axis=0).reset_index()
al = al.drop('index',axis=1)
al<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.096542783059637, 'fraction_numerical': 0.07165082108902333, 'mean_word_length': 2.3548854740504495, 'pattern_counts': {'":': 3, '<': 72, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '626938', 'license': 'MIT', 'n_tokens_mistral': 3937, 'n_tokens_neox': 3631, 'n_words': 956, 'path': 'A2/A2.ipynb', 'repo_name': 'abphilip-vit/College8', 'size': '11587'}