简体   繁体   English

使用python查找置信区间

[英]Finding Confidence Interval using python

I am trying to calculate confidence interval using margin of error but it is giving me an error.我正在尝试使用误差幅度计算置信区间,但它给了我一个错误。 Below is the code I've written.下面是我写的代码。 Please help!请帮忙!

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)

which gives:这使:

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

Most probably, this is due to a typo - you are missing parentheses in最有可能的是,这是由于打字错误 - 您缺少括号

sample_mean = data_sample['installment'].mean

it should be它应该是

sample_mean = data_sample['installment'].mean()

mean (no parentheses) is a method and not a float, hence the error when trying to add it to margin_of_error (which is indeed a float). mean (无括号)是一种方法而不是浮点数,因此在尝试将其添加到margin_of_error (这确实是一个浮点数)时会出现错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM