简体   繁体   English

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

[英]Data science python error- ValueError: x and y must have same first dimension

I am working on doing some statistical analysis in python however I am new to the field and have been stuck on an error. 我正在使用python做一些统计分析,但是我是该领域的新手,并且一直陷入错误。

For background, I am computing a set of sample_means for each sample size, 200 times. 对于背景,我为每个样本大小计算了200个sample_means集合。 I am then calculating the mean and standard deviation for each sample size, which are then stored in arrays. 然后,我将计算每个样本量的平均值和标准差,然后将其存储在数组中。 This is my code: 这是我的代码:

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)

I am now trying to plot the data with the following input: 我现在尝试使用以下输入来绘制数据:

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");

However when I do, I get thrown the following error: 但是,当我这样做时,会抛出以下错误:

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,)

Any thoughts on where I am going wrong? 对我要去哪里错有任何想法吗? I feel like its probably something obvious that im not seeing as I am new to this. 我觉得这很明显,因为我对此并不陌生。 Any help would be appreciated!! 任何帮助,将不胜感激!!

This line: 这行:

plt.plot(sample_sizes, mean_of_sample_means)

need both arguments to have the same shape (because you need x and y for your plot on some cartesian coordinate-system; to be more precise: the same size in regards to the first dimension as seen in the error: if x.shape[0] != y.shape[0] ). 需要两个参数都具有相同的形状(因为在某些笛卡尔坐标系上绘图需要x和y;更准确地说:关于第一维的大小与错误中所示的相同): if x.shape[0] != y.shape[0] )。

But: 但:

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

and: 和:

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 !

And as expected, the error gives exactly this info: ValueError: x and y must have same first dimension, but have shapes (1000,) and (200,) 并如预期的那样,该错误完全提供了以下信息: ValueError: x and y must have same first dimension, but have shapes (1000,) and (200,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python ValueError:x 和 y 必须具有相同的第一维 - Python ValueError: x and y must have same first dimension Python,ValueError:x 和 y 必须具有相同的第一维问题 - Python, ValueError: x and y must have same first dimension issue 输入错误,然后输入值错误:x 和 y 必须具有相同的第一维 - Type error, and then ValueError: x and y must have same first dimension Matplotlib:ValueError:x和y必须具有相同的第一维错误 - Matplotlib: ValueError: x and y must have same first dimension Error Python Pyplot错误:“ ValueError:x和y必须具有相同的第一维” - Python Pyplot error: “ValueError: x and y must have same first dimension” ValueError:x 和 y 必须具有相同的第一维,但具有形状 (1, 2) 和 (2,) - ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,) ValueError:x 和 y 必须具有相同的第一维,但具有形状 - ValueError: x and y must have same first dimension, but have shapes ValueError:x 和 y 必须具有相同的第一维,但具有形状 (6,) 和 (8,) - ValueError: x and y must have same first dimension, but have shapes (6,) and (8,) x和y必须在python中具有相同的第一维错误 - x and y must have same first dimension ERROR in python 绘图:ValueError:x 和 y 必须具有相同的第一维 - Plotting: ValueError: x and y must have same first dimension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM