加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

统计算法_探索性统计

发布时间:2020-12-17 00:10:14 所属栏目:Python 来源:网络整理
导读:最近不知道写什么了,基本python的各种功能百度一下,都能搜到一大把,最近itchat好像很火,不过对这个不是很感冒,等以后有兴趣或者用的上的时候研究研究准备把统计方面的东西再看看,就写一些简单的统计算法吧,其实这些在python里面都有现成的,但是有句

最近不知道写什么了,基本python的各种功能百度一下,都能搜到一大把,最近itchat好像很火,不过对这个不是很感冒,等以后有兴趣或者用的上的时候研究研究准备把统计方面的东西再看看,就写一些简单的统计算法吧,其实这些在python里面都有现成的,但是有句名言“不要只会用,还要知道原理”(是哪个名人说的?如果没有,那就算我说的了),所以写这些供学习之用。那么从头再来,循序渐进,先写这些,本来想写个对数函数的,结果发现真TM难,如果谁能写出来麻烦告知下,我也膜拜大神一下1、算数平均数2、算数平均回报3、中位数4、众数5、极差6、四分位数7、几何平均数8、几何平均回报

9、对数(没写出来)下面上代码,和往常一样,含说明,为了更容易理解,就不直接用函数了,尽量用最原始的语法写

case_list = [] <span style="color: #008000">#<span style="color: #008000">准备个容器,放样例<span style="color: #008000">

<span style="color: #008000">随机生成10个1到30的小数,不能重复,把10个小数放到容器里

<span style="color: #0000ff">while len(case_list) < 10<span style="color: #000000">:
rand_float = random.uniform(1,30<span style="color: #000000">)
<span style="color: #0000ff">if rand_float <span style="color: #0000ff">in<span style="color: #000000"> case_list:
<span style="color: #0000ff">continue<span style="color: #000000">
case_list.append(rand_float)
case_list = [round(case,2) <span style="color: #0000ff">for case <span style="color: #0000ff">in case_list] <span style="color: #008000">#<span style="color: #008000">格式化一下,不然太长不好看,当然这个因需要而定,我为了显示好看,所以格式化保留两位小数<span style="color: #008000">

<span style="color: #008000">我先执行了个结果

<span style="color: #0000ff">print case_list <span style="color: #008000">#<span style="color: #008000">结果是,[5.77,27.21,27.12,12.65,23.12,25.56,18.6,23.75,4.1,25.13]
case_list[6] = 23.12 <span style="color: #008000">#<span style="color: #008000">为了后面众数,弄个相同值出来
<span style="color: #0000ff">print str(case_list) <span style="color: #008000">#<span style="color: #008000">单独打下面了,方便看
<span style="color: #800000">'<span style="color: #800000">[5.77,25.13]<span style="color: #800000">'<span style="color: #000000">
case_list.sort() <span style="color: #008000">#<span style="color: #008000">排序,这个算法有很多,单独写,不在这里啰嗦<span style="color: #008000">

<span style="color: #008000">先写个累加函数,为了方便,就不try了,默认输入的结果都是对的

<span style="color: #0000ff">def<span style="color: #000000"> sum_fun(xlist):
n =<span style="color: #000000"> 0
<span style="color: #0000ff">for x <span style="color: #0000ff">in<span style="color: #000000"> xlist:
n +=<span style="color: #000000"> x
<span style="color: #0000ff">return<span style="color: #000000"> n
<span style="color: #008000">#<span style="color: #008000">统计数据量函数
<span style="color: #0000ff">def<span style="color: #000000"> len_fun(xlist):
n =<span style="color: #000000"> 0
<span style="color: #0000ff">for x <span style="color: #0000ff">in<span style="color: #000000"> xlist:
n += 1
<span style="color: #0000ff">return<span style="color: #000000"> n
<span style="color: #008000">#<span style="color: #008000">累乘,和累加一样
<span style="color: #0000ff">def<span style="color: #000000"> multiply_fun(xlist):
n = 1
<span style="color: #0000ff">for x <span style="color: #0000ff">in<span style="color: #000000"> xlist:
n *=<span style="color: #000000"> x
<span style="color: #0000ff">return<span style="color: #000000"> n

<span style="color: #008000">#<span style="color: #008000">1、算数平均数:加和/个数
def sum_mean_fun(case_list):? sum_mean_num = sum_fun(case_list)/len_fun(case_list)? return sum_mean_num <span style="color: #008000">#<span style="color: #008000">结果是19.753

<span style="color: #008000">#<span style="color: #008000">2、算数平均数回报,平均回报率计算,
<span style="color: #000000">def sum_mean_rate(case_list):? '((case_list[1]-case_list[0])/case_list(0)+(case_list[2]-case_list[1])/case_list(1)+...(case_list[n]-case_list[n-1])/case_list(n-1))/len(case_list-1)'? n = 1? rate_return = [] #存放回报率? while n < len_fun(case_list):??? rate = (case_list[n] - case_list[n-1])/case_list[n-1]??? rate_return.append(rate)??? n += 1? mean_rate_num = sum_fun(rate_return)/len_fun(rate_return)? return mean_rate_num

<span style="color: #008000">#<span style="color: #008000">3、中位数,list中间的数,如果count是基数index = len(list)/2,如果是偶数index1 = (len(list)/2 index2 = (len(list)/2)+1)
def median_fun(case_list):? if len_fun(case_list)%2 == 1:??? median = case_list[(len_fun(case_list)+1)/2-1]? else:??? median = (case_list[len_fun(case_list)/2-1] + case_list[len_fun(case_list)/2])/2? return median

<span style="color: #008000">#<span style="color: #008000">4、众数,存在最多的数
<span style="color: #000000">def modes_fun(case_list):? case_list_delre = list(set(case_list)) #去重,这个如果自己写的话可以用分治法,有兴趣的话可以自己写个? count_max = 0? for case_part in case_list_delre:??? case_count = case_list.count(case_part)??? if case_count > count_max:????? count_max = case_count????? max_return = case_part? if count_max == 1:??? return None? mode = max_return? return mode

<span style="color: #008000">#<span style="color: #008000">5、极差,最大-最小,因为已经拍好序,所以index min - index max
<span style="color: #000000">def ext_minus_fun(case_list):? ext_minus_num = case_list[len_fun(case_list)-1] - case_list[0]? return ext_minus_num

<span style="color: #008000">#<span style="color: #008000">6、四分位数,箱图用的,可以避免极值的影响,分别是index1 = len(list)/4 index2 = 3len(list)/4
<span style="color: #000000">def four_bit_fun(case_list):? Q1 = case_list[len_fun(case_list)/4]? Q2 = case_list[3
len_fun(case_list)/4]? return Q1,Q2

<span style="color: #008000">#<span style="color: #008000">7、几何平均数,和算数平均数不同,把所有都乘过^(1/len(list))
<span style="color: #000000">def geom_mean_fun(case_list):? geom_mean_num = multiply_fun(case_list) ** (1.0/len_fun(case_list))? return geom_mean_num

<span style="color: #008000">#<span style="color: #008000">8、几何平均回报,R = 回报,((1+R1)(1+R2)...(1+Rn))^1/(len(count(R))-1)
def geom_mean_rate(case_list):? n = 1? rate_return = [] #存放回报率? while n < len_fun(case_list):??? rate = (case_list[n] - case_list[n-1])/case_list[n-1]??? rate_return.append(rate)??? n += 1? rate_return = [1+rate for rate in rate_return]? geom_mean_rate_num = multiply_fun(rate_return) ** (1.0/len_fun(rate_return)) - 1? return geom_mean_rate_num# 验证if name == 'main':? rand_list = create_rand_list(1,30,10)? rand_list.sort()? print rand_list? sum_mean_num = sum_mean_fun(rand_list)? print sum_mean_num? mean_rate_num = sum_mean_rate(rand_list)? print mean_rate_num? median_num = median_fun(rand_list)? print median_num? modes_num = modes_fun(rand_list)? print modes_num? ext_minus_num = ext_minus_fun(rand_list)? print ext_minus_num? four_bit_q1,four_bit_q2 = four_bit_fun(rand_list)? print four_bit_q1? print four_bit_q2? geom_mean_num = geom_mean_fun(rand_list)? print geom_mean_num? geom_mean_rate_num = geom_mean_rate(rand_list)? print geom_mean_rate_num

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读