使用自制分离器获得 Precision、Recall、F1 Score

Get Precision, Recall, F1 Score with self-made splitter

我想输出 Precision、Recall 和 F1 Score 三个指标。 不幸的是我得到了 NameErrory_test 未定义。

拆分数据时,scikit-learn经常返回一个y_testy_trainx_testx_train。但是由于我自己拆分了数据,所以我没有 y_test。我怎样才能仍然输出这三个指标?

数据框:

d = {'purchaseid': [0, 0, 0, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 8, 9, 9, 9, 9],
     'itemid': [ 3, 8, 2, 10, 3, 10, 4, 12, 3, 12, 3, 4, 8, 6, 3, 0, 5, 12, 9, 9, 13, 1, 7, 11, 11]}
df = pd.DataFrame(data=d)

   purchaseid  itemid
0           0       3
1           0       8
2           0       2
3           1      10
4           2       3

代码:

PERCENTAGE_SPLIT = 20
    NUM_NEGATIVES = 4
    def splitter(df):
      df_ = pd.DataFrame()
      sum_purchase = df['purchaseid'].nunique()
      amount = round((sum_purchase / 100) * PERCENTAGE_SPLIT)
    
      random_list = random.sample(df['purchaseid'].unique().tolist(), amount)
      df_ = df.loc[df['purchaseid'].isin(random_list)]
      df_reduced = df.loc[~df['purchaseid'].isin(random_list)]
      return [df_reduced, df_]
    
    def generate_matrix(df_main, dataframe, name):
      
      mat = sp.dok_matrix((df_main.shape[0], len(df_main['itemid'].unique())), dtype=np.float32)
      for purchaseid, itemid in zip(dataframe['purchaseid'], dataframe['itemid']):
        mat[purchaseid, itemid] = 1.0
    
      return mat
    
    dfs = splitter(df)
    df_tr = dfs[0].copy(deep=True)
    df_val = dfs[1].copy(deep=True)
    
    train_mat = generate_matrix(df, df_tr, 'train')
    val_mat = generate_matrix(df, df_val, 'val')
    num_users, num_items = train_mat.shape
    user_input, item_input, labels = get_train_samples(train_mat, NUM_NEGATIVES)
    val_user_input, val_item_input, val_labels = get_train_samples(val_mat, NUM_NEGATIVES)

hist = model.fit([np.array(user_input), np.array(item_input)], np.array(labels),
                 validation_data=([np.array(val_user_input), np.array(val_item_input)], np.array(val_labels)))

尝试:

from sklearn.metrics import classification_report

y_pred = model.predict(([np.array(val_user_input), np.array(val_item_input)], np.array(val_labels)), batch_size=64, verbose=1)
y_pred_bool = np.argmax(y_pred, axis=1)

print(classification_report(y_test, y_pred_bool))

输出:

NameError: name 'y_test' is not defined

我看到的是这个...因为你没有提供任何输入数据..我无法运行确认。

你的情况

x_train is user_input, item_input
y_train is labels
x_test is val_user_input, val_item_input
y_test is val_labels