Issue
I have somewhat working code, which is giving me trouble. I seem to get an almost random accuracy_score metric, whereas my printout of predicted values suggests otherwise. I was following this tutorial online and here is what I have written so far:
import os
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix
adult_train = pd.read_csv(os.path.expanduser("~/Desktop/") + "adult_train_srt.csv", sep=',')
print(adult_train.head(100))
le = LabelEncoder()
adult_train['age'] = le.fit_transform(adult_train['age'])
adult_train['workclass'] = le.fit_transform(adult_train['workclass'].astype(str))
adult_train['education'] = le.fit_transform(adult_train['education'].astype(str))
adult_train['occupation'] = le.fit_transform(adult_train['occupation'].astype(str))
adult_train['race'] = le.fit_transform(adult_train['race'].astype(str))
adult_train['sex'] = le.fit_transform(adult_train['sex'].astype(str))
adult_train['hours_per_week'] = le.fit_transform(adult_train['hours_per_week'])
adult_train['native_country'] = le.fit_transform(adult_train['native_country'].astype(str))
adult_train['classs'] = le.fit_transform(adult_train['classs'].astype(str))
cols = [col for col in adult_train.columns if col not in ['classs']]
data = adult_train[cols]
target = adult_train['classs']
data_train, data_test, target_train, target_test = train_test_split(data, target, test_size = 0.1) #, random_state = 42)
gnb = GaussianNB()
pred = gnb.fit(data_train, target_train).predict(data_test)
pred_gnb = gnb.predict(data_test)
print(pred_gnb)
print("Naive-Bayes accuracy: (TN + TP / ALL) ", accuracy_score(pred_gnb, target_test)) #normalize = True
print("""Confusion matrix:
TN - FP
FN - TP
Guessed:
0s +, 1s -
0s -, 1s +
""")
print(confusion_matrix(target_test, pred_gnb))
Prediction = pd.DataFrame({'Prediction':pred_gnb})
result = pd.concat([adult_train, Prediction], axis=1)
print(result.head(10))
I am at a loss, I have no way of understanding whether or not my dataframe concatenation is working or if the accuracy_score metric is solving something else, because I get outputs like so:
This particular instance it is saying there are 7 true negatives (OK), 1 false positive (???), 2 false negatives (O.K), and 0 true positives (???, but there was one guessed correct?). The [classs] column is what the [Prediction] columnn is guessing.
Solution
result = pd.concat([adult_train, Prediction], axis=1)
Here the Prediction dataframe, should not be concatenated with adult_train, Prediction is the result of prediction on the test set data_set
pred_gnb = gnb.predict(data_test)
So, I think you should concatenate the data_test, the target_test and the Prediction, try this and it may work
result = pd.concat([pd.DataFrame(data_test), pd.DataFrame(target_test), Prediction], axis=1)
Answered By - Djaballah DJEDID
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.