# summarize history for accuracy and loss modelHistory(history) y_pred = model.predict([x_test_rev], batch_size=1024, verbose=1) """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Testing accuracy """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" acc = sum([ np.argmax(y_test_rev[i]) == np.argmax(y_pred[i]) for i in range(x_test_rev.shape[0]) ]) / x_test_rev.shape[0] print("Model accuracy:", acc) """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Plot confusion matrix """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" y_pred_copy = np.copy(y_pred) for row in range(y_pred_copy.shape[0]): y_pred_copy[row] = (y_pred_copy[row] == np.amax( y_pred_copy[row])).astype(int) np.set_printoptions(precision=2) plot_cf(y_test_rev.argmax(axis=1), y_pred_copy.argmax(axis=1), "LSTM", normalize=True) plt.show() """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Classification report (precision, recall, f1, micro¯o avg, weighted avg) """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" print('Classification Report') print(classification_report(y_test_rev.argmax(axis=1), y_pred.argmax(axis=1)))
#hyperparameters tuning #from sklearn.model_selection import GridSearchCV #def svc_param_selection(X, y, nfolds): # Cs = {'C': [0.001, 0.10, 0.1, 10, 25, 50]} # grid_search = GridSearchCV(LinearSVC(), Cs, cv=nfolds) # grid_search.fit(X, y) # grid_search.best_params_ # return grid_search.best_params_ #params = svc_param_selection(X_train_tfidf, y_train, 5) #best params is C = 0.1 SVM_linear.fit(X_train_tfidf, y_train) prediction_SVM_linear = SVM_linear.predict(X_test_tfidf) print("Model accuracy (review predict overall rating):", accuracy_score(y_test, prediction_SVM_linear)) """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Plot confusion matrix """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" plot_cf(y_test, prediction_SVM_linear, "SVM", normalize=True) """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Classification report (precision, recall, f1, micro¯o avg, weighted avg) """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" report = classification_report(y_test, prediction_SVM_linear) print(report) """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Save SVM model to disk """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" filename = 'svm_model_rev.sav' pickle.dump(SVM_linear, open(filename, 'wb'))