【Pythonサードパーティバッグ】scikit-learn

22999 ワード

KFoldとStratifiedKFoldの違い
class sklearn.model_selection.StratifiedKFold(n_splits=3, shuffle=False, random_state=None) Stratified K-Folds cross-validator Provides train/test indices to split data in train/test sets.This cross-validation object is a variation of KFold that returns stratified folds. The folds are made by preserving the percentage of samples for each class
Stra~は、種別別ラベルの相対占有比によって行われる分割である
コードを見て
import sklearn.model_selection as skmodsel

print('   100   ,X       ,y       ')
X = [[i] for i in range(100)]
y = ['a'] * 30 + ['b'] * 30 + ['c'] * 30 + ['d'] * 10

print(' KFold    ')
K_folds = skmodsel.KFold(n_splits=10)
for train_indices, test_indices in K_folds.split(X):
    print('test set:', test_indices)
    d = {}
    for i in train_indices:
        d[y[i]] = d.setdefault(y[i], 0) + 1
    print(d)

K_strafold = skmodsel.StratifiedKFold(n_splits=10)
print(' StratifiedKFold    ')
for train_indices, test_indices in K_strafold.split(X, y):
    print('test set:', test_indices)
    d = {}
    for i in train_indices:
        d[y[i]] = d.setdefault(y[i], 0) + 1
    print(d)


結果は次のとおりです.
   100   ,X       ,y       
 KFold    
test set: [0 1 2 3 4 5 6 7 8 9]
{'a': 20, 'b': 30, 'c': 30, 'd': 10}
test set: [10 11 12 13 14 15 16 17 18 19]
{'a': 20, 'b': 30, 'c': 30, 'd': 10}
test set: [20 21 22 23 24 25 26 27 28 29]
{'a': 20, 'b': 30, 'c': 30, 'd': 10}
test set: [30 31 32 33 34 35 36 37 38 39]
{'a': 30, 'b': 20, 'c': 30, 'd': 10}
test set: [40 41 42 43 44 45 46 47 48 49]
{'a': 30, 'b': 20, 'c': 30, 'd': 10}
test set: [50 51 52 53 54 55 56 57 58 59]
{'a': 30, 'b': 20, 'c': 30, 'd': 10}
test set: [60 61 62 63 64 65 66 67 68 69]
{'a': 30, 'b': 30, 'c': 20, 'd': 10}
test set: [70 71 72 73 74 75 76 77 78 79]
{'a': 30, 'b': 30, 'c': 20, 'd': 10}
test set: [80 81 82 83 84 85 86 87 88 89]
{'a': 30, 'b': 30, 'c': 20, 'd': 10}
test set: [90 91 92 93 94 95 96 97 98 99]
{'a': 30, 'b': 30, 'c': 30}
 StratifiedKFold    
test set: [ 0  1  2 30 31 32 60 61 62 90]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [ 3  4  5 33 34 35 63 64 65 91]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [ 6  7  8 36 37 38 66 67 68 92]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [ 9 10 11 39 40 41 69 70 71 93]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [12 13 14 42 43 44 72 73 74 94]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [15 16 17 45 46 47 75 76 77 95]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [18 19 20 48 49 50 78 79 80 96]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [21 22 23 51 52 53 81 82 83 97]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [24 25 26 54 55 56 84 85 86 98]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}
test set: [27 28 29 57 58 59 87 88 89 99]
{'a': 27, 'b': 27, 'c': 27, 'd': 9}

私たちが設定したのは10割引で、つまりaを10部に分けて、毎回1部取ってtestセットを作る結果は明らかで、現在100個のサンプルがあると仮定して、カテゴリラベルはa,b,cの各30個で、10個のラベルはdのサンプルで、彼らの割合は3:3:3:1でKFoldを直接使う時、0~9個目のサンプルは第1割引として、第2回は10-19を第2割引として、このように押します.各折り返し内のサンプル分布は考慮されないが、Stra~を使用する場合、折り返しの場合はサンプル分布を考慮し、各折り返し内の各サンプルの割合がすべてのデータと一致することを保証する
SVMのdecision_funcとpredit_proba
デフォルトでは、sklearnパッケージのsvmメソッドにはpredit_がありません.probaメソッドの、つまりデフォルトでは、あるサンプルの予測に対する後験確率を得ることはできません.svmには独自のdecision_があります.funcメソッドは、次のようになります.
import numpy as np
from sklearn import svm

X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 2])
svm1 = svm.SVC(kernel='linear', probability=False, random_state=random_state)
svm1.fit(X, y)
print(svm1.decision_function(X))
#output
#[-1.  -1.5  1.   1.5]

#   multi-class  
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1], [0, 0], [0, 0.1]])
y = np.array([1, 1, 2, 2, 3, 3])
svm1 = svm.SVC(kernel='linear', probability=True, random_state=random_state)
svm1.fit(X, y)
print(svm1.decision_function(X))
#  
#[[ 2.18181818 -0.35863636  1.17681818]
#[ 2.31818182 -0.495       1.17681818]
#[-0.36363636  2.16863636  1.195     ]
#[-0.5         2.305       1.195     ]
#[ 0.90909091 -0.095       2.18590909]
#[-0.10454545  0.91772727  2.18681818]]

2つの問題であればdecision_functionが出力するのは1次元であり、複数の問題であればdecision_と出力される次元です.function_Shapeこのパラメータについては、SVM-apiを参照してください.また、上記の例のような2つの問題であれば、あるサンプルのdecision_function出力が負であれば,このサンプルはクラス番号の小さいクラス,すなわち1に分けるべきである.
SVCのパラメータprobability=Trueを持つとsvmにもpredit_が付きますprobaメソッドは、次のようになります.
from sklearn import svm

X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 2])
svm = svm.SVC(kernel='linear', probability=True, random_state=random_state)
svm.fit(X, y)
print(svm.predict_proba(X))
#  
# [[0.71303947 0.28696053]
#  [0.79661758 0.20338242]
#  [0.28696001 0.71303999]
#  [0.20335122 0.79664878]]

predict_probaこの方法の出力はdecision_に基づいているfuncの出力、Platt scalingを用いて算出されたもので、詳細はSVM-apiのAttributesのprobaA_を参照とprobaB_
PCA
explained_variance_つまりフィーチャー値
import numpy as np
from sklearn.decomposition import PCA
X = np.array([(2.5,2.4), (0.5,0.7), (2.2,2.9), (1.9,2.2), (3.1,3.0), (2.3, 2.7), (2, 1.6), (1, 1.1), (1.5, 1.6), (1.1, 0.9)])
pca = PCA(n_components=1)
pca.fit(X)

print(pca.components_)
print(pca.explained_variance_) 

#[[-0.6778734  -0.73517866]]
#[1.28402771]