在 Python 中以表格形式打印数据
Printing data in Tabular Form in Python
我正在寻找 accuarcy/PRecision/Reacll 等等...
所以我使用了这段代码,它对我来说效果很好,但实际上我想将输出形式更改为表格
我的输出:
Column 2 acc: 1.0
Column 2 p: 1.0
Column 2 r: 1.0
Column 1 acc: 1.0
Column 1 p: 1.0
Column 1 r: 1.0
Column 3 acc: 1.0
Column 3 p: 1.0
Column 3 r: 1.0
我想要的输出:
+----------+-----------+-------+---------+
| Feature | Precision |Recall | Accuracy|
+----------+-----------+-------+---------+
| 1 | 1.0 | 1.0 | 1.0 |
| 2 | 1.0 | 1.0 | 1.0 |
| 3 | 1.0 | 1.0 | 1.0 |
+----------+----------+--------+---------+
我的代码:
def calc_acc(original, predect1):
common_columns = list(set(original.columns).intersection(predect1.columns))
avg_a = 0.0
avg_p = 0.0
avg_r = 0.0
for c in common_columns:
c_acc = accuracy_score(original[c], predect1[c])
p = precision_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
r = recall_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
print(f'Column {c} acc: {c_acc}')
print(f'Column {c} p: {p}')
print(f'Column {c} r: {r}')
avg_a += c_acc/len(common_columns)
avg_p += p/len(common_columns)
avg_r += r/len(common_columns)
NB: c 是列
使用此代码绘制PrettyTable
:
from prettytable import PrettyTable
pt = PrettyTable()
pt.field_names = ['Feature','Precision','Recall','Accuracy']
pt.add_row([c,p,r,c_acc])
最后你想要的代码和输出如下:
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score
from prettytable import PrettyTable
def calc_acc(original, predect1):
common_columns = list(set(original.columns).intersection(predect1.columns))
avg_a = 0.0
avg_p = 0.0
avg_r = 0.0
pt = PrettyTable()
pt.field_names = ['Feature','Precision','Recall','Accuracy']
for c in common_columns:
c_acc = accuracy_score(original[c], predect1[c])
p = precision_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
r = recall_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
pt.add_row([c,p,r,c_acc])
avg_a += c_acc/len(common_columns)
avg_p += p/len(common_columns)
avg_r += r/len(common_columns)
print(pt)
pre = [[1, 1, 3], [2, 3, 4]]
pre = pd.DataFrame(pre, columns= ['1', '2', '3'])
calc_acc(pre, pre)
输出:
您可以使用 pandas 数据框。
import pandas as pd
df = pd.DataFrame({
"Feature": col,
"Precision": prec,
"Recall": rec,
"Accuracy": acc
})
print(df)
注意: col, prec, rec, acc --> 列出数据类型。 从你的 for 循环创建这些列表,然后只将它们转换成数据帧,仅此而已。
我正在寻找 accuarcy/PRecision/Reacll 等等... 所以我使用了这段代码,它对我来说效果很好,但实际上我想将输出形式更改为表格 我的输出:
Column 2 acc: 1.0
Column 2 p: 1.0
Column 2 r: 1.0
Column 1 acc: 1.0
Column 1 p: 1.0
Column 1 r: 1.0
Column 3 acc: 1.0
Column 3 p: 1.0
Column 3 r: 1.0
我想要的输出:
+----------+-----------+-------+---------+
| Feature | Precision |Recall | Accuracy|
+----------+-----------+-------+---------+
| 1 | 1.0 | 1.0 | 1.0 |
| 2 | 1.0 | 1.0 | 1.0 |
| 3 | 1.0 | 1.0 | 1.0 |
+----------+----------+--------+---------+
我的代码:
def calc_acc(original, predect1):
common_columns = list(set(original.columns).intersection(predect1.columns))
avg_a = 0.0
avg_p = 0.0
avg_r = 0.0
for c in common_columns:
c_acc = accuracy_score(original[c], predect1[c])
p = precision_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
r = recall_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
print(f'Column {c} acc: {c_acc}')
print(f'Column {c} p: {p}')
print(f'Column {c} r: {r}')
avg_a += c_acc/len(common_columns)
avg_p += p/len(common_columns)
avg_r += r/len(common_columns)
NB: c 是列
使用此代码绘制PrettyTable
:
from prettytable import PrettyTable
pt = PrettyTable()
pt.field_names = ['Feature','Precision','Recall','Accuracy']
pt.add_row([c,p,r,c_acc])
最后你想要的代码和输出如下:
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score, recall_score
from prettytable import PrettyTable
def calc_acc(original, predect1):
common_columns = list(set(original.columns).intersection(predect1.columns))
avg_a = 0.0
avg_p = 0.0
avg_r = 0.0
pt = PrettyTable()
pt.field_names = ['Feature','Precision','Recall','Accuracy']
for c in common_columns:
c_acc = accuracy_score(original[c], predect1[c])
p = precision_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
r = recall_score(original[c], predect1[c], average='macro', labels=np.unique(predect1[c]))
pt.add_row([c,p,r,c_acc])
avg_a += c_acc/len(common_columns)
avg_p += p/len(common_columns)
avg_r += r/len(common_columns)
print(pt)
pre = [[1, 1, 3], [2, 3, 4]]
pre = pd.DataFrame(pre, columns= ['1', '2', '3'])
calc_acc(pre, pre)
输出:
您可以使用 pandas 数据框。
import pandas as pd
df = pd.DataFrame({
"Feature": col,
"Precision": prec,
"Recall": rec,
"Accuracy": acc
})
print(df)
注意: col, prec, rec, acc --> 列出数据类型。 从你的 for 循环创建这些列表,然后只将它们转换成数据帧,仅此而已。