我已经学习了如何实现SBS算法,现在尝试在python中实现SFS
class SBS():
def __init__(self, estimator, k_features, scoring=accuracy_score, test_size=0.25, random_state=1):
self.scoring = scoring
self.estimator = clone(estimator)
self.k_features = k_features
self.test_size = test_size
self.random_state = random_state
def fit(self, X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.test_size, random_state=self.random_state)
dim = X_train.shape[1]
self.indices_ = tuple(range(dim))
self.subsets_ = [self.indices_]
score = self._calc_score(X_train, y_train, X_test, y_test, self.indices_)
self.scores_ = [score]
while dim > self.k_features:
scores = []; subsets = []
for p in combinations(self.indices_, r=dim - 1):
score = self._calc_score(X_train, y_train, X_test, y_test, p)
scores.append(score)
subsets.append(p)
best = np.argmax(scores)
self.indices_ = subsets[best]
self.subsets_.append(self.indices_)
dim -= 1
self.scores_.append(scores[best])
self.k_score_ = self.scores_[-1]
return self
def transform(self, X):
return X[:, self.indices_]
def _calc_score(self, X_train, y_train, X_test, y_test, indices):
self.estimator.fit(X_train[:, indices], y_train)
y_pred = self.estimator.predict(X_test[:, indices])
score = self.scoring(y_test, y_pred)
return score
现在,我正在尝试根据上述代码实现SFS算法。
到目前为止:
class SFS():
def __init__(self, estimator, k_features=0, verbose=0, scoring=accuracy_score, test_size=0.25, random_state=1):
self.estimator = clone(estimator)
self.scoring = scoring
self.test_size = test_size
self.k_features = k_features
self.random_state = random_state
def fit(self, X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.test_size, random_state=self.random_state)
dim = X_train.shape[1]
self.indices_ = 0
self.subsets_ = []
score = self._calc_score(X_train, y_train, X_test, y_test, self.indices_)
def transform(self, X):
return X[:, self.indices_]
def _calc_score(self, X_train, y_train, X_test, y_test, indices):
self.estimator.fit(X_train[1:], y_train)
y_pred = self.estimator.predict(X_test[1:])
score = self.scoring(y_test, y_pred)
return score
我了解SFS算法,
但是用代码编写是另一回事。因为我真的是这个主题的初学者。
有人可以在fit()
方法上为我提供帮助并解释其工作原理吗?
谢谢!