数据科学python错误 - ValueError:x和y必须具有相同的第一个维度

时间:2017-09-07 00:03:19

标签: python python-3.x matplotlib data-science valueerror

我正在努力在python中进行一些统计分析,但我是该领域的新手并且一直遇到错误。

对于背景,我计算每个样本大小的一组sample_means,200次。然后,我计算每个样本大小的均值和标准差,然后将其存储在数组中。这是我的代码:

in[] = 
sample_sizes = np.arange(1,1001,1)
number_of_samples = 200
mean_of_sample_means = []
std_dev_of_sample_means = []
for x in range (number_of_samples):
    mean_of_sample_means.append(np.mean(sample_sizes))
    std_dev_of_sample_means.append(np.std(sample_sizes))

in[] = # mean and std of 200 means from 200 replications, each of size 10
trials[0], mean_of_sample_means[0], std_dev_of_sample_means[0] 

out[] = (10, 500.5, 288.67499025720952)

我现在正尝试使用以下输入绘制数据:

plt.plot(sample_sizes, mean_of_sample_means);
plt.ylim([0.480,0.520]);
plt.xlabel("sample sizes")
plt.ylabel("mean probability of heads")
plt.title("Mean of sample means over 200 replications");

然而,当我这样做时,我会抛出以下错误:

242         if x.shape[0] != y.shape[0]:
243             raise ValueError("x and y must have same first dimension, but "
--> 244                              "have shapes {} and {}".format(x.shape, y.shape))
245         if x.ndim > 2 or y.ndim > 2:
246             raise ValueError("x and y can be no greater than 2-D, but have "

ValueError: x and y must have same first dimension, but have shapes (1000,) and (200,)

有关我哪里出错的任何想法?我觉得它可能是显而易见的,因为我不是新来的。任何帮助,将不胜感激!!

1 个答案:

答案 0 :(得分:1)

这一行:

plt.plot(sample_sizes, mean_of_sample_means)

需要两个参数具有相同的形状(因为在某些笛卡尔坐标系上需要x和y用于绘图;更准确:与错误中显示的第一个维度相同的大小:{{ 1}})。

可是:

if x.shape[0] != y.shape[0]

sample_sizes = np.arange(1,1001,1)  # 1000 !

正如预期的那样,错误提供了完整的信息:number_of_samples = 200 mean_of_sample_means = [] for x in range (number_of_samples): mean_of_sample_means.append(np.mean(sample_sizes)) # mean by default over flattened-structure # so i assume: 1 element per iteration # 200 !