本文整理匯總了Python中sklearn.feature_selection.mutual_info_regression方法的典型用法代碼示例。如果您正苦於以下問題:Python feature_selection.mutual_info_regression方法的具體用法?Python feature_selection.mutual_info_regression怎麽用?Python feature_selection.mutual_info_regression使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.feature_selection
的用法示例。
在下文中一共展示了feature_selection.mutual_info_regression方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: predict
# 需要導入模塊: from sklearn import feature_selection [as 別名]
# 或者: from sklearn.feature_selection import mutual_info_regression [as 別名]
def predict(self, a, b):
""" Compute the test statistic
Args:
a (array-like): Variable 1
b (array-like): Variable 2
Returns:
float: test statistic
"""
a = np.array(a).reshape((-1, 1))
b = np.array(b).reshape((-1, 1))
return (mutual_info_regression(a, b.reshape((-1,))) + mutual_info_regression(b, a.reshape((-1,))))/2
示例2: feature_importance_regression
# 需要導入模塊: from sklearn import feature_selection [as 別名]
# 或者: from sklearn.feature_selection import mutual_info_regression [as 別名]
def feature_importance_regression(features, target, n_neighbors=3, random_state=None):
cont = features.select_dtypes(include=[np.floating])
disc = features.select_dtypes(include=[np.integer, np.bool])
cont_imp = pd.DataFrame(index=cont.columns)
disc_imp = pd.DataFrame(index=disc.columns)
# Continuous features
if cont_imp.index.size > 0:
# Pearson correlation
pearson = np.array([stats.pearsonr(feature, target) for _, feature in cont.iteritems()])
cont_imp['pearson_r'] = pearson[:, 0]
cont_imp['pearson_r_p_value'] = pearson[:, 1]
# Mutual information
mut_inf = feature_selection.mutual_info_regression(cont, target, discrete_features=False,
n_neighbors=n_neighbors,
random_state=random_state)
cont_imp['mutual_information'] = mut_inf
# Discrete features
if disc_imp.index.size > 0:
# F-test
f_tests = defaultdict(dict)
for feature in disc.columns:
groups = [target[idxs] for idxs in disc.groupby(feature).groups.values()]
statistic, p_value = stats.f_oneway(*groups)
f_tests[feature]['f_statistic'] = statistic
f_tests[feature]['f_p_value'] = p_value
f_tests_df = pd.DataFrame.from_dict(f_tests, orient='index')
disc_imp['f_statistic'] = f_tests_df['f_statistic']
disc_imp['f_p_value'] = f_tests_df['f_p_value']
# Mutual information
mut_inf = feature_selection.mutual_info_regression(disc, target, discrete_features=True,
n_neighbors=n_neighbors,
random_state=random_state)
disc_imp['mutual_information'] = mut_inf
return cont_imp, disc_imp