首页 > 解决方案 > Best model for variable selection with big data?

问题描述

I posted a question earlier about some code but now I realize I should be more broad with the general idea. Basically, I'm trying to build a statistical model with about 1000 observations and 2000 variables. I would like to determine which variables are most influential in effecting my dependent variable with high significance. I don't plan to use the model for prediction, just for variable selection. My independent variables are binary and dependent variable is continuous. I've tried multiple linear regression and fixed models with tools such as statsmodels and scikit-learn. However, I have encountered issues such as having more variables than observations. I would prefer to solve the problem in python since I have basic knowledge in it. However, stats is very new to me so I don't know the best direction. Any help is appreciated.

Tree method

import pandas as pd
from sklearn import tree
from sklearn import preprocessing

data=pd.read_excel('data_file.xlsx')

y=data.iloc[:, -1]
X=data.iloc[:, :-1]

le=preprocessing.LabelEncoder()
y=le.fit_transform(y)

clf=tree.DecisionTreeClassifier()
clf=clf.fit(X,y)

tree.export_graphviz(clf, out_file='tree.dot')

Or if I output to text file, the first few lines are:

digraph Tree {
node [shape=box] ;
0 [label="X[685] <= 0.5\ngini = 0.995\nsamples = 1097\nvalue = [2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1\n1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1\n1, 1, 1, 8, 1, 1, 3, 1, 2, 1, 1, 1, 2, 1\n1, 1, 1, 2, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1\n1, 1, 1, 1, 1, 1, 1, 1, 3, 2, 4, 1, 1, 1\n1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1\n1, 3, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2\n1, 1, 1, 1, 1, 1, 30, 3, 1, 3, 1, 1, 2, 1\n1, 5, 1, 2, 1, 4, 2, 1, 1, 1, 1, 1, 1, 1\n1, 1, 2, 1, 1, 1, 3, 1, 1, 3, 1, 2, 1, 1\n1, 7, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1\n6, 2, 1, 2, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1\n1, 1, 1, 1,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scikit-learn.org/stable/modules/feature_selection.html

这里总结了上面的评论。

2.Variante B:线性回归的特征选择

如果您对其运行线性回归,您还可以读取您的特征重要性。https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html。该函数reg.coef_会给你未来的系数,绝对数越高,你的特征越重要,所以例如 0.8 是一个非常重要的未来,而 0.00001 并不重要。

3.Variante C:PCA(不适用于二进制情况)

为什么你想杀死你的变量?我建议您使用:PCA - Principal ocmponent analysis https://en.wikipedia.org/wiki/Principal_component_analysis

基本概念是将您的 2000 个特征转换为更小的空间(可能是 1000 个或其他),同时在数学上仍然有用。

Scikik-learn有一个很好的包:https ://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html


推荐阅读