这就是我训练模型的方式。我已经使用 train_test_split 拆分了数据。
G_clf = GaussianNB()
G_clf.fit(X_train, y_train)
M_clf = MultinomialNB()
M_clf.fit(X_train, y_train)
B_clf = BernoulliNB(alpha=0.1, fit_prior=True, binarize=0.5)
B_clf.fit(X_train, y_train)
然后把它扔到泡菜里
with open('nbB_model.pkl', 'wb') as f:
pickle.dump(B_clf, f)
nbB_model_dump = pickle.load(open('nbB_model.pkl', 'rb'))
with open('nbG_model.pkl', 'wb') as g:
pickle.dump(G_clf, g)
nbG_model_dump = pickle.load(open('nbG_model.pkl', 'rb'))
with open('nbM_model.pkl', 'wb') as h:
pickle.dump(M_clf, h)
nbM_model_dump = pickle.load(open('nbM_model.pkl', 'rb'))
这是根据输入的薪水和经验预测工作的代码
job_map = {
1: 'Junior',
2: 'Senior',
3: 'Project Manager',
4: 'CTO',
}
def predictnaive():
# Get the user's input values
salary = float(request.form['salary'])
experience = float(request.form['experience'])
# Use each Naive Bayes model to make a prediction based on the user's input values
gaussian_prediction = gaussian_nb.predict([[salary, experience]])[0]
multinomial_prediction = multinomial_nb.predict([[salary, experience]])[0]
bernoulli_prediction = bernoulli_nb.predict([[salary, experience]])[0]
# Map the predicted job titles to their corresponding string values
gaussian_prediction = job_map.get(gaussian_prediction)
multinomial_prediction = job_map.get(multinomial_prediction)
bernoulli_prediction = job_map.get(bernoulli_prediction)