2017-09-07 902 views
0

我正在python中做一些統計分析,但我是新來的字段,並一直卡在一個錯誤。數據科學python錯誤 - ValueError:x和y必須具有相同的第一維

對於背景,我計算了一組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,) 

關於我要去哪裏的錯誤?我覺得它可能很明顯,我沒有看到,因爲我是新手。任何幫助,將不勝感激!!

+0

嗨,你能嘗試編輯代碼,以實際代碼你用來生成'ValueError'?包括所有模塊導入和變量定義(例如'trials' does not exist) – user3479780

+1

我認爲相同,但我認爲從筆記本粘貼的OP副本 –

+0

我認爲你的'mean_of sample_means'將有一個常數條目500.5所有200人。與'std_dev_of_sample_means'相同,它將成爲288.67499025720952 – kaza

回答

1

這條線:

plt.plot(sample_sizes, mean_of_sample_means) 

需要兩個參數具有相同的形狀(因爲你需要x和y的一些笛卡爾座標系上的情節,更精確地說:在關於大小相同在錯誤中看到的第一個維度: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 ! 

和預期的一樣,錯誤給出的正是這種信息:ValueError: x and y must have same first dimension, but have shapes (1000,) and (200,)

相關問題