gpt4 book ai didi

python - estimator 应该是一个实现 'fit' 方法的估计器

转载 作者:行者123 更新时间:2023-12-03 21:22:35 27 4
gpt4 key购买 nike

当我尝试在 python 中运行代码时遇到了这个问题,我该如何解决?
---> 95 验证(sm_classifier,x_test_normal_s,y_n2_test,y_test,classes_names,'NSLKDD SAE-SAE(测试)')
96 #if 姓名 == " main ":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 操作符导入 itemgetter

Softmax 类:
#from IPython.core.debugger import 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)

最佳答案

如果您更改,它应该可以工作(或至少,它可以修复当前错误)

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):

关于python - estimator 应该是一个实现 'fit' 方法的估计器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50656471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com