【问题标题】:Visualization data train and data test from train_test_split with seaborn来自 train_test_split 和 seaborn 的可视化数据训练和数据测试
【发布时间】:2022-01-19 14:02:04
【问题描述】:

我有一个第 9583 行的数据,我将其拆分为 train_test_split。我想像这个例子一样使用 barplot 可视化我的数据训练和数据测试:

import pandas as pd

df = pd.read_excel("Data/data_clean_spacy_for_implementation.xlsx")

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    df["text"], df["label"], test_size=0.2, stratify=df["label"], random_state=42)

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()

X_train = vectorizer.fit_transform(X_train)
X_test = vectorizer.transform(X_test)
X_array = X_train.toarray()

print(X_train.shape) #output (7666, 12222)
print(X_test.shape) #output (1917, 12222)

怎么做?

我的数据github

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    您可以使用 value_counts 来计算每个标签的唯一值,然后使用 sns.barplot 使用 index 作为 x 轴和 values 作为 y 轴。如果这对您的分析有任何意义,您可以使用sharey='row' (plt.subplots(..., sharey='row')),这样每一行(两列,traintest)将共享相同的 y 轴。

    ...
    ...
    print(X_train.shape) #output (7666, 12222)
    print(X_test.shape) #output (1917, 12222)
    
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    fig, ax = plt.subplots(1,2, figsize=(12,5))
    for idx, group in enumerate([('Train', y_train), ('Test', y_test)]):
        data = group[1].value_counts()
        sns.barplot(ax=ax[idx], x=data.index, y=data.values)
        ax[idx].set_title(f'{group[0]} Label Count')
        ax[idx].set_xlabel(f'{group[0]} Labels')
        ax[idx].set_ylabel('Label Count')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-05-28
      • 2022-12-10
      • 2017-02-20
      • 1970-01-01
      • 2021-08-03
      • 2021-03-14
      • 2019-07-06
      • 2019-06-16
      • 2019-12-26
      相关资源
      最近更新 更多