0
$\begingroup$

The following question (this one) did not help me.

I have a big dataset, and I want to know which Columns are the most relevant for the Target Variable. I know that, in my case, for each class in the Target Variable, different Columns have a different impact.

In that question, the suggested answer recomend using LDA. From what I understood, it looks like a normal classification algorithm, so it's not what I need

What I what is something like

In : 
    magic_function("name_of_target_variable_1")
Out :
    ["really_important_column_a", "really_important_column_b" ...]
In : 
    magic_function("name_of_target_variable_2")
Out :
    ["really_important_column_a", "really_important_column_f" ...]

How can I obtain this result? Is there a way, in the first place?

$\endgroup$
  • 1
    $\begingroup$ Feature engineering is a whole thing by itself. You could use a linear regression or a classifier to investigate which columns are the most related to your target variable. $\endgroup$ – Grzegorz Oct 11 '19 at 14:04
  • $\begingroup$ Depending on the type of your variables, you could also just do something as simple as computing the correlation of your target variable with each feature using that to filter. If you have time series data, there is Granger causality, to measure influence of other variables over time. $\endgroup$ – n1k31t4 Oct 11 '19 at 16:57
0
$\begingroup$

In the end, this is what I've found around the internet. Really helpful

def best_features(df,target):
    features = list(df.columns)
    features.remove(target)

    y = df[target]
    X=df[features]
    #apply SelectKBest class to extract top 10 best features
    bestfeatures = SelectKBest(score_func=chi2, k=15)
    fit = bestfeatures.fit(X,y)
    dfscores = pd.DataFrame(fit.scores_)
    dfcolumns = pd.DataFrame(X.columns)
    #concat two dataframes for better visualization 
    featureScores = pd.concat([dfcolumns,dfscores],axis=1)
    featureScores.columns = ['Specs','Score']  #naming the dataframe columns
    return featureScores.nlargest(15,'Score')  #print 10 best features


def worst_features(df,target):
    features = list(df.columns)
    features.remove(target)

    y = df[target]
    X=df[features]
    #apply SelectKBest class to extract top 10 best features
    bestfeatures = SelectKBest(score_func=chi2, k='all')
    fit = bestfeatures.fit(X,y)
    dfscores = pd.DataFrame(fit.scores_)
    dfcolumns = pd.DataFrame(X.columns)
    #concat two dataframes for better visualization 
    featureScores = pd.concat([dfcolumns,dfscores],axis=1)
    featureScores.columns = ['Specs','Score']  #naming the dataframe columns
    return featureScores.nsmallest(15,'Score')  #print 10 best features

def largest(df,target):
    features = list(df.columns)
    features.remove(target)

    y = df[target]
    X=df[features]
    model = ExtraTreesClassifier()
    model.fit(X,y)
    feat_importances = pd.Series(model.feature_importances_, index=X.columns)
    feat_importances.nlargest(15).plot(kind='barh')
    plt.show()
    print(feat_importances.nlargest(15).index)

def smallest(df,target):
    features = list(df.columns)
    features.remove(target)

    y = df[target]
    X=df[features]
    model = ExtraTreesClassifier()
    model.fit(X,y)
    feat_importances = pd.Series(model.feature_importances_, index=X.columns)
    feat_importances.nsmallest(15).plot(kind='barh')
    plt.show()
    return feat_importances.nsmallest(15).index.to_list()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.