Python 在SelectKBest中,get_support()的长度代表什么?
复制时,我得到2x4列矩阵(xtrain)的len(b.get_support())为1000 000。这是否意味着已在模型中创建了1000 000个功能?或者只有2个,因为具有影响的功能的数量是2。谢谢Python 在SelectKBest中,get_support()的长度代表什么?,python,scikit-learn,linear-regression,Python,Scikit Learn,Linear Regression,复制时,我得到2x4列矩阵(xtrain)的len(b.get_support())为1000 000。这是否意味着已在模型中创建了1000 000个功能?或者只有2个,因为具有影响的功能的数量是2。谢谢 %matplotlib inline import numpy as np import matplotlib.pyplot as plt from sklearn.feature_selection import SelectKBest, f_regression from sklear
%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))
它表示可应用于
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
希望有帮助 它表示可应用于
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
希望有帮助