Python Numpy错误:操作数无法与形状(65,)(77,)一起广播
我试图在IPython笔记本中为.csv电子表格生成一个图表(在kaggle=titanic_data.csv中很容易找到),并在下面总结我所做的步骤。首先,我导入了工作表并取出了一些列Python Numpy错误:操作数无法与形状(65,)(77,)一起广播,python,csv,jupyter-notebook,kaggle,Python,Csv,Jupyter Notebook,Kaggle,我试图在IPython笔记本中为.csv电子表格生成一个图表(在kaggle=titanic_data.csv中很容易找到),并在下面总结我所做的步骤。首先,我导入了工作表并取出了一些列 titanic_data = pd.read_csv("titanic_data.csv") titanic_data_cleaned = titanic_data.drop(['Name','Ticket','Cabin','Fare','Embarked'], axis=1 疑问:我试图生成下面的百分比图
titanic_data = pd.read_csv("titanic_data.csv")
titanic_data_cleaned = titanic_data.drop(['Name','Ticket','Cabin','Fare','Embarked'], axis=1
疑问:我试图生成下面的百分比图,但无法解决以下错误:操作数无法与形状(65,)(77,)一起广播。我解决不了,如果有人能帮忙,谢谢
totals = survivors_age_group + non_survivors_age_group
calculate_percentage1 = survivors_age_group
calculate_percentage2 = non_survivors_age_group
# Use calculate_percentage_function to calculate the percentage of total
data1_percentages = calculate_percentage1(survivors_age_group, totals)*100
data2_percentages = calculate_percentage2(non_survivors_age_group, totals)*100
tick_spacing = np.array(range(len(age_labels)))+0.4
# Graph to percentage of survivors per class
ax2.bar(range(len(data1_percentages)), data1_percentages, alpha=0.5, color='g')
ax2.bar(range(len(data2_percentages)), data2_percentages, bottom=data1_percentages, alpha=0.5, color='r')
plt.sca(ax2)
plt.xticks(tick_spacing, age_labels)
ax2.set_ylabel("Percentage")
ax2.set_xlabel("")
ax2.set_title("% of survivors by age group",fontsize=14)
没有a很难准确判断,但是看起来问题很可能在于
ax2.bar(范围(len(data2_百分比))、data2_百分比、bottom=data1_百分比、alpha=0.5、color='r')
。data2_百分比
和data1_百分比
长度相同吗?不,它们不是因为幸存者和非幸存者在同一列,但非幸存者的数量更大。如何解决它或使用另一个numpy绘图显示?如果要在第二次调用中使用data1\u百分比
作为底部
,则它的长度必须与data2\u百分比
相同。如果无法运行代码,我无法告诉您为什么它们的长度不同。如果没有一个简单的工作示例,就不可能回答这个问题。至少,提供到数据文件的链接。