ks检验正态分布结果_KS检验及其在机器学习中的应用
KS檢驗及其在機器學習中的應用
什么是KS檢驗
Kolmogorov–Smirnov 檢驗,簡稱KS檢驗,是統計學中的一種非參數假設檢驗,用來檢測單樣本是否服從某一分布,或者兩樣本是否服從相同分布。在單樣本的情況下,我們想檢驗這個樣本是否服從某一分布函數,記是該樣本的經驗分布函數。我們構造KS統計量:
如下圖,經驗分布函數與目標分布的累積分布函數的最大差值就是我們要求的KS統計量:
95%置信度的KS統計量的臨界值由給出,如果我們根據樣本得到的KS統計量的值小于,那么我們就接收原假設!否則,拒絕原假設。
兩樣本的KS檢驗
用同樣的思想,我們可以檢驗「兩個樣本是否服從同一分布」,此時KS統計量為兩樣本的經驗分布函數的最大差值
這時候,95%置信度的臨界值為
「KS 檢驗只能檢驗連續型的分布」
import?numpy?as?npimport?matplotlib.pyplot?as?plt
from?scipy.stats?import?kstest,?ks_2samp
from?sklearn?import?datasets
from?sklearn.model_selection?import?train_test_split
from?sklearn.linear_model?import?LogisticRegression
如何用Python進行KS檢驗
Python的scipy.stats模塊提供了與KS檢驗有關的函數
單樣本檢驗
有函數:scipy.stats.kstest(rvs, cdf, args=(), N=20, alternative='two-sided', mode='approx')最重要的兩個參數:
- rvs : str, array or callableIf a string, it should be the name of a distribution in scipy.stats.If an array, it should be a 1-D array of observations of randomvariables.If a callable, it should be a function to generate random variables;it is required to have a keyword argument size.
- cdf : str or callableIf a string, it should be the name of a distribution in scipy.stats.If rvs is a string then cdf can be False or the same as rvs.If a callable, that callable is used to calculate the cdf.```
Returns:
statistic : float
KS test statistic, either D, D+ or D-.
pvalue : float
One-tailed or two-tailed p-value.
x?=?np.random.randn(100)
kstest(x,?'norm')
KstestResult(statistic=0.14648390717722642, pvalue=0.024536061749414313)
生成100個標準正態分布隨機數,得到KS統計量的值為,因此我們認為該樣本服從正態分布。
x?=?np.random.exponential(size=100)kstest(x,?'norm')
KstestResult(statistic=0.505410956721057, pvalue=3.4967106846361894e-24)
kstest(x,?'expon')
KstestResult(statistic=0.09854002120537766, pvalue=0.2685899206780503)
生成100個指數分布隨機數,KS檢驗拒絕它們服從正態分布的假設,接收了它們服從指數分布的假設。
兩樣本檢驗
有函數:scipy.stats.ks_2samp(data1, data2, alternative='two-sided', mode='auto')參數:
- data1, data2 : sequence of 1-D ndarraystwo arrays of sample observations assumed to be drawn from a continuousdistribution, sample sizes can be different`
Returns
statistic : float
KS statistic
pvalue : float
two-tailed p-value
x?=?np.random.randn(100)
y?=?np.random.randn(50)
ks_2samp(x,?y)
Ks_2sampResult(statistic=0.11, pvalue=0.804177768619009)
,因此我們接收原假設,認為x,y服從相同分布。
x?=?np.random.randn(100)y?=?np.random.exponential(size=50)
ks_2samp(x,?y)
Ks_2sampResult(statistic=0.59, pvalue=3.444644569583488e-11)
拒絕x,y服從相同分布的假設。
KS檢驗在機器學習中的應用
應用一:判斷特征在訓練集和測試集上分布是不是相同
特征遷移是在機器學習任務中經常碰到的情況,「線上數據的分布跟離線數據的分布情況不一致」,這就導致模型的泛化能力不足。而我們去判斷兩份數據的分布是不是相同的一個工具就是KS檢驗!
X,?y?=?datasets.make_classification(n_samples=10000,?n_features=5,????????????????????????????????????n_informative=2,?n_redundant=2,?random_state=2020)
X_train,?X_test,?y_train,?y_test?=?\
????train_test_split(X,?y,?test_size=0.4,?random_state=2020)
for?i?in?range(5):
????print(ks_2samp(X_train[:,?i],?X_test[:,?i]))
Ks_2sampResult(statistic=0.013083333333333334, pvalue=1.0)
Ks_2sampResult(statistic=0.013083333333333334, pvalue=1.0)
Ks_2sampResult(statistic=0.008916666666666666, pvalue=1.0)
Ks_2sampResult(statistic=0.012916666666666667, pvalue=1.0)
Ks_2sampResult(statistic=0.013583333333333333, pvalue=1.0)
隨機生成了一個有5個特征,包含10000組數據的數據集,劃分訓練集和測試集后,對比每個特征上測試集和訓練集的分布。這里每一個特征都通過了KS檢驗(這里顯然是可以通過的hhh)
應用二:判斷二分類模型能否將正負樣本很好的分開
在信用評分領域,會使用KS統計量衡量二分類模型分類正負樣本的能力。在測試集上,將模型對y_true=1的樣本的輸出概率值作為data1,對y_true=0的樣本的輸出概率值作為data2,計算兩個分布的KS統計量。我們用 lr 拿上面的數據做個例子。畫出測試集上正負樣本的預測概率值的分布情況。
lr?=?LogisticRegression(solver='liblinear')lr.fit(X_train,?y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, l1_ratio=None, max_iter=100,
multi_class='auto', n_jobs=None, penalty='l2',
random_state=None, solver='liblinear', tol=0.0001, verbose=0,
warm_start=False)
data1?=?np.sort(lr.predict_proba(X_test[y_test==1])[:,?1])
data2?=?np.sort(lr.predict_proba(X_test[y_test==1])[:,?0])
plt.figure(figsize=(8,?4))
last,?i?=?0,?0
while?i?????plt.plot([last,?data1[i]],?[i/len(data1),?i/len(data1)],?'k')
????if?i?????????last?=?data1[i]
????i?+=?1
last,?i?=?0,?0
while?i?????plt.plot([last,?data2[i]],?[i/len(data2),?i/len(data2)],?'r')
????if?i?????????last?=?data2[i]
????i?+=?1
這兩條曲線的最大差值就是我們要求的KS統計量。這個差值越大,說明模型對這個正負樣本的區別能力越強。
ks_2samp(data1,?data2)Ks_2sampResult(statistic=0.9219219219219219, pvalue=0.0)
這里KS統計量甚至超過了0.9,一般來說,KS統計量超過0.6,就說明模型的分類能力比較強了。
贊 賞 作 者
推薦閱讀:2020Python招聘內推渠道開啟啦!
老司機教你5分鐘讀懂Python裝飾器
用Python實現粒子群算法
抄底美股?用Python分析美股實際收益率
▼點擊成為社區會員? ?喜歡就點個在看吧
總結
以上是生活随笔為你收集整理的ks检验正态分布结果_KS检验及其在机器学习中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 制作ppt,PPT制作三个基本要
- 下一篇: python语言基本语句例句-关于pyt