【问题标题】:estimator should be an estimator implementing 'fit' method估计器应该是实现“拟合”方法的估计器
【发布时间】:2018-11-12 08:46:50
【问题描述】:

当我尝试在 python 中运行代码时,我遇到了这个问题,我该如何解决?
---> 95 验证(sm_classifier,x_test_normal_s,y_n2_test,y_test,classes_names,'NSLKDD SAE-SAE(test)') 96 #if name == "ma​​in":main() ## with if 97

<ipython-input-23-95022ce9a680> in validation(classifier, data, y_data, y_target, class_names, title)
     52         print ("No accuracy to be computed")
     53     else:
---> 54         accuracy = model_selection.cross_val_score(classifier,x, y_target, scoring='accuracy')
     55         print("Accuracy: "+ str(accuracy))
     56     precision = model_selection.cross_val_score(self.classifier, x, target, scoring='precision')

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in cross_val_score(estimator, X, y, groups, scoring, cv, n_jobs, verbose, fit_params, pre_dispatch)
    130     cv = check_cv(cv, y, classifier=is_classifier(estimator))
    131     cv_iter = list(cv.split(X, y, groups))
--> 132     scorer = check_scoring(estimator, scoring=scoring)
    133     # We clone the estimator to make sure that all the folds are
    134     # independent, and that it is pickle-able.

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\scorer.py in check_scoring(estimator, scoring, allow_none)
    248     if not hasattr(estimator, 'fit'):
    249         raise TypeError("estimator should be an estimator implementing "
--> 250                         "'fit' method, %r was passed" % estimator)
    251     if isinstance(scoring, six.string_types):
    252         return get_scorer(scoring)

TypeError: estimator should be an estimator implementing 'fit' method, <__main__.Softmax object at 0x00000000048D1F98> was passed

将 numpy 导入为 np 导入数学 随机导入 from operator import itemgetter

类 Softmax: #from IPython.core.debugger 导入 Tracer;示踪剂()()

def __init__(self, batch_size=50, epochs=1000, learning_rate=1e-2, reg_strength=1e-5, weight_update='adam'):
    self.W = None
    self.batch_size = batch_size
    self.epochs = epochs
    self.learning_rate = learning_rate
    self.reg_strength = reg_strength
    self.weight_update = weight_update

def train(self, X, y):
    n_features = X.shape[1]
    n_classes = y.max() + 1
    self.W = np.random.randn(n_features, n_classes) / np.sqrt(n_features/2)
    config = {'reg_strength': self.reg_strength, 'batch_size': self.batch_size,
            'learning_rate': self.learning_rate, 'eps': 1e-8, 'decay_rate': 0.99,
            'momentum': 0.9, 'cache': None, 'beta_1': 0.9, 'beta_2':0.999,
            'velocity': np.zeros(self.W.shape)}
    c = globals()['Softmax']
    for epoch in range(self.epochs):
        loss, config = getattr(c, self.weight_update)(self, X, y, config)
        print ("Epoch:" +str(epoch)+", Loss: "+str(loss))

def predict(self, X):
    return np.argmax(X.dot(self.W), 1)

def loss(self, X, y, W, b, reg_strength):
    sample_size = X.shape[0]
    predictions = X.dot(W) + b

    # Fix numerical instability
    predictions -= predictions.max(axis=1).reshape([-1, 1])

    # Run predictions through softmax
    softmax = math.e**predictions
    softmax /= softmax.sum(axis=1).reshape([-1, 1])

    # Cross entropy loss
    loss = -np.log(softmax[np.arange(len(softmax)), y]).sum() 
    loss /= sample_size
    loss += 0.5 * reg_strength * (W**2).sum()

    softmax[np.arange(len(softmax)), y] -= 1
    dW = (X.T.dot(softmax) / sample_size) + (reg_strength * W)
    return loss, dW

def sgd(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength')(config)
    learning_rate, batch_size, reg_strength = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    self.W -= learning_rate * dW
    return loss, config

def sgd_with_momentum(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'momentum')(config)
    learning_rate, batch_size, reg_strength, momentum = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    config['velocity'] = momentum*config['velocity'] - learning_rate*dW
    self.W += config['velocity']
    return loss, config

def rms_prop(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'decay_rate', 'eps', 'cache')(config)
    learning_rate, batch_size, reg_strength, decay_rate, eps, cache = items

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    cache = np.zeros(dW.shape) if cache == None else cache
    cache = decay_rate * cache + (1-decay_rate) * dW**2
    config['cache'] = cache

    self.W -= learning_rate * dW / (np.sqrt(cache) + eps)
    return loss, config

def adam(self, X, y, config):
    items = itemgetter('learning_rate', 'batch_size', 'reg_strength', 'eps', 'beta_1', 'beta_2')(config)
    learning_rate, batch_size, reg_strength, eps, beta_1, beta_2 = items
    config.setdefault('t', 0)
    config.setdefault('m', np.zeros(self.W.shape))
    config.setdefault('v', np.zeros(self.W.shape))

    loss, dW = self.sample_and_calculate_gradient(X, y, batch_size, self.W, 0, reg_strength)

    config['t'] += 1
    config['m'] = config['m']*beta_1 + (1-beta_1)*dW
    config['v'] = config['v']*beta_2 + (1-beta_2)*dW**2
    m = config['m']/(1-beta_1**config['t'])
    v = config['v']/(1-beta_2**config['t'])
    self.W -= learning_rate*m/(np.sqrt(v)+eps)
    return loss, config

def sample_and_calculate_gradient(self, X, y, batch_size, w, b, reg_strength):
    random_indices = random.sample(range(X.shape[0]), batch_size)
    X_batch = X[random_indices]
    y_batch = y[random_indices]
    return self.loss(X_batch, y_batch, w, b, reg_strength)

【问题讨论】:

  • 根据堆栈跟踪,您的 sm_classifierSoftmax 类型,它不是 sklearn Estimator 汽车,它没有 fit 方法。你能给出你的Softmax 类的实现吗?
  • 当我运行此代码“打印(sm_classifier)”时,输出 <__main__.softmax>
  • 问题已修改并添加代码

标签: python scikit-learn


【解决方案1】:

如果你改变它应该可以工作(或者至少,它修复了当前的错误)

def train(self, X, y):

def fit(self, X, y):

一个有效的 sklearn 估计器需要 fitpredict 方法。


看看这个 How to create a custom Sklearn Estimator class

尝试替换:

class Softmax:

作者:

from sklearn.base import BaseEstimator, ClassifierMixin
class Softmax(BaseEstimator, ClassifierMixin):  

【讨论】:

  • TypeError: Cannot clone object '<__main__.softmax object at>' (type main.Softmax'>): 它似乎不是 scikit -learn 估计器,因为它没有实现“get_params”方法。
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 2015-12-09
  • 2011-11-27
  • 2020-06-25
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多