<jupyter_start><jupyter_text>F21DL CW(P1): DATA EXPLORATION AND VISUALIZATION  [INCOME DATA SET](https://www.kaggle.com/mastmustu/income?select=test.csv)  SETUP<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import sklearn<jupyter_output><empty_output><jupyter_text>IMPORTING DATA<jupyter_code>income = pd.read_csv("Income Dataset.csv")<jupyter_output><empty_output><jupyter_text>DESCRIBING DATA  This is a dataset income level of workers of different age/occupation etc.<jupyter_code>income
income.dtypes
income.describe()
income.info()
income.isna().sum()<jupyter_output><empty_output><jupyter_text>Analysis: From the above table, we can conclude that the data set has missing values.<jupyter_code>income.hist(bins = 50, figsize=(20,15))
plt.show()<jupyter_output><empty_output><jupyter_text>Filling Up Missing Values<jupyter_code>income["workclass"].fillna("missing", inplace = True)
income["occupation"].fillna("missing", inplace = True)
income["native-country"].fillna("missing", inplace = True)
income.isna().sum()<jupyter_output><empty_output><jupyter_text>Analysis: No missing values  VISUALIZING DATA<jupyter_code>income.plot(kind="scatter", x="age", y="hours-per-week")<jupyter_output><empty_output><jupyter_text>Analysis: 1 worker at age 90 works 100 hours a week<jupyter_code>#correlation of attirbutes
corr_matrix = income.corr()
corr_matrix["income_>50K"].sort_values(ascending = False)
ax = sns.heatmap(corr_matrix)
#Heatmap showing the correlation of all attributes<jupyter_output><empty_output><jupyter_text>Analysis: Income_>50 has a high correlation with educational-num<jupyter_code>from pandas.plotting import scatter_matrix
attributes = ["age","capital-loss","capital-gain", "hours-per-week"]
scatter_matrix(income[attributes], figsize = (12,8));
income.plot.hist(figsize = (10,30),bins = 20, subplots = True);<jupyter_output><empty_output><jupyter_text>Analysis: Final Weight shows a normal distribution frequency  F21DL CW1(P1): MACHINE LEARNING  Dataset 1 (Taking all attributes in the parent dataset as features) Setting up the data  We are taking class as income_>50<jupyter_code>income.head()
#Removing target attribute(income_>50K) from the dataset
X = income.drop('income_>50K', axis = 1)
X.head()
#Creating a set with only the target attribute
y = income['income_>50K']
y.head()
# Splitting data into target and training set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.2) #20% of the whole datset is the test size

X_train.shape, X_test.shape, y_train.shape, y_test.shape
X_test.head()
y_test.head()<jupyter_output><empty_output><jupyter_text>Converting categorical values to numerical values<jupyter_code>#Converting categorical values into numerical values using dummies
dummies = pd.get_dummies(income[["age", "workclass", "education", "educational-num", "capital-gain", "capital-loss", "fnlwgt", "marital-status", "occupation", 
                           "relationship", "race", "gender","hours-per-week","native-country"]])
dummies

#Checking if our categorical variable has converted into a numerical value
X["workclass"].value_counts()
#Refitting the model
np.random.seed(0)
X_train, X_test, y_train, y_test = train_test_split(dummies, y, test_size = 0.2)

X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Training and Predicting our dataset with Naive Bayes Classifier<jupyter_code>#Fitting our training model into the MultinomialNB Classifier
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
#Predicting our model X_test with the classifier
x_preds = classifier.predict(X_test)
x_preds
# Checking the sum of correct number of prediction
correct_pred = (y_test == x_preds).sum() 
correct_pred<jupyter_output><empty_output><jupyter_text>Analysis: Our model has predicted 6848 right predictions for the test set.  Recording the major metrics - Dataset 1<jupyter_code>from sklearn.metrics import recall_score, precision_score, f1_score, confusion_matrix

#Accuracy of our model
accuracy = classifier.score(X_test, y_test)
print(f"Accuracy of the model = {accuracy:.2%}")
#Recall Score tp / (tp + fn) (proportion of actual positive identified correctly)
recall_score(y_test, x_preds)
#Precision score tp / (tp + fp) (proportion of positive identifications that was actually correct)
precision_score(y_test, x_preds)
#F1 score (Balace between Precison and Recall)
f1_score(y_test, x_preds)
#Confusion Matrix
conf_m = confusion_matrix(y_test, x_preds)
conf_m
tp,fp,fn,tn = conf_m.ravel() 
print("True Positive = {}, False Positive = {}, True Negative = {}, False Negative = {}".format(tp,fp,fn,tn))
#ROC Curve
from sklearn.metrics import roc_curve,roc_auc_score
y_probs = classifier.predict_proba(X_test)
y_probs = y_probs[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, y_probs)
fpr
def plot_roc_curve(fpr, tpr):
    # Plot ROC curve
    plt.plot(fpr, tpr, color='green', label='ROC')
    # Plot line with no predictive power (baseline)
    plt.plot([0, 1], [0, 1], color='black', linestyle='--', label='No Skill')
    # Customization of the plot
    plt.title('Receiver Operating Characteristic Curve')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.legend()
    plt.show()
    
plot_roc_curve(fpr, tpr)<jupyter_output><empty_output><jupyter_text>Analysis: ROC curve is very close to the baseline, which indicates that the model has poor performance. The closer the curve comes to the 45-degree diagonal of the ROC space, the less accurate the test.<jupyter_code>#quantifying the ROC curve in a single number is AUC (Area Under Curve)
roc_auc_score(y_test, y_probs) 
from sklearn.metrics import classification_report

print(classification_report(y_test, x_preds))<jupyter_output>precision    recall  f1-score   support

           0       0.79      0.96      0.87      6675
           1       0.61      0.22      0.33      2117

    accuracy                           0.78      8792
   macro avg       0.70      0.59      0.60      8792
weighted avg       0.75      0.78      0.74      8792<jupyter_text>Dataset 2 (Taking top 5 feature of the class)  Setting up the data  Top features were selected by reading the correlation matrix<jupyter_code>income.head()
X = income.drop(['income_>50K', 'capital-gain','capital-loss','fnlwgt','race','marital-status','gender','education'], axis = 1)
X.head()
y = income['income_>50K']
y.head()
#Converting categorical values into numerical values using dummies
dummies = pd.get_dummies(income[["educational-num", "hours-per-week", "occupation", 
                           "relationship", "native-country"]])
dummies<jupyter_output><empty_output><jupyter_text>Fitting the model - Dataset 2 (Multinomial NB)<jupyter_code>#fitting the model
np.random.seed(1)
X_train, X_test, y_train, y_test = train_test_split(dummies, y, test_size = 0.2)

X_train.shape, X_test.shape, y_train.shape, y_test.shape
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
x_preds = classifier.predict(X_test)
x_preds
correct_pred = (y_test == x_preds).sum() 
correct_pred<jupyter_output><empty_output><jupyter_text>Analysis: Our model has predicted 7243 right predictions for the test set. Hence an improved version of the previous model  Recording Major Metrics - Dataset 2<jupyter_code>from sklearn.metrics import recall_score, precision_score, f1_score, confusion_matrix
#Accuracy of our model
accuracy = classifier.score(X_test, y_test)
print(f"Accuracy of the model = {accuracy:.2%}")

recall_score(y_test, x_preds)
precision_score(y_test, x_preds)
f1_score(y_test, x_preds)
conf_m = confusion_matrix(y_test, x_preds)
conf_m
tp,fp,fn,tn = conf_m.ravel() 
print("True Positive = {}, False Positive = {}, True Negative = {}, False Negative = {}".format(tp,fp,fn,tn))
#ROC Curve
from sklearn.metrics import roc_curve,roc_auc_score
y_probs = classifier.predict_proba(X_test)
y_probs = y_probs[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, y_probs)
fpr
plot_roc_curve(fpr, tpr)<jupyter_output><empty_output><jupyter_text>Analysis: ROC curve is farther away from the baseline, which indicates good/better performance of the model.<jupyter_code>#quantifying the ROC curve in a single number is AUC (Area Under Curve)
roc_auc_score(y_test, y_probs) 

from sklearn.metrics import classification_report
print(classification_report(y_test, x_preds))<jupyter_output>precision    recall  f1-score   support

           0       0.86      0.92      0.89      6735
           1       0.66      0.52      0.58      2057

    accuracy                           0.82      8792
   macro avg       0.76      0.72      0.73      8792
weighted avg       0.81      0.82      0.82      8792<jupyter_text>Dataset 3 (Taking top 2 feature of the class)  Setting up the data  Top features were selected by reading the correlation matrix<jupyter_code>income.head()
X = income.drop(['income_>50K', 'capital-gain','capital-loss','fnlwgt','race','marital-status','gender','education',
                ], axis = 1)
X.head()
y = income['income_>50K']
y.head()
#Converting categorical values into numerical values using dummies
dummies = pd.get_dummies(income[["educational-num", "relationship"]])
dummies<jupyter_output><empty_output><jupyter_text>Fitting the model - Dataset 3 (Gaussian NB)<jupyter_code>#fitting the model
np.random.seed(5)
X_train, X_test, y_train, y_test = train_test_split(dummies, y, test_size = 0.2)

X_train.shape, X_test.shape, y_train.shape, y_test.shape
#Implemented Gaussian NB 
from sklearn.naive_bayes import GaussianNB
classifier = GaussianNB()
classifier.fit(X_train, y_train)
x_preds = classifier.predict(X_test)
x_preds
correct_pred = (y_test == x_preds).sum() 
correct_pred<jupyter_output><empty_output><jupyter_text>Analysis: Our model has predicted 6257 right predictions for the test set. Hence this model is less accurate than the last model.  Recording Major Metrics - Dataset 3<jupyter_code>#Accuracy of our model
from sklearn.metrics import recall_score, precision_score, f1_score, confusion_matrix
accuracy = classifier.score(X_test, y_test)
print(f"Accuracy of the model = {accuracy:.2%}")
recall_score(y_test, x_preds)
precision_score(y_test, x_preds)
f1_score(y_test, x_preds)
conf_m = confusion_matrix(y_test, x_preds)
conf_m
tp,fp,fn,tn = conf_m.ravel() 
print("True Positive = {}, False Positive = {}, True Negative = {}, False Negative = {}".format(tp,fp,fn,tn))
#ROC Curve
from sklearn.metrics import roc_curve,roc_auc_score
y_probs = classifier.predict_proba(X_test)
y_probs = y_probs[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, y_probs)
plot_roc_curve(fpr, tpr)<jupyter_output><empty_output><jupyter_text>Analysis: ROC curve is farther away from the baseline, which indicates good/better performance of the model.<jupyter_code>#quantifying the ROC curve in a single number is AUC (Area Under Curve)
roc_auc_score(y_test, y_probs) 
from sklearn.metrics import classification_report
print(classification_report(y_test, x_preds))<jupyter_output>precision    recall  f1-score   support

           0       0.94      0.67      0.78      6721
           1       0.44      0.86      0.58      2071

    accuracy                           0.71      8792
   macro avg       0.69      0.76      0.68      8792
weighted avg       0.82      0.71      0.73      8792
{'fraction_non_alphanumeric': 0.10428110558897899, 'fraction_numerical': 0.0253727439183887, 'mean_word_length': 4.992685475444096, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 92, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '278535', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4181, 'n_tokens_neox': 3847, 'n_words': 998, 'path': 'F21DL CW1 (Part 1).ipynb', 'repo_name': 'PranavPai/F21DL-Coursework', 'size': '11469'}