猿问

使用 python 查找置信区间

我试图使用误差幅度计算置信区间,但它给了我一个错误。下面是我写的代码。请帮忙!


import pandas as pd

import scipy.stats as stats

import math

import numpy as np

import warnings


warnings.filterwarnings('ignore')


sample_size=2000

z_critical = stats.norm.ppf(q = 0.95)  


# path        [File location variable]

#Code starts here

data = pd.read_csv(path)

data_sample = data.sample(n=2000, random_state=0)

sample_mean = data_sample['installment'].mean

sample_std = data_sample['installment'].std()

a = sample_size ** 0.5

margin_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'


holdtom
浏览 163回答 1
1回答

幕布斯6054654

最有可能的是,这是由于拼写错误 - 您缺少括号sample_mean&nbsp;=&nbsp;data_sample['installment'].mean它应该是sample_mean&nbsp;=&nbsp;data_sample['installment'].mean()mean(没有括号)是一个方法而不是浮点数,因此在尝试将其添加到(实际上是浮点数)时会出现错误。margin_of_error
随时随地看视频慕课网APP

相关分类

Python
我要回答