简体   繁体   English

在 CSV 文件中打印预测结果

[英]print prediction results in CSV file

I applied many deep learning techniques and I have a result for each model, I want to print the prediction(class label) in a CSV file.我应用了许多深度学习技术,每个 model 都有一个结果,我想在 CSV 文件中打印预测(类标签)。 this is the code I used这是我使用的代码

# load model
model = keras.models.load_model('bestmodelslstm.h5')

df = train[['clean_tweet_no_stop','Affect Dimension']]
df.head()
#output
clean_tweet_no_stop                                     Affect Dimension
0   النهارده كنت قاعد البيت مرعوب صوت جمهور الدر... fear
1   اول مره اشوف حديقه وسط طرق سريعه مواقف سيارا... fear
2   اي حلو وانت مرعب العالم حاط السكين يمينك ال...  fear
3   الأنظمة تمارس الحجب الكلمة والرأي المخالف أ...  fear
4   وحيد جدا أغلق هاتفي بلا خوف يقلق بشأني أحد أ... fear

train.shape
#(5600, 2)

labels = np.array(df['Affect Dimension'])
y = []
for i in range(len(df["Affect Dimension"])):
    if labels[i] == 'anger':
        y.append(0)
    if labels[i] == 'fear':
        y.append(1)
    if labels[i] == 'joy':
        y.append(2)
    if labels[i] == 'sadness':
        y.append(3)
y = np.array(y)
labels = tf.keras.utils.to_categorical(y, 4, dtype="float32")
del y

print(labels)
#output
[[0. 1. 0. 0.]
 [0. 1. 0. 0.]
 [0. 1. 0. 0.]
 ...
 [0. 0. 0. 1.]
 [0. 0. 0. 1.]
 [0. 0. 0. 1.]]

X_train, X_test, y_train, y_test = train_test_split(tweets,labels, test_size=0.25, random_state=0)
print (len(X_train),len(X_test),len(y_train),len(y_test))
#4200 1400 4200 1400


test_loss, test_acc = model1.evaluate(X_test, y_test, verbose=2)
print('Model accuracy: ',test_acc)
predictions = model1.predict(X_test)
matrix = confusion_matrix(y_test.argmax(axis=1), np.around(predictions, decimals=0).argmax(axis=1))

#Model accuracy:  0.4235714285714286

pd.DataFrame(matrix, index = ['Anger','Fear','Joy', 'Sadness'],columns = ['Anger','Fear','Joy', 'Sadness'])


from sklearn.metrics import classification_report
print(classification_report(np.argmax(y_test, axis=1), np.argmax(predictions, axis=1)))

print(predictions)

#Output for prediction for each emotion and predict labels 
[[3.1125692e-01 3.5565314e-01 9.2857322e-03 3.2380414e-01]
 [3.4271225e-01 4.1075385e-01 4.9968611e-04 2.4603422e-01]
 [3.3079410e-01 3.0755761e-01 4.6885073e-02 3.1476316e-01]
 ...
 [2.8897709e-01 4.5005488e-01 4.4657052e-05 2.6092330e-01]
 [3.3190650e-01 3.2205048e-01 2.6132595e-02 3.1991041e-01]
 [8.9242887e-03 6.6916817e-03 9.7304100e-01 1.1342982e-02]]

predictions.shape
(1400, 4)

import numpy as np
import pandas as pd

my_results=pd.DataFrame(matrix, index = ['Anger','Fear','Joy', 'Sadness'],columns = ['Anger','Fear','Joy', 'Sadness'])

my_results
#   Anger   Fear    Joy Sadness
Anger   328 1   24  0
Fear    350 3   8   0
Joy 134 0   188 0
Sadness 337 1   26  0

from sklearn.metrics import classification_report
print(classification_report(np.argmax(y_test, axis=1), np.argmax(predictions, axis=1)))

    precision    recall  f1-score   support

           0       0.40      0.16      0.23       353
           1       0.36      0.57      0.44       361
           2       0.63      0.75      0.69       322
           3       0.29      0.25      0.27       364

    accuracy                           0.42      1400
   macro avg       0.42      0.43      0.41      1400
weighted avg       0.41      0.42      0.40      1400

#prediction = pd.DataFrame(predictions, columns=['anger', 'Fear', 'Joy', 'Sadness']).to_csv('prediction.csv')```

but i got a CSV file with values of each emotion without prediction class label or true label(original)!但我得到了一个 CSV 文件,其中包含每种情绪的值,没有预测 class label 或真实标签(原始)!

i want to print tweet and original class label and predict class label?我想打印推文和原始 class label 并预测 class ZD304BA20E96D87411588EEABAC850E34

create a dataframe, for input创建一个 dataframe,用于输入

my_results=pd.DataFrame(matrix, index = ['Anger','Fear','Joy', 'Sadness'],columns = ['Anger','Fear','Joy', 'Sadness'])

then add the prediction to it.然后将预测添加到它。

my_results['prediction']=predictions

and then save the csv.然后保存 csv。

my_results.to_csv("Result.csv",index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM