本文整理汇总了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