网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python分位点函数 python求分位数函数

round函数python

round函数python:

创新互联建站是一家以网络技术公司,为中小企业提供网站维护、成都网站设计、成都网站建设、网站备案、服务器租用、域名注册、软件开发、小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站电话联系:13518219792

这个函数相当于调去里面的一个函数,有一个数组,从中里面调取一个数据。简单的说,round是使用四舍五入对小数进行位数控制的函数,round(a,b),a参数是小数,b是小数点后保留的位数。实际使用需要考虑的python2和python3版本的差异与小数精度的问题。

ound函数的使用用法

根据Excel的帮助得知,round函数就是返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果。

round函数的语法是:ROUND(number,num_digits),即:Round(数值,保留的小数位数)

Number:需要进行四舍五入的数字。

Num_digits:指定的位数,按此位数进行四舍五入。

其中,如果num_digits大于0,则四舍五入到指定的小数位。

如果num_digits等于0,则四舍五入到最接近的整数。

如果num_digits小于0,则在小数点左侧进行四舍五入。

聊聊python的分位数

在日常的数据分析中,分位数 是非常重要的一环,在探查数据分布,定义指标中都必不可缺。但 python 里的分位数计算却潜藏了一些坑点,特分享。

我们先看看百度百科的 分位数 定义:

正如上文所言,四分位数 就是将数据从小到大排成4等分,然后取出3个分割点的数值。百分位数则以此类推,通过分位数 我们可以对数据的分布有更深的了解:

分位数 的定义是很容易理解的,但大部分人不知道的是,分位数的计算方法有两种:

我们依旧以 四分位数 为例

三种方法各有利弊,但结果都可能存在差距,需要与需求方仔细确认到底是哪个计算方法。

能满足4分位计算的函数主要有2个:numpy 的 percentile 方法 和 pandas 的 quantile 方法 。但他们的计算方法都是 1+(n-1)方法,我们看个例子:

既然没有现成的方法,我们就手写一个 n 的方法。

百分位的计算是非常常见的数据分析需求,但在实际使用时并没有那么的简单,专业的统计逻辑和""我们以为""的逻辑并不尽然相同。需时时谨慎,校验数据。

如何使用python做统计分析

Shape Parameters

形态参数

While a general continuous random variable can be shifted and scaled

with the loc and scale parameters, some distributions require additional

shape parameters. For instance, the gamma distribution, with density

γ(x,a)=λ(λx)a−1Γ(a)e−λx,

requires the shape parameter a. Observe that setting λ can be obtained by setting the scale keyword to 1/λ.

虽然一个一般的连续随机变量可以被位移和伸缩通过loc和scale参数,但一些分布还需要额外的形态参数。作为例子,看到这个伽马分布,这是它的密度函数

γ(x,a)=λ(λx)a−1Γ(a)e−λx,

要求一个形态参数a。注意到λ的设置可以通过设置scale关键字为1/λ进行。

Let’s check the number and name of the shape parameters of the gamma

distribution. (We know from the above that this should be 1.)

让我们检查伽马分布的形态参数的名字的数量。(我们知道从上面知道其应该为1)

from scipy.stats import gamma

gamma.numargs

1

gamma.shapes

'a'

Now we set the value of the shape variable to 1 to obtain the

exponential distribution, so that we compare easily whether we get the

results we expect.

现在我们设置形态变量的值为1以变成指数分布。所以我们可以容易的比较是否得到了我们所期望的结果。

gamma(1, scale=2.).stats(moments="mv")

(array(2.0), array(4.0))

Notice that we can also specify shape parameters as keywords:

注意我们也可以以关键字的方式指定形态参数:

gamma(a=1, scale=2.).stats(moments="mv")

(array(2.0), array(4.0))

Freezing a Distribution

冻结分布

Passing the loc and scale keywords time and again can become quite

bothersome. The concept of freezing a RV is used to solve such problems.

不断地传递loc与scale关键字最终会让人厌烦。而冻结RV的概念被用来解决这个问题。

rv = gamma(1, scale=2.)

By using rv we no longer have to include the scale or the shape

parameters anymore. Thus, distributions can be used in one of two ways,

either by passing all distribution parameters to each method call (such

as we did earlier) or by freezing the parameters for the instance of the

distribution. Let us check this:

通过使用rv我们不用再更多的包含scale与形态参数在任何情况下。显然,分布可以被多种方式使用,我们可以通过传递所有分布参数给对方法的每次调用(像我们之前做的那样)或者可以对一个分布对象冻结参数。让我们看看是怎么回事:

rv.mean(), rv.std()

(2.0, 2.0)

This is indeed what we should get.

这正是我们应该得到的。

Broadcasting

广播

The basic methods pdf and so on satisfy the usual numpy broadcasting

rules. For example, we can calculate the critical values for the upper

tail of the t distribution for different probabilites and degrees of

freedom.

像pdf这样的简单方法满足numpy的广播规则。作为例子,我们可以计算t分布的右尾分布的临界值对于不同的概率值以及自由度。

stats.t.isf([0.1, 0.05, 0.01], [[10], [11]])

array([[ 1.37218364, 1.81246112, 2.76376946],

[ 1.36343032, 1.79588482, 2.71807918]])

Here, the first row are the critical values for 10 degrees of freedom

and the second row for 11 degrees of freedom (d.o.f.). Thus, the

broadcasting rules give the same result of calling isf twice:

这里,第一行是以10自由度的临界值,而第二行是以11为自由度的临界值。所以,广播规则与下面调用了两次isf产生的结果相同。

stats.t.isf([0.1, 0.05, 0.01], 10)

array([ 1.37218364, 1.81246112, 2.76376946])

stats.t.isf([0.1, 0.05, 0.01], 11)

array([ 1.36343032, 1.79588482, 2.71807918])

If the array with probabilities, i.e, [0.1, 0.05, 0.01] and the array of

degrees of freedom i.e., [10, 11, 12], have the same array shape, then

element wise matching is used. As an example, we can obtain the 10% tail

for 10 d.o.f., the 5% tail for 11 d.o.f. and the 1% tail for 12 d.o.f.

by calling

但是如果概率数组,如[0.1,0.05,0.01]与自由度数组,如[10,11,12]具有相同的数组形态,则元素对应捕捉被作用,我们可以分别得到10%,5%,1%尾的临界值对于10,11,12的自由度。

stats.t.isf([0.1, 0.05, 0.01], [10, 11, 12])

array([ 1.37218364, 1.79588482, 2.68099799])

Specific Points for Discrete Distributions

离散分布的特殊之处

Discrete distribution have mostly the same basic methods as the

continuous distributions. However pdf is replaced the probability mass

function pmf, no estimation methods, such as fit, are available, and

scale is not a valid keyword parameter. The location parameter, keyword

loc can still be used to shift the distribution.

离散分布的简单方法大多数与连续分布很类似。当然像pdf被更换为密度函数pmf,没有估计方法,像fit是可用的。而scale不是一个合法的关键字参数。Location参数,关键字loc则仍然可以使用用于位移。

The computation of the cdf requires some extra attention. In the case of

continuous distribution the cumulative distribution function is in most

standard cases strictly monotonic increasing in the bounds (a,b) and

has therefore a unique inverse. The cdf of a discrete distribution,

however, is a step function, hence the inverse cdf, i.e., the percent

point function, requires a different definition:

ppf(q) = min{x : cdf(x) = q, x integer}

Cdf的计算要求一些额外的关注。在连续分布的情况下,累积分布函数在大多数标准情况下是严格递增的,所以有唯一的逆。而cdf在离散分布,无论如何,是阶跃函数,所以cdf的逆,分位点函数,要求一个不同的定义:

ppf(q) = min{x : cdf(x) = q, x integer}

For further info, see the docs here.

为了更多信息可以看这里。

We can look at the hypergeometric distribution as an example

from scipy.stats import hypergeom

[M, n, N] = [20, 7, 12]

我们可以看这个超几何分布的例子

from scipy.stats import hypergeom

[M, n, N] = [20, 7, 12]

If we use the cdf at some integer points and then evaluate the ppf at

those cdf values, we get the initial integers back, for example

如果我们使用在一些整数点使用cdf,它们的cdf值再作用ppf会回到开始的值。

x = np.arange(4)*2

x

array([0, 2, 4, 6])

prb = hypergeom.cdf(x, M, n, N)

prb

array([ 0.0001031991744066, 0.0521155830753351, 0.6083591331269301,

0.9897832817337386])

hypergeom.ppf(prb, M, n, N)

array([ 0., 2., 4., 6.])

If we use values that are not at the kinks of the cdf step function, we get the next higher integer back:

如果我们使用的值不是cdf的函数值,则我们得到一个更高的值。

hypergeom.ppf(prb + 1e-8, M, n, N)

array([ 1., 3., 5., 7.])

hypergeom.ppf(prb - 1e-8, M, n, N)

array([ 0., 2., 4., 6.])

什么是标准正态分布的上α分位点以及怎样求?

标准正态分布的上α分位点:设X~N(0,1),对于任给的α,(0α1),称满足P(XZα)= α的点Zα为标准正态分布的上α分位点。

当α=0.01时。1- α=0.99。在标准正态分布表中函数值。

中找到最接近0.99的值:0.9898与0.9901,对应的x值分。

别为2.32与2.33,故可取其算术平均值为上0.01分位点。

zα=2.325;

同理:α=0.003,1- α=0.097,zα=2.75,

α/2=0.0015,1-α/2 =0.09985,zα/2=2.96。

分位点可以查正态分布表,在正态分布表中找α,对应查出Zα.例如查Z0.025的值,即需要查1-0.025=0.975对应的Z值,翻开正态分布表,刚好能查到0.9750对应的Z值为1.96,故Z0.025=1.96 。

如果要查Zα=1.96对应的α值,需要先查1.96,对应着0.975,1-0.975=0.025,0.0125即为α值。

扩展资料

标准正态分布的特点

平均值与它的众数以及中位数同一数值。

函数曲线下68.268949%的面积在平均数左右的一个标准差范围内。

正态分布具有两个参数μ和σ^2的连续型随机变量的分布,第一参数μ是遵从正态分布的随机变量的均值,第二个参数σ2是此随机变量的方差,所以正态分布记作N(μ,σ^2)。

标准正态分布曲线下面积分布规律是:在-1.96~+1.96范围内曲线下的面积等于0.9500,在-2.58~+2.58范围内曲线下面积为0.9900。

由于正态分布是由其平均数μ和标准差σ唯一决定的,因此从某种意义上说,正态分布就有好多好多,这给我们深入研究带来一定的困难。

但也发现,许多正态分布中,重点研究N(0,1),其他的正态分布都可以通过转化为N(0,1),我们把N(0,1)称为标准正态分布,其密度函数为,x∈(-∞,+∞),从而使正态分布的研究得以简化。

参考资料来源:百度百科-标准正态分布

如何用python编写一个求分段函数的值的程序

1、首先打开python的编辑器软件,编辑器的选择可以根据自己的喜好,之后准备好一个空白的python文件:

2、接着在空白的python文件上编写python程序,这里假设当x>1的时候,方程为根号下x加4,当x-1时,方程为5乘以x的平方加3。所以在程序的开始需要引入math库,方便计算平方和开方,之后在函数体重写好表达式就可以了,最后调用一下函数,将结果打印出来:

3、最后点击软件内的绿色箭头,运行程序,在下方可以看到最终计算的结果,以上就是python求分段函数的过程:

python之KS曲线

# 自定义绘制ks曲线的函数

def plot_ks(y_test, y_score, positive_flag):

# 对y_test,y_score重新设置索引

y_test.index = np.arange(len(y_test))

#y_score.index = np.arange(len(y_score))

# 构建目标数据集

target_data = pd.DataFrame({'y_test':y_test, 'y_score':y_score})

# 按y_score降序排列

target_data.sort_values(by = 'y_score', ascending = False, inplace = True)

# 自定义分位点

cuts = np.arange(0.1,1,0.1)

# 计算各分位点对应的Score值

index = len(target_data.y_score)*cuts

scores = target_data.y_score.iloc[index.astype('int')]

# 根据不同的Score值,计算Sensitivity和Specificity

Sensitivity = []

Specificity = []

for score in scores:

    # 正例覆盖样本数量与实际正例样本量

    positive_recall = target_data.loc[(target_data.y_test == positive_flag) (target_data.y_scorescore),:].shape[0]

    positive = sum(target_data.y_test == positive_flag)

    # 负例覆盖样本数量与实际负例样本量

    negative_recall = target_data.loc[(target_data.y_test != positive_flag) (target_data.y_score=score),:].shape[0]

    negative = sum(target_data.y_test != positive_flag)

    Sensitivity.append(positive_recall/positive)

    Specificity.append(negative_recall/negative)

# 构建绘图数据

plot_data = pd.DataFrame({'cuts':cuts,'y1':1-np.array(Specificity),'y2':np.array(Sensitivity),

                          'ks':np.array(Sensitivity)-(1-np.array(Specificity))})

# 寻找Sensitivity和1-Specificity之差的最大值索引

max_ks_index = np.argmax(plot_data.ks)

plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y1.tolist()+[1], label = '1-Specificity')

plt.plot([0]+cuts.tolist()+[1], [0]+plot_data.y2.tolist()+[1], label = 'Sensitivity')

# 添加参考线

plt.vlines(plot_data.cuts[max_ks_index], ymin = plot_data.y1[max_ks_index],

          ymax = plot_data.y2[max_ks_index], linestyles = '--')

# 添加文本信息

plt.text(x = plot_data.cuts[max_ks_index]+0.01,

        y = plot_data.y1[max_ks_index]+plot_data.ks[max_ks_index]/2,

        s = 'KS= %.2f' %plot_data.ks[max_ks_index])

# 显示图例

plt.legend()

# 显示图形

plt.show()

# 调用自定义函数,绘制K-S曲线

plot_ks(y_test = y_test, y_score = y_score, positive_flag = 1)


网页标题:python分位点函数 python求分位数函数
分享URL:http://bjjierui.cn/article/hjhdcg.html

其他资讯