【问题标题】:Data science python error- ValueError: x and y must have same first dimension数据科学python错误-ValueError:x和y必须具有相同的第一维
【发布时间】:2018-02-15 13:52:28
【问题描述】:

我正在使用 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,)

对我哪里出错有任何想法吗?我觉得它可能很明显,我没有看到,因为我是新手。任何帮助,将不胜感激!!

【问题讨论】:

  • 嗨,您可以尝试将代码编辑为您用来生成ValueError 的实际代码吗?包括所有模块导入和变量定义(例如,trials 不存在)
  • 我也这么认为,但我认为 OP 副本是从笔记本上粘贴的
  • 我认为您的 mean_of sample_means 将有 500.5 的所有 200 个恒定条目。与std_dev_of_sample_means 相同,它将有 288.67499025720952
  • 您的 sample_sizes 数组是一个在所有 200 次迭代中包含 [1,2,3....1000] 的常量数组。您正在这个常量数组上计算平均值和 std_dev 200 次 not 在这个集合中的随机样本(子集)上。

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


【解决方案1】:

这一行:

plt.plot(sample_sizes, mean_of_sample_means)

需要两个参数具有相同的形状(因为您需要 x 和 y 在某些笛卡尔坐标系上绘制绘图;更准确地说:与错误中看到的第一个维度的大小相同:@987654322 @)。

但是:

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 !

正如预期的那样,错误给出了以下信息:ValueError: x and y must have same first dimension, but have shapes (1000,) and (200,)

【讨论】:

  • 如何处理这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 2022-01-09
相关资源
最近更新 更多