概率与数理统计---练习题

题目:

某次数学考试中,A、B两组的考试成绩如下:
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
求上面实例对应的各种集中量数,手写python函数求解对应量数值

#中位数

def zhongweishu(x):
    x.sort()
    if len(x)%2 == 0:
        i = int(len(x)/2-1)
        t = (x[i] +x[i+1])/2
        print(t)
    else:
        i = int(len(x)/2)
        print(x[i])
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
zhongweishu(a)
zhongweishu(b)

#众数

def zhongshu(x): #用集合set函数
    unique_x = set(x)
    t = 0 #记录出现最多频率的下标
    max = 0 #记录出现最多的次数
    for i in unique_x:
        count = 0
        for item in x:
            if i == item:
                count += 1
        if max < count:
            max = count
            t = i
    print(max)
a = [1,2,3,3,3,3]
zhongshu(a)

#几何平均数

import math
def jihepingjunshu(x):
    s = 1
    for item in x:
        s *= item
    s =s**(1/len(x))
    print(s)
 
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [3,3,3,3,3]
jihepingjunshu(a)
jihepingjunshu(b)
jihepingjunshu(c)

#平均差

import math
def pingjuncha(x):
    s = 0
    for item in x:
        s += item
    s = s/len(x)
    t = 0
    for item in x:
        t += abs(s-item)
    t = t/len(x)
    print(t)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [3,3,3,3,3]
pingjuncha(a)
pingjuncha(b)
pingjuncha(c)

#调和平均值

import math
def tiaohepingjunzhi(x):
    s = 0
    for item in x:
        s += 1/item
    s =len(x)/s
    print(s)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [3,3,3,3,3]
tiaohepingjunzhi(a)
tiaohepingjunzhi(b)
tiaohepingjunzhi(c)

#标准差

import math
def biaozhuncha(x):
    s = 0
    for item in x:
        s += item
    s = s/len(x)
    t = 0
    for item in x:
        t += (s-item)**2
    t = t/len(x)
    t = t ** (1/2)
    print(t)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [1,2,3]
biaozhuncha(a)
biaozhuncha(b)
biaozhuncha(c)

#全距

import math
def quanju(x):
    max = x[0]
    min = x[0]
    for item in x:
        if item > max:
            max = item
        if item < min:
            min = item
    print(min)
    print(max)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [1,2,3]
quanju(a)
quanju(b)
quanju(c)

#偏度

import math
def piandu(x):
    s = 0
    for item in x:
        s += item
    s = s/len(x)
    t = 0
    k = 0
    for item in x:
        t += (item-s)**3
        k += (item-s)**2
    t = t/len(x)
    k = k/len(x)
    k = k**(3/2) 
    print(t/k)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [1,2,3]
piandu(a)
piandu(b)
piandu(c)

#峰度

import math
def fengdu(x):
    s = 0
    for item in x:
        s += item
    s = s/len(x)
    t = 0
    k = 0
    for item in x:
        t += (item-s)**4
        k += (item-s)**2
    k = k/len(x)
    t = t/len(x)
    k = k**(2) 
    print(t)
    print(k)
    print(t/k)
        
c = [1,2,3]
fengdu(c)

#标准分数#归一化

import math
def biaozhunfenshu(x):#归一化
    y =[]
    s = 0
    for item in x:
        s += item
    s = s/len(x)#平均值
    t = 0
    for item in x:
        t += (s-item)**2
    t = t/len(x)
    t = t ** (1/2)#标准差
    for item in x:
        k = (item -s)/t#去中心化
        y.append(k)
    print(y)
        
a = [60,70,65,90,81,97,88,63,45]
b = [3,5,7]
c = [1,2,3]
biaozhunfenshu(a)
biaozhunfenshu(b)
biaozhunfenshu(c)


#全距归一化

import math
def quzhongxinhua(x):
    y = []
    max = x[0]
    min = x[0]
    for item in x:
        if item > max:
            max = item
        if item < min:
            min = item
    for item in x:
        k = (item - min)/(max-min)
        y.append(k)
    print(y)
        
a = [60,70,65,90,81,97,88,63,45]
b = [85,67,75,80,91,100,68,53,77]
c = [1,2,3]
quzhongxinhua(a)
quzhongxinhua(b)
quzhongxinhua(c)

#方差

import math  
def pingjunzhi(x):  
    s = 0  
    for item in x:  
        s += item  
    s = s/len(x)    
    return s  
    
def fangcha(x):   
    s = pingjunzhi(x) 
    t = 0  
    for item in x:  
        t += (s-item)**2 
    t = t/len(x)  
    return t


#协方差

import math  
def pingjunzhi(x):  
    s = 0  
    for item in x:  
        s += item  
    s = s/len(x)    
    return s  
    
def fangcha(x):   
    s = pingjunzhi(x) 
    t = 0  
    for item in x:  
        t += (s-item)**2 
    t = t/len(x)  
    return t  
     
def xiefangcha(x,y):
    s = 0
    i = 0
    for i in range(len(x)):
        s+=(x[i]-pingjunzhi(x))*(y[i]-pingjunzhi(y))
    return s/len(x)


#协方差系数

import math  
def pingjunzhi(x):  
    s = 0  
    for item in x:  
        s += item  
    s = s/len(x)    
    return s  
    
def fangcha(x):   
    s = pingjunzhi(x) 
    t = 0  
    for item in x:  
        t += (s-item)**2 
    t = t/len(x)  
    return t  
     
def xiefangcha(x,y):
    s = 0
    i = 0
    for i in range(len(x)):
        s+=(x[i]-pingjunzhi(x))*(y[i]-pingjunzhi(y))
    return s/len(x)
 
def xiangguanxingxishu(x,y):
    biaozhuncha_x = fangcha(x)**(1/2)
    biaozhuncha_y = fangcha(y)**(1/2)
    return xiefangcha(x,y)/biaozhuncha_x/biaozhuncha_y
    
a = [1,2,3,4,5]  
b = [2,3,4,5,9]

print(xiefangcha(a,b))
print(xiangguanxingxishu(a,b))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值