accuracy_scoreや、F1_score等を求めたいのですが、
print(accuracy_score(Y_test, Y_pred)) print(precision_score(Y_test, Y_pred)) print(recall_score(Y_test, Y_pred)) print(F1_score(Y_test, Y_pred))
上記のように書いたところ、以下のようなエラーが発生しました。
ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].
そこで、webで調べてbinary対策として次のようなコードにすると良いという事で変更したのですが、
print(accuracy_score(Y_test, Y_pred,average=’micro’)) print(precision_score(Y_test, Y_pred,average=’micro’)) print(recall_score(Y_test, Y_pred,average=’micro’)) print(F1_score(Y_test, Y_pred,average=’micro’))
以下のエラーが発生し言語認識をしてくれません。どのように変えたらいいのでしょうか。
分かる方がいらしたら教えてください。
print(precision_score(Y_test, Y_pred,average=’micro’)) ^ SyntaxError: invalid character in identifier
コード本文です。
# -*- coding: utf-8 -*- import scipy as sp import pandas as pd from pandas import Series, DataFrame import matplotlib.pyplot as plt from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier #RandomForest from sklearn.svm import SVC # SVM用 import lightgbm as lgb from sklearn.metrics import confusion_matrix #混同行列 from sklearn.metrics import accuracy_score, precision_score #適合率 from sklearn.metrics import recall_score, f1_score #再現率,F1スコア from sklearn.metrics import make_scorer def SVM(X_train_std, X_test_std, Y_train, Y_test): model_SVM = SVC(random_state=0, kernel = "rbf", C = 1000 , gamma = 0.01 ) #学習モデル構築。引数に訓練データの特徴量と、それに対応したラベル model_SVM.fit(X_train_std,Y_train) #予測したクラスラベル Y_pred = model_SVM.predict(X_test_std) # .scoreで正解率を算出。 print("\nSVM") print("train score:",model_SVM.score(X_train_std,Y_train)) print("test score:",model_SVM.score(X_test_std,Y_test)) print("accuracy score:",accuracy_score(Y_test, Y_pred,average='micro')) print("precision score:",precision_score(Y_test, Y_pred,average='micro')) print("recall score:",recall_score(Y_test, Y_pred,average='micro')) print("f1 score:",f1_score(Y_test, Y_pred,average='micro')) def GBM(X_train, X_test, Y_train, Y_test): model_GBM = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=58, max_depth=14, learning_rate=0.1, n_estimators=940, min_child_samples=40, importance_type="split", random_state=0) #学習モデル構築。引数に訓練データの特徴量と、それに対応したラベル model_GBM.fit(X_train, Y_train) #予測したクラスラベル Y_pred = model_GBM.predict(X_test) # .scoreで正解率を算出。 print("\nGBM") print("train score:",model_GBM.score(X_train,Y_train)) print("test score:",model_GBM.score(X_test,Y_test)) print("accuracy score:",accuracy_score(Y_test, Y_pred,average='micro')) print("precision score:",precision_score(Y_test, Y_pred,average='micro')) print("recall score:",recall_score(Y_test, Y_pred,average='micro')) print("f1 score:",f1_score(Y_test, Y_pred,average='micro')) def main(): """ TODO case_nameに任意の名前を指定 フォルダを統一するために以降のscriptも名前を統一する """ case_name = "case3" # --------------------------------------- case_dir = "./casestudy/{}/".format(case_name) input_csv_name = "2_extracted_features_original.csv" input_csv_path = case_dir + input_csv_name input_df = pd.read_csv(input_csv_path, encoding="utf-8-sig") #すべてのデータを対象に分類を行う場合 #----------------------------------------------------------------------- task = "all" train_df = input_df[input_df["train_test_flag"] == 0] test_df = input_df[input_df["train_test_flag"] == 1] # print(train_df) # print(test_df) X_train = train_df.loc[:, "contrast":"inverse_difference_m_norm"] X_test = test_df.loc[:, "contrast":"inverse_difference_m_norm"] # print(X_train) # print(X_test) Y_train = train_df["category"] Y_test = test_df["category"] # print(Y_train) # print(Y_test) sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) # print(X_train_std) # print(X_test_std) # #------------------------------------------------------------------------ SVM(X_train_std, X_test_std, Y_train, Y_test) GBM(X_train, X_test, Y_train, Y_test) if __name__ == "__main__": main()
Ypred [1 1 1 ... 6 6 6] Ytest 384 1 385 1 386 1 387 1 388 1 .. 7675 8 7676 8 7677 8 7678 8 7679 8 Name: category, Length: 1536, dtype: in
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/02 02:19
2020/12/02 03:42 編集
2020/12/02 03:44
2020/12/02 04:49
2020/12/02 04:52
2020/12/02 06:47
2020/12/02 14:06
2020/12/02 15:59
2020/12/02 16:56