<jupyter_start><jupyter_text>Combining the both train and test data to perform the naive bayes classifer<jupyter_code>data=pd.merge(train,test)
data.head()
data.shape<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>data.describe()
data.isnull().sum()
data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 5910 entries, 0 to 5909
Data columns (total 14 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   age            5910 non-null   int64 
 1   workclass      5910 non-null   object
 2   education      5910 non-null   object
 3   educationno    5910 non-null   int64 
 4   maritalstatus  5910 non-null   object
 5   occupation     5910 non-null   object
 6   relationship   5910 non-null   object
 7   race           5910 non-null   object
 8   sex            5910 non-null   object
 9   capitalgain    5910 non-null   int64 
 10  capitalloss    5910 non-null   int64 
 11  hoursperweek   5910 non-null   int64 
 12  native         5910 non-null   object
 13  Salary         5910 non-null   object
dtypes: int64(5), object(9)
memory usage: 692.6+ KB<jupyter_text>From this we can configure that there are no null values in the data<jupyter_code>data.dtypes<jupyter_output><empty_output><jupyter_text>Plots<jupyter_code>def count_plot(data, columns):
    plt.figure(figsize=(15,10))
    for indx, var in enumerate(columns):
        plt.subplot(5,3,indx+1)
        g=sns.countplot(data[var],hue=data['Salary'])
    plt.tight_layout()
features=data.columns.tolist()
features.remove('Salary')
features
import warnings 
warnings.filterwarnings('ignore') #TO get rid of the package related erros
count_plot(data,features)
for var in features:
    print(data[var].value_counts().sort_values(ascending=False)/data.shape[0])
    print('='*30)
    print()
#From this we can easily check for the percentages of values spread in the data
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix,roc_curve,roc_auc_score,recall_score,precision_score
from sklearn.metrics import f1_score,classification_report
from sklearn.naive_bayes import GaussianNB, CategoricalNB
from statsmodels.stats.outliers_influence import variance_inflation_factor
from collections import Counter<jupyter_output><empty_output><jupyter_text>Ordinal and Label Ecodings for Independent Feature we use Ordinal Encoding for Taret feature we use Label Encodings<jupyter_code>encoder=OrdinalEncoder()
data_encoded=encoder.fit_transform(data[features])
data_encoded=pd.DataFrame(data_encoded, columns=features)
data_encoded
encoder1=LabelEncoder()
data_encoded1=encoder1.fit_transform(data['Salary'])
data_encoded1
data_encoded['Salary']=data_encoded1
data_encoded
data_encoded.shape<jupyter_output><empty_output><jupyter_text>Train Test split<jupyter_code>from sklearn.naive_bayes import MultinomialNB
X=data_encoded.iloc[:,0:-1]
y=data_encoded['Salary']
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.30,random_state=10)
X_test
X_test.shape,X_train.shape,y_train.shape,y_test.shape
model=MultinomialNB()
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
y_pred
count_misclassified=(y_test !=y_pred).sum()
count_misclassified
accuracy=accuracy_score(y_test,y_pred)
print(accuracy)
print('Accuracy:{:.2f}'.format(accuracy))<jupyter_output>Accuracy:0.75<jupyter_text>Precision Recall and F1 score<jupyter_code>print("precision:",precision_score(y_test,y_pred,average='micro'))
print("f1 score:",f1_score(y_test,y_pred,average='micro'))
print("recall score:",recall_score(y_test,y_pred,average='micro'))<jupyter_output>precision: 0.754653130287648
f1 score: 0.754653130287648
recall score: 0.754653130287648<jupyter_text>Classification Report<jupyter_code>print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.93      0.76      0.84      1464
           1       0.39      0.71      0.50       309

    accuracy                           0.75      1773
   macro avg       0.66      0.74      0.67      1773
weighted avg       0.83      0.75      0.78      1773<jupyter_text>Gaussian Naive Bayes Classifier<jupyter_code>model1=GaussianNB()
model1.fit(X_train,y_train)
y_pred_gnb=model1.predict(X_test)
y_pred_gnb
count_misclassified=(y_test !=y_pred_gnb).sum()
count_misclassified<jupyter_output><empty_output><jupyter_text>There are more missclassifications in GassianNB when compared to MultinomialNB<jupyter_code>print("Recall score : ", recall_score(y_test, y_pred_gnb , average='micro'))
print("Precision score : ",precision_score(y_test, y_pred_gnb , average='micro'))
print("F1 score : ",f1_score(y_test, y_pred_gnb , average='micro'))<jupyter_output>Recall score :  0.7478849407783418
Precision score :  0.7478849407783418
F1 score :  0.7478849407783417
{'fraction_non_alphanumeric': 0.09872029250457039, 'fraction_numerical': 0.06134470851107048, 'mean_word_length': 4.918269230769231, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '59505', 'license': 'MIT', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1692, 'n_words': 334, 'path': 'Assignment -12 Naive Bayes.ipynb', 'repo_name': 'sai-kumar26/Naive-Bayes', 'size': '4923'}