gpt4 book ai didi

python - sklearn选择Kbest : which variables were chosen?

转载 作者:太空狗 更新时间:2023-10-29 17:34:49 24 4
gpt4 key购买 nike

我正在尝试让 sklearn 为线性回归选择最佳的 k 个变量(例如 k=1)。这行得通,我可以获得 R 平方,但它没有告诉我哪些变量是最好的。我怎样才能找到它?

我有以下形式的代码(真正的变量列表要长得多):

X=[]
for i in range(len(df)):
X.append([averageindegree[i],indeg3_sum[i],indeg5_sum[i],indeg10_sum[i])


training=[]
actual=[]
counter=0
for fold in range(500):
X_train, X_test, y_train, y_test = crossval.train_test_split(X, y, test_size=0.3)
clf = LinearRegression()
#clf = RidgeCV()
#clf = LogisticRegression()
#clf=ElasticNetCV()

b = fs.SelectKBest(fs.f_regression, k=1) #k is number of features.
b.fit(X_train, y_train)
#print b.get_params

X_train = X_train[:, b.get_support()]
X_test = X_test[:, b.get_support()]


clf.fit(X_train,y_train)
sc = clf.score(X_train, y_train)
training.append(sc)
#print "The training R-Squared for fold " + str(1) + " is " + str(round(sc*100,1))+"%"
sc = clf.score(X_test, y_test)
actual.append(sc)
#print "The actual R-Squared for fold " + str(1) + " is " + str(round(sc*100,1))+"%"

最佳答案

您需要使用 get_support:

features_columns = [.......]
fs = SelectKBest(score_func=f_regression, k=5)
print zip(fs.get_support(),features_columns)

关于python - sklearn选择Kbest : which variables were chosen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21471513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com