本文整理汇总了Python中mlxtend.feature_selection.SequentialFeatureSelector.fit方法的典型用法代码示例。如果您正苦于以下问题:Python SequentialFeatureSelector.fit方法的具体用法?Python SequentialFeatureSelector.fit怎么用?Python SequentialFeatureSelector.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mlxtend.feature_selection.SequentialFeatureSelector
的用法示例。
在下文中一共展示了SequentialFeatureSelector.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_knn_scoring_metric
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_scoring_metric():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs5 = SFS(knn,
k_features=3,
forward=False,
floating=True,
scoring='accuracy',
cv=4,
skip_if_stuck=True,
verbose=0)
sfs5 = sfs5.fit(X, y)
assert round(sfs5.k_score_, 4) == 0.9728
sfs6 = SFS(knn,
k_features=3,
forward=False,
floating=True,
cv=4,
skip_if_stuck=True,
verbose=0)
sfs6 = sfs6.fit(X, y)
assert round(sfs6.k_score_, 4) == 0.9728
sfs7 = SFS(knn,
k_features=3,
forward=False,
floating=True,
scoring='f1_macro',
cv=4,
skip_if_stuck=True)
sfs7 = sfs7.fit(X, y)
assert round(sfs7.k_score_, 4) == 0.9727, sfs7.k_score_
示例2: test_knn_scoring_metric
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_scoring_metric():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs5 = SFS(knn,
k_features=3,
forward=False,
floating=True,
scoring='accuracy',
cv=4,
skip_if_stuck=True,
print_progress=False)
sfs5 = sfs5.fit(X, y)
assert round(sfs5.k_score_, 4) == 0.9728
sfs6 = SFS(knn,
k_features=3,
forward=False,
floating=True,
scoring='precision',
cv=4,
skip_if_stuck=True,
print_progress=False)
sfs6 = sfs6.fit(X, y)
assert round(sfs6.k_score_, 4) == 0.9737
示例3: test_run_default
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_run_default():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier()
sfs = SFS(estimator=knn,
verbose=0)
sfs.fit(X, y)
assert sfs.k_feature_idx_ == (3,)
示例4: test_fit_params
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_fit_params():
iris = load_iris()
X = iris.data
y = iris.target
sample_weight = np.ones(X.shape[0])
forest = RandomForestClassifier(n_estimators=100, random_state=123)
sfs = SFS(estimator=forest,
verbose=0)
sfs.fit(X, y, sample_weight=sample_weight)
assert sfs.k_feature_idx_ == (3,)
示例5: test_knn_cv3
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_cv3():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs1 = SFS(knn,
k_features=3,
forward=True,
floating=False,
scoring='accuracy',
cv=4,
skip_if_stuck=True,
print_progress=False)
sfs1 = sfs1.fit(X, y)
sfs1.subsets_
expect = {1: {'avg_score': 0.95299145299145294,
'cv_scores': np.array([0.97435897,
0.94871795,
0.88888889,
1.0]),
'feature_idx': (3,)},
2: {'avg_score': 0.95993589743589736,
'cv_scores': np.array([0.97435897,
0.94871795,
0.91666667,
1.0]),
'feature_idx': (2, 3)},
3: {'avg_score': 0.97275641025641035,
'cv_scores': np.array([0.97435897,
1.0,
0.94444444,
0.97222222]),
'feature_idx': (1, 2, 3)}}
dict_compare_utility(d1=expect, d2=sfs1.subsets_)
示例6: test_knn_cv3_groups
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_cv3_groups():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs1 = SFS(knn,
k_features=3,
forward=True,
floating=False,
cv=GroupKFold(n_splits=3),
verbose=0)
np.random.seed(1630672634)
groups = np.random.randint(0, 6, size=len(y))
sfs1 = sfs1.fit(X, y, groups=groups)
# print(sfs1.subsets_)
expect = {
1: {'cv_scores': np.array([0.97916667, 0.93877551, 0.96226415]),
'feature_idx': (3,),
'avg_score': 0.9600687759380482},
2: {'cv_scores': np.array([0.95833333, 0.93877551, 0.98113208]),
'feature_idx': (1, 3),
'avg_score': 0.9594136396697044},
3: {'cv_scores': np.array([0.97916667, 0.95918367, 0.94339623]),
'feature_idx': (1, 2, 3),
'avg_score': 0.9605821888503829}}
dict_compare_utility(d_actual=sfs1.subsets_, d_desired=expect, decimal=3)
示例7: test_knn_cv3
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_cv3():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs1 = SFS(knn,
k_features=3,
forward=True,
floating=False,
cv=4,
verbose=0)
sfs1 = sfs1.fit(X, y)
sfs1.subsets_
expect = {1: {'avg_score': 0.95299145299145294,
'cv_scores': np.array([0.97435897,
0.94871795,
0.88888889,
1.0]),
'feature_idx': (3,)},
2: {'avg_score': 0.95993589743589736,
'cv_scores': np.array([0.97435897,
0.94871795,
0.91666667,
1.0]),
'feature_idx': (2, 3)},
3: {'avg_score': 0.97275641025641035,
'cv_scores': np.array([0.97435897,
1.0,
0.94444444,
0.97222222]),
'feature_idx': (1, 2, 3)}}
dict_compare_utility(d_actual=sfs1.subsets_, d_desired=expect)
示例8: get_best_logisitc
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def get_best_logisitc(y):
from mlxtend.feature_selection import SequentialFeatureSelector as SFS
from sklearn.cross_validation import StratifiedKFold
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import cross_val_score
my_data = pd.read_csv('data/my_data_test.csv', encoding='utf-8')
y = my_data.target
my_data = my_data.drop('target', axis=1)
# To have better CV
skf = StratifiedKFold(y, n_folds=5, random_state=17, shuffle=False)
C_params = [0.01 , 1, 10, 50, 70, 100]
solvers = ['newton-cg', 'lbfgs', 'liblinear', 'sag']
my_result_list = []
for C_param in C_params:
for solver in solvers:
print "Looking for C : %s and solver : %s" % (C_param, solver)
model = LogisticRegression(class_weight='balanced', random_state=17,
solver=solver, C=C_param)
sfs = SFS(model,
k_features=len(my_data.columns),
forward=True,
floating=False,
scoring='roc_auc',
print_progress=False,
cv=skf,
n_jobs=-1)
sfs = sfs.fit(my_data.values, y.values)
result_sfs = pd.DataFrame.from_dict(sfs.get_metric_dict()).T
result_sfs.sort_values('avg_score', ascending=0, inplace=True)
features_sfs = result_sfs.feature_idx.head(1).tolist()
select_features_sfs = list(my_data.columns[features_sfs])
scores = cross_val_score(model, my_data[select_features_sfs], y, cv=skf, scoring='roc_auc')
my_result_list.append({'C' : C_param,
'solver' : solver,
'auc' : scores.mean(),
'std' : scores.std(),
'best_columns' : select_features_sfs,
'estimator' : model})
my_result = pd.DataFrame(my_result_list)
my_result.sort_values('auc', ascending=0, inplace=True)
best_features = my_result.best_columns.head(1).values[0]
best_model = my_result.estimator.head(1).values[0]
return best_features, best_model
示例9: test_check_pandas_dataframe_fit_backward
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_check_pandas_dataframe_fit_backward():
for floating in [True, False]:
iris = load_iris()
X = iris.data
y = iris.target
lr = SoftmaxRegression(random_seed=1)
sfs1 = SFS(lr,
k_features=2,
forward=False,
floating=floating,
scoring='accuracy',
cv=0,
verbose=0,
n_jobs=1)
df = pd.DataFrame(X, columns=['sepal len', 'sepal width',
'petal len', 'petal width'])
sfs1 = sfs1.fit(X, y)
assert sfs1.k_feature_idx_ == (1, 2)
assert sfs1.k_feature_names_ == ('1', '2')
assert sfs1.subsets_[2]['feature_names'] == ('1', '2')
sfs1 = sfs1.fit(df, y)
assert sfs1.subsets_[3]['feature_names'] == ('sepal len',
'sepal width',
'petal len')
assert sfs1.subsets_[2]['feature_names'] == ('sepal width',
'petal len')
assert sfs1.subsets_[3]['feature_idx'] == (0, 1, 2)
assert sfs1.subsets_[2]['feature_idx'] == (1, 2)
assert sfs1.k_feature_idx_ == (1, 2)
assert sfs1.k_feature_names_ == ('sepal width', 'petal len')
sfs1._TESTING_INTERRUPT_MODE = True
out = sfs1.fit(df, y)
assert len(out.subsets_.keys()) > 0
assert sfs1.interrupted_
assert sfs1.subsets_[3]['feature_names'] == ('sepal len',
'sepal width',
'petal len')
assert sfs1.k_feature_idx_ == (0, 1, 2)
assert sfs1.k_feature_names_ == ('sepal len', 'sepal width',
'petal len')
示例10: test_regression_sbfs
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_regression_sbfs():
boston = load_boston()
X, y = boston.data, boston.target
lr = LinearRegression()
sfs_r = SFS(lr,
k_features=3,
forward=False,
floating=True,
scoring='neg_mean_squared_error',
cv=10,
verbose=0)
sfs_r = sfs_r.fit(X, y)
assert sfs_r.k_feature_idx_ == (7, 10, 12), sfs_r.k_feature_idx_
示例11: test_knn_option_sfbs
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_option_sfbs():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=4)
sfs4 = SFS(knn,
k_features=3,
forward=False,
floating=True,
cv=4,
verbose=0)
sfs4 = sfs4.fit(X, y)
assert sfs4.k_feature_idx_ == (1, 2, 3)
示例12: test_max_feature_subset_parsimonious
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_max_feature_subset_parsimonious():
boston = load_boston()
X, y = boston.data, boston.target
lr = LinearRegression()
sfs = SFS(lr,
k_features='parsimonious',
forward=True,
floating=False,
cv=10)
sfs = sfs.fit(X, y)
assert sfs.k_feature_idx_ == (5, 10, 11, 12)
示例13: test_max_feature_subset_best
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_max_feature_subset_best():
boston = load_boston()
X, y = boston.data, boston.target
lr = LinearRegression()
sfs = SFS(lr,
k_features='best',
forward=True,
floating=False,
cv=10)
sfs = sfs.fit(X, y)
assert sfs.k_feature_idx_ == (1, 3, 5, 7, 8, 9, 10, 11, 12)
示例14: test_knn_option_sbs_tuplerange_1
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_knn_option_sbs_tuplerange_1():
iris = load_iris()
X = iris.data
y = iris.target
knn = KNeighborsClassifier(n_neighbors=3)
sfs4 = SFS(knn,
k_features=(1, 3),
forward=False,
floating=False,
cv=4,
verbose=0)
sfs4 = sfs4.fit(X, y)
assert round(sfs4.k_score_, 3) == 0.967, sfs4.k_score_
assert sfs4.k_feature_idx_ == (0, 2, 3), sfs4.k_feature_idx_
示例15: test_regression_in_range
# 需要导入模块: from mlxtend.feature_selection import SequentialFeatureSelector [as 别名]
# 或者: from mlxtend.feature_selection.SequentialFeatureSelector import fit [as 别名]
def test_regression_in_range():
boston = load_boston()
X, y = boston.data, boston.target
lr = LinearRegression()
sfs_r = SFS(lr,
k_features=(1, 13),
forward=True,
floating=False,
scoring='neg_mean_squared_error',
cv=10,
verbose=0)
sfs_r = sfs_r.fit(X, y)
assert len(sfs_r.k_feature_idx_) == 9
assert round(sfs_r.k_score_, 4) == -31.1537