当再现this cross-validation example时,我得到2x4列车矩阵(xtrain)和1 000 000的len(b.get_support())。这是否意味着模型中已创建了1 000 000个特征?或者只有2,因为有影响的功能的数量是2.谢谢!
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.cross_validation import cross_val_score, KFold
from sklearn.linear_model import LinearRegression
### create data
def hidden_model(x):
#y is a linear combination of columns 5 and 10...
result = x[:, 5] + x[:, 10]
#... with a little noise
result += np.random.normal(0, .005, result.shape)
return result
def make_x(nobs):
return np.random.uniform(0, 3, (nobs, 10 ** 6))
x = make_x(20)
y = hidden_model(x)
scores = []
clf = LinearRegression()
for train, test in KFold(len(y), n_folds=5):
xtrain, xtest, ytrain, ytest = x[train], x[test], y[train], y[test]
b = SelectKBest(f_regression, k=2)
b.fit(xtrain,ytrain)
xtrain = xtrain[:, b.get_support()] #get_support: get mask or integer index of selected features
xtest = xtest[:, b.get_support()]
print len(b.get_support())
clf.fit(xtrain, ytrain)
scores.append(clf.score(xtest, ytest))
yp = clf.predict(xtest)
plt.plot(yp, ytest, 'o')
plt.plot(ytest, ytest, 'r-')
plt.xlabel('Predicted')
plt.ylabel('Observed')
print("CV Score (R_square) is", np.mean(scores))
答案 0 :(得分:3)
它代表可以应用于x
的掩码,以获取使用SelectKBest
例程选择的功能。
print x.shape
print b.get_support().shape
print np.bincount(b.get_support())
输出:
(20, 1000000)
(1000000,)
[999998 2]
其中显示了20个1000000维数据的例子,一个长度为1000000的布尔数组,其中只有两个是1。
希望有所帮助!