for循環(huán)的賦值用法,極其精妙
- 一個數(shù)組的每一個值增加500賦值給另一個空數(shù)組
new_list = [ f + 500 for f in old_list] - 一個數(shù)組包含元素的排列順序1,2,3,4,另一個數(shù)組為其內(nèi)容的離散排列數(shù)組,需要得到離散數(shù)組中內(nèi)容在數(shù)組1中編號組成的新數(shù)組。
survey_scale = ["none","a few","some","a lot"]
survey = ["none","some","a lot","a few","some"]
survey_number = [survey_scale.index(item) for item in survey]
Skew數(shù)據(jù)傾斜
-
如果數(shù)據(jù)直方圖集中在圖像右側(cè),被稱為消極傾斜
-
如果數(shù)據(jù)集中在圖像左側(cè),被稱為積極傾斜
image.png -
如果數(shù)據(jù)集中在直方圖中部,被稱為無傾斜
image.png
為傾斜賦值
將數(shù)組數(shù)據(jù)賦值給傾斜對象
from scipy.stats import skew
positive_skew = skew(test_scores_positive)
negative_skew = skew(test_scores_negative)
no_skew = skew(test_scores_normal)
print(negative_skew,positive_skew,no_skew)
output
-0.6093247474592195 0.5376950498203763 0.0223645171350847
kurtosis峰態(tài)
-
直方圖分布均勻,被稱為平峰態(tài)即platykurtic
platykurtic -
直方圖分布出現(xiàn)只有高聳點,所有數(shù)據(jù)幾乎相同,被稱為尖峰態(tài)即leptokurtic
leptokurtic -
直方圖所有值不低也不甚高,叫做常峰態(tài)即mesokurtic
mesokurtic
負值分布代表平峰態(tài),正值分布代表尖峰態(tài),0值附近表示常峰態(tài)
峰態(tài)對象由數(shù)組賦值如下
from scipy.stats import kurtosis
kurt_platy = kurtosis(test_scores_platy)
kurt_lepto = kurtosis(test_scores_lepto)
kurt_meso = kurtosis(test_scores_meso)
print(kurt_platy,kurt_lepto,kurt_meso)
output
-0.9283967256161696 0.023335026722224317 -0.042791859857727044
modaility 模態(tài)
-
直方圖只有一個模式,稱為單峰unimodal
unimodal -
直方圖有兩個模式,稱為雙峰bimodal
bimodal -
直方圖多個模式,稱為多峰multimodal
multimodal
mean()
平均值是數(shù)組所有值求和后除以數(shù)組元素數(shù)量,求平均值plt展示方法,plt.axvline直接畫一條垂直于X軸的線,標注平均值,求數(shù)組平均值則采用array.mean()
下面是求平均值并畫圖
mean_test_score = test_scores_normal.mean()
plt.axvline(mean_test_score)

median 中位數(shù)
中位數(shù)是數(shù)組排序,取中間一個數(shù),或者兩個數(shù)的mean,中位數(shù)對于特大或者特小數(shù)不敏感,即我們稱之為outliers(異常值)的值。
- 求中位數(shù)的方法需要調(diào)用numpy框架
from numpy import median
使用函數(shù)
numpy.median(array)
如下列為求positive數(shù)組平均值與中位數(shù)的對比
plt.hist(test_scores_positive)
positive_median = numpy.median(test_scores_positive)
positive_mean = test_scores_positive.mean()
plt.axvline(positive_median,color = "green")
plt.axvline(positive_mean,color = "red")
plt.show()

方差 variance|標準差 Standard deviation


求標準差的函數(shù)舉例:求nab_stats數(shù)據(jù)表中mp列和ast列的標準差,求方差方法雷同
import numpy
def standard(column):
mean = column.mean()
a = 0
for num in column:
a += (num - mean) ** 2 / len(column)
sigma = a ** (1/2)
return sigma
mp_dev = standard(nba_stats["mp"])
ast_dev = standard(nba_stats["ast"])
print(mp_dev)
print(ast_dev)
output
896.32565278
130.883290708
正態(tài)分布 normal distribution
正態(tài)分布繪制需要依賴幾率密度函數(shù),需要加載norm模塊中的pdf函數(shù)來實現(xiàn),比如繪制從-10到10步進為0.1的圖形內(nèi),平均值為0方差為2的正態(tài)分布圖
import numpy as np
import matplotlib.pyplot as plt
# The norm module has a pdf function (pdf stands for probability density function)
from scipy.stats import norm
points2 = np.arange(-10,10,0.1)
probabilities2 = norm.pdf(points,0,2)
plt.plot(points,probabilities2)
plt.show()

求正態(tài)分布中距離mean多少個標準差內(nèi)的比例問題的方法,思路可以轉(zhuǎn)換為算出每一個值與mean的差值除以標準差,就能夠得到一個所有值距離mean的標準差數(shù)量list,然后直接調(diào)用一個函數(shù)就能夠計算出某個標準差內(nèi)的數(shù)據(jù)比例是多少,具體代碼如下
import numpy
def standard(column): #求標準差的函數(shù)
mean = sum(column)/len(column)
a = 0
for num in column:
a += (num - mean) ** 2 / len(column)
sigma = a ** (1/2)
return sigma
wing_lengths = [36, 37, 38, 38, 39, 39, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 52, 52, 53, 53, 54, 55]
mean = sum(wing_lengths)/len(wing_lengths)
standard_deviation = standard(wing_lengths)
standard_deviations = [(item - mean)/standard_deviation for item in wing_lengths] #關(guān)鍵步驟,求出了每個值跟mean差幾個標準差的數(shù)組
def percentage(column,count): #標準差數(shù)組在某個標準差數(shù)量內(nèi)的比例函數(shù)
num = len([item for item in column if item <= count and item >= -count])/len(wing_lengths)
return num
within_one_percentage = percentage(standard_deviations,1) #求一個標準差內(nèi)的數(shù)據(jù)比例
within_two_percentage = percentage(standard_deviations,2) #求兩個標準差內(nèi)的數(shù)據(jù)比例
within_three_percentage = percentage(standard_deviations,3) #求三個標準差內(nèi)的數(shù)據(jù)比例
print(within_one_percentage,within_two_percentage,within_three_percentage)
output
0.68 0.96 1.0
正相關(guān)positively correlated、負相關(guān)negative correlated、不相關(guān)uncorrelated



使用離散圖畫法可明了觀察出x軸與y軸兩種變量的相關(guān)性
import matplotlib.pyplot as plt
plt.scatter(nba_stats["fga"], nba_stats["pts"])
plt.show()
相關(guān)系數(shù) Pearson's r
需要import pearsonr 框架,求得兩組serices數(shù)據(jù)對應(yīng)的相關(guān)性值,r值取值范圍為[-1,1],越接近[-1,0)為負相關(guān),0為不相關(guān),(0,1] 為正相關(guān)
from scipy.stats.stats import pearsonr
r_fta_pts, p_value = pearsonr(nba_stats["fta"],nba_stats["pts"])
print(r_fta_pts)
r_stl_pf, p_value = pearsonr(nba_stats["stl"],nba_stats["pf"])
print(r_stl_pf)
output
0.918978538402
0.737628216749
協(xié)方差 covariance
兩個變量是相互獨立的,但也有相似的方式。例如,當一個變量上升時,另一個變量也會上升。這就是所謂的協(xié)方差。協(xié)方差指的是不同的數(shù)字是如何相互變化的。
有兩個變量可以相互改變的極限。這是因為每個變量都有自己的方差。這些方差為兩個變量之間的協(xié)方差設(shè)定了最大的理論極限。換句話說,一組變量不能因均值而異,而這兩個變量的均值各不相同。當兩個變量以一種完全相同的方式變化時,兩個變量可以達到最大可能的協(xié)方差(ux是x serices的mean值)
covariance
下面是舉了兩個column數(shù)據(jù)的協(xié)方差求法,先定義了一個求協(xié)方差公式
def covariance(column1,column2):
mean1 = column1.mean()
mean2 = column2.mean()
cov = [(column1[i] -mean1)*(column2[i] - mean2) for i in range(len(column1))] #核心語句,用range迭代生成cov數(shù)組
cov_total = sum(cov)/len(column1)
return cov_total
cov_stl_pf = covariance(nba_stats["stl"],nba_stats["pf"])
cov_fta_pts = covariance(nba_stats["fta"],nba_stats["pts"])
print(cov_stl_pf,cov_fta_pts)
Output
1823.35484805 56618.4139807
r值的實際計算方法
r值是實際協(xié)方差和最大可能正協(xié)方差的比值。
r值
計算標準差最終我們使用std()函數(shù)
nba_stats["pf"].std()
計算協(xié)方差我們是用numpy框架中cov函數(shù)進行計算
cov(nba_stats["pf"], nba_stats["stl"])[0,1]
下面是計算協(xié)方差的一個舉例
from numpy import cov
r_fta_blk = cov(nba_stats["fta"],nba_stats["blk"])[0,1]/ (nba_stats["fta"].std() * nba_stats["blk"].std())#將協(xié)方差和兩組column的標準差代入到公式中,就能得到r的值,即相關(guān)系數(shù)
r_ast_stl = cov(nba_stats["ast"],nba_stats["stl"])[0,1]/ (nba_stats["ast"].std() * nba_stats["stl"].std())
print(r_fta_blk,r_ast_stl)










