我试图使用误差幅度计算置信区间,但它给了我一个错误。下面是我写的代码。请帮忙!import pandas as pdimport scipy.stats as statsimport mathimport numpy as npimport warningswarnings.filterwarnings('ignore')sample_size=2000z_critical = stats.norm.ppf(q = 0.95) # path [File location variable]#Code starts heredata = pd.read_csv(path)data_sample = data.sample(n=2000, random_state=0)sample_mean = data_sample['installment'].meansample_std = data_sample['installment'].std()a = sample_size ** 0.5margin_of_error = z_critical * (sample_std/44.721)confidence_interval = (sample_mean + margin_of_error, sample_mean - margin_of_error)这给出了:---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-1-3f5eda42a1b0> in <module>() 24 # print(type(a)) 25 margin_of_error = z_critical * (sample_std/a)---> 26 confidence_interval = (sample_mean + margin_of_error, sample_mean - margin_of_error) 27 true_mean = data['installment'].mean 28 print(confidence_interval)TypeError: unsupported operand type(s) for +: 'method' and 'float'
1 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
最有可能的是,这是由于拼写错误 - 您缺少括号
sample_mean = data_sample['installment'].mean
它应该是
sample_mean = data_sample['installment'].mean()
mean
(没有括号)是一个方法而不是浮点数,因此在尝试将其添加到(实际上是浮点数)时会出现错误。margin_of_error
添加回答
举报
0/150
提交
取消