# Look through unique values in each categorical column categorical_cols = train_df.select_dtypes(include="object").columns.tolist() for col in categorical_cols: print(f"{col}", f"Number of unique entries: {len(train_df[col].unique().tolist())},") print(train_df[col].unique().tolist()) def plot_bar_chart(df, columns, grid_rows, grid_cols, x_label='', y_label='', title='', whole_numbers_only=False, count_labels=True, as_percentage=True): num_plots = len(columns) grid_size = grid_rows * grid_cols num_rows = math.ceil(num_plots / grid_cols) if num_plots == 1: fig, axes = plt.subplots(1, 1, figsize=(12, 8)) axes = [axes] # Wrap the single axes in a list for consistent handling else: fig, axes = plt.subplots(num_rows, grid_cols, figsize=(12, 8)) axes = axes.ravel() # Flatten the axes array to iterate over it for i, column in enumerate(columns): df_column = df[column] if whole_numbers_only: df_column = df_column[df_column % 1 == 0] ax = axes[i] y = [num for (s, num) in df_column.value_counts().items()] x = [s for (s, num) in df_column.value_counts().items()] ax.bar(x, y, color='blue', alpha=0.5) try: ax.set_xticks(range(x[-1], x[0] + 1)) except: pass ax.set_xlabel(x_label) ax.set_ylabel(y_label) ax.set_title(title + ' - ' + column) if count_labels: df_col = df_column.value_counts(normalize=True).mul(100).round(1).astype(str) + '%' for idx, (year, value) in enumerate(df_column.value_counts().items()): if as_percentage == False: ax.annotate(f'{value}\n', xy=(year, value), ha='center', va='center') else: ax.annotate(f'{df_col[year]}\n', xy=(year, value), ha='center', va='center', size=8) if num_plots < grid_size: for j in range(num_plots, grid_size): fig.delaxes(axes[j]) # Remove empty subplots if present plt.tight_layout() plt.show()
时间: 2024-04-02 20:34:27 浏览: 18
这段代码定义了一个名为plot_bar_chart的函数,它可以绘制柱状图。函数的输入包括一个数据框(df)、一个列名的列表(columns)、网格的行数和列数(grid_rows和grid_cols)、x轴和y轴标签(x_label和y_label)、标题(title)、是否只显示整数(whole_numbers_only)、是否在图上显示数据标签(count_labels)、以及是否以百分比形式显示数据标签(as_percentage)。
在函数内部,它首先计算需要绘制的子图的数量,然后根据网格的行数和列数创建子图。接下来,对于每个给定的列名,函数从数据框中选择该列,并将其存储在变量df_column中。如果whole_numbers_only为True,则函数只绘制整数值。接着,函数将x轴和y轴的值分别设置为列中每个唯一值的出现次数和值本身。最后,函数在每个子图上绘制柱状图,并将数据标签添加到每个条形上。如果没有足够的子图来填充整个网格,则函数会删除空的子图以使图像更清晰。
相关问题
train_y=tf.keras.utils.to_categorical(train_y) test_y=tf.keras.utils.to_categorical(test_y)
这两段代码分别是什么意思?
第一段代码 `train_y=tf.keras.utils.to_categorical(train_y)` 的作用是将训练集的标签进行one-hot编码。其中,`train_y`是训练集的标签,`to_categorical`是keras.utils中的函数。one-hot编码是指将标签转换为向量的形式,向量的长度等于标签的种类数,向量中只有一个元素为1,其余元素都为0,1所在的位置表示该样本的标签。例如,如果标签种类数为3,那么标签1会被编码为[1,0,0],标签2会被编码为[0,1,0],标签3会被编码为[0,0,1]。这样做的目的是为了方便神经网络模型的训练。
第二段代码 `test_y=tf.keras.utils.to_categorical(test_y)` 的作用是将测试集的标签进行one-hot编码,与第一段代码的作用类似,都是将标签转换为向量的形式,方便神经网络模型的训练。
y_train = tf.keras.utils.to_categorical(y_train, num_classes=100)
这段代码是使用 TensorFlow 的 Keras 工具函数 `to_categorical()` 将目标变量 `y_train` 转换为独热编码格式。独热编码是一种用于表示分类变量的编码方法,其中每个类别都被表示为一个只有一个元素为1,其余元素为0的向量。在这里,`num_classes` 参数指定了类别的数量,即100个类别。这样做的目的是将目标变量转换为适合训练神经网络的格式。