LoginSignup
1
1

classification_reportをデータフレーム化

Posted at

classification_reportはそのまま使うと小数点以下2桁で、詳細に使おうとすると辞書形式になります。

なのでより自由かつCSVで保存できるようにデータフレーム化する関数を作ってみました。

関数

from sklearn.metrics import classification_report
import pandas as pd
def classification_report_dataframe(y, y_pred):
    rep = classification_report(y, y_pred, output_dict=True)
    dst = []
    index = []
    for col in rep:
        index.append(col)
        if col != "accuracy":
            dst.append([rep[col]["precision"], rep[col]["recall"], rep[col]["f1-score"],rep[col]["support"]])
        else:
            dst.append(["", "", rep["accuracy"], rep["weighted avg"]["support"]])
    df_rep = pd.DataFrame(dst)
    df_rep.index = index
    df_rep.columns = ["precision", "recall", "f1-score", "support"]
    return df_rep

使用例

from sklearn.linear_model import LogisticRegression as LR
import pandas as pd

df = pd.read_csv("iris.csv")

y = df["category"]
x = df.drop("category", axis=1)
for col in x.columns:
    x[col] = (x[col] - x[col].mean()) / x[col].std()
model = LR()
model.fit(x, y)
y_pred = model.predict(x)

df_rep = classification_report_dataframe(y, y_pred)    
df_rep

image.png

ちなみにこれが普通にclassification_reportを使うとこうなります。
image.png

どっちがいいかは人それぞれですが、精度をめちゃくちゃ気にする場合はこの関数が有効かもしれません。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1