Skip to content

Instantly share code, notes, and snippets.

@Ayushijain09
Last active July 15, 2020 11:59
Show Gist options
  • Save Ayushijain09/28932e641c8516286907fbd02f005c79 to your computer and use it in GitHub Desktop.
Save Ayushijain09/28932e641c8516286907fbd02f005c79 to your computer and use it in GitHub Desktop.
Mutual Information for Regression
from sklearn.feature_selection import mutual_info_regression
from sklearn.feature_selection import SelectKBest
selector = SelectKBest(mutual_info_regression, k=10)
X_train_new = selector.fit_transform(X_train, y_train) #Applying transformation to the training set
#to get names of the selected features
mask = selector.get_support() # Output array([False, False, True, True, True, False ....])
print(selector.scores_) #Output array([0.16978127, 0.01829886, 0.45461366, 0.55126343, 0.66081217, 0.27715287 ....])
new_features = X_train.columns[mask]
print(new_features) #Output Index(['wheelbase', 'carlength', 'carwidth', 'curbweight', 'enginesize','boreratio', 'horsepower', 'citympg', 'highwaympg', 'fuelsystem_2bbl'],dtype='object')
print(train.shape) #Output (143, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment