我正在尝试使用具有朴素贝叶斯分类器的字符串值的数据集进行预测。具有 14 列和 12 列的数据集具有字符串值。
我使用 Labalencoder 和 onehot 编码器对数据集进行编码,并准备好数据集以使用朴素贝叶斯分类器。
dataset = pd.read_csv('D:\\\\CRC data set copies\\Testing1.csv')
columns = ['Age', 'Weight', 'Gender', 'Ethnic_Group', 'Religion', 'Smoking', 'Alchohol', 'Maritial_Status', 'Family_History', 'District', 'Blood_in_stools', 'Abnormal_Stomach_pain', 'Weight_Loss', 'Tiredness']
X = dataset[columns]
y = dataset['Class']
labelencoder_X = LabelEncoder() # encoding the categorical variables
# replacing the column0 categorical data with numeric values
for col in columns[2:]:
X[col] = labelencoder_X.fit_transform(X[col])
onehotencoder = OneHotEncoder(categorical_features=[2,3,4,5,6,7,8,9,10,11,12,13])
# creating new columns and representing true by 1
X = onehotencoder.fit_transform(X).toarray()
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
然后模型被创建并保存。
joblib.dump(model, 'model_joblib')
# load the trained model using joblib
load_model = joblib.load('model_joblib')
predict = [[70,65,"M","b","s","Yes","Yes","MA","Y","kurunegala","P","P","P","P"]]
predict = pd.DataFrame(predict,columns=columns)
for col in columns[2:]:
predict[col] = labelencoder_X.fit_transform(predict[col])
predict = onehotencoder.transform(predict).toarray()
print('\nNew predicted value: ', load_model.predict(predict))
我想获取用户输入并使用保存的朴素贝叶斯模型预测结果。我尝试使用相同的编码方式对用户输入进行编码,但现在它的编码不与数据集值相同。正因为如此,预测是错误的。
有人可以帮助我将用户输入值编码为与数据集已编码的值相同的值吗?
最佳答案
在线:
for col in columns[2:]:
predict[col] = labelencoder_X.fit_transform(predict[col])
尝试使用 labelencoder_X.transform(predict[col]) 而不是 fit_transform。
关于python - 无法对提供给加载的朴素贝叶斯模型的用户输入值进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648492/