日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

可汉学院python_A可汗学院-统计学python实现1-10

發(fā)布時(shí)間:2023/12/15 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可汉学院python_A可汗学院-统计学python实现1-10 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【第 1集】 均值 中位數(shù) 眾數(shù)平均數(shù)(Mean):指在一組數(shù)據(jù)中所有數(shù)據(jù)之和再除以這組數(shù)據(jù)的個(gè)數(shù)。

中位數(shù)(Median):按順序排列的一組數(shù)據(jù)中居于中間位置的數(shù),如果觀察值有偶數(shù)個(gè),通常取最中間的兩個(gè)數(shù)值的平均數(shù)作為中位數(shù)。

眾數(shù)(Mode):一組數(shù)據(jù)中出現(xiàn)次數(shù)最多的數(shù)值,有時(shí)眾數(shù)在一組數(shù)中有好幾個(gè)。

import numpy as np

from scipy import stats

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

nums1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 8]

print(np.mean(nums))# 均值 5.5

print(np.median(nums))# 中位數(shù) 5.5

print(np.median(nums1))# 中位數(shù) 6.0

print(stats.mode(nums)[0][0]) #沒有眾數(shù)

print(stats.mode(nums1)) #眾數(shù) 8

【第 2集】 極差 中程數(shù)極差:其最大值與最小值之間的差距,即最大值減最小值后所得之?dāng)?shù)據(jù)

中程數(shù)(Mid-range)是一組統(tǒng)計(jì)數(shù)據(jù)值的最大值和最小值的平均數(shù)

import numpy as np

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(np.ptp(nums)) #極差 9

print(np.ptp(nums)/2) #中程數(shù) 4.5

【第 3集】 象形統(tǒng)計(jì)圖

象形統(tǒng)計(jì)圖:表現(xiàn)統(tǒng)計(jì)數(shù)字大小和變動(dòng)的各種圖形總稱。其中有條形統(tǒng)計(jì)圖、扇形統(tǒng)計(jì)圖、折線統(tǒng)計(jì)圖、象形圖等。

O-型血人數(shù):2×8=16

【第 4集】 條形圖

條形統(tǒng)計(jì)圖:是用一個(gè)單位長度表示一定的數(shù)量,根據(jù)數(shù)量的多少畫成長短不同的直條,然后把這些直條按一定的順序排列起來。從條形統(tǒng)計(jì)圖中很容易看出各種數(shù)量的多少條形圖:參考https://blog.csdn.net/hohaizx/article/details/79101322

import matplotlib.pyplot as plt

import matplotlib

# 設(shè)置中文字體和負(fù)號正常顯示

matplotlib.rcParams['font.sans-serif'] = ['SimHei']

matplotlib.rcParams['axes.unicode_minus'] = False

label_list = ['Jasmine', 'Jeff', 'nevin', 'Alejandrn', 'Marta'] # 橫坐標(biāo)刻度顯示值

list1 = [72, 86, 82, 81, 96] # 縱坐標(biāo)值1

list2 = [78, 84, 88, 95, 90] # 縱坐標(biāo)值2

x = np.arange(len(list1))

"""

繪制條形圖

left:長條形中點(diǎn)橫坐標(biāo)

height:長條形高度

width:長條形寬度,默認(rèn)值0.8

label:為后面設(shè)置legend準(zhǔn)備

"""

rects1 = plt.bar(x=x, height=list1, width=0.4, alpha=0.4, color='blue', label="期中考試")

rects2 = plt.bar(x=x+0.4, height=list2, width=0.4, alpha=0.5, color='red', label="期末考試")

plt.ylim(0, 110) # y軸取值范圍

plt.ylabel("成績")

"""

設(shè)置x軸刻度顯示值

參數(shù)一:中點(diǎn)坐標(biāo)

參數(shù)二:顯示值

"""

plt.xticks(x+0.2, label_list)

plt.xlabel("名字")

plt.title("成績單")

plt.legend() # 設(shè)置題注

# 編輯文本

for rect in rects1:

height = rect.get_height()

plt.text(rect.get_x() + rect.get_width() / 2, height+1, str(height),

ha="center", va="bottom")

for rect in rects2:

height = rect.get_height()

plt.text(rect.get_x() + rect.get_width() / 2, height+1, str(height),

ha="center", va="bottom")

plt.show()

【第 5集】 線形圖

線形圖又稱為“點(diǎn)狀圖”(point chart)、“停頓圖”(Stopping chart)或“星狀圖”(star chart)。線形圖屬于圖表分析的一類,僅記錄收盤價(jià),至於開盤價(jià)、當(dāng)日最高價(jià)、當(dāng)日最高的變動(dòng)及波動(dòng)幅度則欠缺。以線形圖捕捉長期趨勢還可以,但卻難于捕捉短線和中線趨勢,目前已較少有人使用。

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']

#X軸,Y軸數(shù)據(jù)

x = [0, 1, 2, 3, 4, 5, 6, 7, 8]

y = [6.59, 6.70, 6.95, 7.30, 7.30, 7.37, 7.1, 7.51, 7.58]

plt.figure(figsize=(8, 4)) # 創(chuàng)建繪圖對象

plt.style.use('ggplot') # 網(wǎng)格

plt.plot(x, y, "b--", linewidth=1) #(X軸,Y軸,藍(lán)色虛線,線寬度)

plt.xlabel("時(shí)間") #X軸標(biāo)簽

plt.ylabel("股票價(jià)格") #Y軸標(biāo)簽

plt.title("股票價(jià)格-時(shí)間") #圖標(biāo)題

plt.show()

【第 6集】 餅圖

餅圖顯示一個(gè)數(shù)據(jù)系列中各項(xiàng)的大小與各項(xiàng)總和的比例。餅圖中的數(shù)據(jù)點(diǎn)顯示為整個(gè)餅圖的百分比。

每個(gè)季度的收入:

import matplotlib.pyplot as plt

labels = ['quarter1', 'quarter2', 'quarter3', 'quarter4']

sizes = [40, 30, 20, 10]

plt.pie(sizes, labels=labels, autopct='%1.1f%%')

# plt.legend(loc=[0.9, 0.6])

plt.axis('equal')

plt.show()

【第 7集】 誤導(dǎo)人的線形圖

若線形圖的刻度不一樣容易引起誤解,為了不引起誤解,線性圖畫在一張圖中。

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']

#X軸,Y軸數(shù)據(jù)

x = [0, 1, 2, 3, 4, 5, 6, 7, 8]

y = [6.59, 6.70, 6.95, 7.30, 7.30, 7.37, 7.1, 7.51, 7.58]

y1 = [3.295, 3.35, 3.475, 3.65, 4, 3.685, 3.55, 3.755, 3.79]

# print(y1)

plt.figure(figsize=(8, 4)) # 創(chuàng)建繪圖對象

plt.style.use('ggplot') # 網(wǎng)格

plt.plot(x, y, "b--", linewidth=1) #(X軸,Y軸,藍(lán)色虛線,線寬度)

plt.plot(x, y1, "r--", linewidth=1) #(X軸,Y軸,紅色虛線,線寬度)

plt.xlabel("時(shí)間") #X軸標(biāo)簽

plt.ylabel("股票價(jià)格") #Y軸標(biāo)簽

plt.title("股票價(jià)格-時(shí)間") #圖標(biāo)題

plt.show()

【第 8集】 莖葉圖

莖葉圖的思路是將數(shù)組中的數(shù)按位數(shù)進(jìn)行比較,將數(shù)的大小基本不變或變化不大的位作為一個(gè)主干(莖),將變化大的位的數(shù)作為分枝(葉),列在主干的后面,這樣就可以清楚地看到每個(gè)主干后面的幾個(gè)數(shù),每個(gè)數(shù)具體是多少。

上圖是12個(gè)足球隊(duì)員的各自得分,總共得多少分?

Stem:得分十位數(shù)

Leaf:得分個(gè)位數(shù)

總計(jì)得分:(0+0+2+4+7+7+9) + (11+11+11+13+18) + (20)

from itertools import groupby

nums2 = [0, 0, 2, 4, 7, 7, 9, 11, 11, 13, 18, 20]

for k, g in groupby(sorted(nums2), key=lambda x: int(x) // 10):

# print('k', k)

# print('g', list(g))

lst = map(str, [int(y) % 10 for y in list(g)])

print(k, '|', ' '.join(lst))

【第 9集】 箱線圖

顧客距離餐廳遠(yuǎn)近的分布,可以用箱線圖。

箱形圖(Box plot):又稱為盒須圖、盒式圖、盒狀圖或箱線圖,是一種用作顯示一組數(shù)據(jù)分散情況資料的統(tǒng)計(jì)圖。因型狀如箱子而得名。其最大的優(yōu)點(diǎn)就是不受異常值的影響,可以以一種相對穩(wěn)定的方式描述數(shù)據(jù)的離散分布情況。線形圖:適于了解變量隨時(shí)間變化的趨勢,也可以是一個(gè)變量對另外一個(gè)變量的趨勢。

條形圖:適于將事物歸類,看類別的分布情況。

餅圖:適于看到各部分的占比。

莖葉圖:適于了解分布的情況。

箱線圖:適于了解中位數(shù)和散布的情況。作者求解四分位數(shù)的方法是第一種方法

另一種求解箱線圖的的方法:箱線圖中的四分位數(shù)是第二種求解方法

import pandas as pd

import matplotlib.pyplot as plt

from pandas.plotting import table

data = {

'distance': [1, 1, 2, 2, 3, 3, 4, 4, 6, 7, 8, 10, 11, 14, 15, 20, 22]

}

df = pd.DataFrame(data)

fig, ax = plt.subplots(1, 1)

table(ax, np.round(df.describe(), 2),

loc='upper right',

colWidths=[0.2, 0.2]

)

# df.plot.box(title="Consumer spending in each country", vert=False)

df.plot.box(title="customer distance",

ax=ax,

ylim=(0, 30))

plt.grid(linestyle="--", alpha=0.3)

plt.show()

四分位數(shù):有兩種求法不包含中位數(shù),求解四分位數(shù)

包含中位數(shù),求解四分位數(shù)例子:2、4、4、5、6、7、8

第一四分位數(shù)(Q1) = 4

第二四分位數(shù)(Q2) = 5

第三四分位數(shù)(Q3) = 7例子:1、3、3、4、5、6、6、7、8、8

第一四分位數(shù)(Q1) = 3

第二四分位數(shù)(Q2) = 5.5

第三四分位數(shù)(Q3) = 7

import numpy as np

def quartile_t1(data):

'''四分位數(shù)'''

n = len(data)

q1, q2, q3 = None, None, None

if n >= 4:

sorted_data = sorted(data)

q2 = np.median(sorted_data)

if n % 2 == 1:

q1 = np.median(sorted_data[:n//2])

q3 = np.median(sorted_data[n//2 + 1:])

else:

q1 = np.median(sorted_data[:n // 2])

q3 = np.median(sorted_data[n // 2:])

return q1, q2, q3

# 不包含中位數(shù),求解四分位數(shù)

# 包含中位數(shù),求解四分位數(shù)

data1 = [5, 7, 4, 4, 6, 2, 8]

print(quartile_t1(data1)) # 4, 5, 7

print(np.percentile(data1, [25, 50, 75])) # 4. 5. 6.5

data2 = [1, 3, 3, 4, 5, 6, 6, 7, 8, 8]

print(quartile_t1(data2)) # 3, 5.5, 7

print(np.percentile(data2, [25, 50, 75])) # 3.25 5.5 6.75

【第10集】 箱線圖2

上圖是100棵樹的樹齡箱線圖,問樹齡極差是多少,樹齡中位數(shù)是多少?

由圖可以看出:最小值是8,最大值是50,所以極差是50-8=42;中位數(shù)是豎線坐標(biāo)21。

總結(jié)

以上是生活随笔為你收集整理的可汉学院python_A可汗学院-统计学python实现1-10的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。