x_tr = SelectKBest(chi2, k=25).fit_transform(x_tr,y_tr)
x_ts = SelectKBest(chi2, k=25).fit_transform(x_ts, y_ts)
這是我的代碼。我擔心它會為訓練和測驗資料選擇不同的特征。我應該更改代碼還是會提供相同的功能?
uj5u.com熱心網友回復:
簡短回答:您將獲得不同的功能(除非您很幸運)。
為什么?基本上,因為您從不同的資料中獲取資訊:
x_tr = SelectKBest(chi2, k=25).fit_transform(x_tr,y_tr)
x_ts = SelectKBest(chi2, k=25).fit_transform(x_ts, y_ts)
在第一行中,您從x_tr和獲取功能y_tr;在第二行中,您從x_ts和獲取特征y_ts。因此,您獲得的輸出不同是有道理的。綜上所述,如果輸入不同,輸出也很可能不同。
您將獲得相同特征的唯一情況是當訓練和測驗資料超級同質并且它們隱藏完全相同的資訊時。在您的情況下,您要求 25 個功能,并且在每個代碼中獲得完全相同的 25 個功能將是非常困難的。
如果要應用轉換,請使用以下代碼:
select = SelectKBest(score_func=chi2, k=25) #We define the model by using SelectKBest class
x_tr_selected = select.fit_transform(x_tr ,y_tr) #We fit the class using x_tr and y_tr. And we transform x_tr
x_ts_selected = select.transform(x_ts) #We only transform the data x_ts with the information we obtain in the previous fit
uj5u.com熱心網友回復:
要獲得相同的特征,您必須擬合訓練資料,然后轉換訓練和測驗資料。
select = SelectKBest(chi2, k=25).fit(x_tr, y_tr)
X_train_new = select.transform(x_tr)
X_test_new = select.transform(x_ts)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329463.html
