我尝试使用以下代码,但是当我尝试对我的X_train和y_train使用fit函数时我收到以下错误fit() missing 1 required positional argument: 'self'
我对班级了解不多,但我知道它不应该问我自己。我发现了一些有关实例化的内容,但无法弄清楚。
class BernoulliNB(object):
def __init__(self, alpha=1.0):
self.alpha = alpha
def fit(self, X, y):
count_sample = X.shape[0]
# group by class
separated = [[x for x, t in zip(X, y) if t == c] for c in np.unique(y)]
# class prior
self.class_log_prior_ = [np.log(len(i) / count_sample) for i in separated]
# count of each word
count = np.array([np.array(i).sum(axis=0) for i in separated]) + self.alpha
smoothing = 2 * self.alpha
# number of documents in each class + smoothing
n_doc = np.array([len(i) + smoothing for i in separated])
print(n_doc)
def predict(self, X):
return np.argmax(self.predict_log_proba(X), axis=1)
您正在呼叫BernoulliNB.fit(...)
,而不是
b = BernoulliNB()
b.fit(...)
在我的代码中,b
是BernoulliNB
的实例,因此将自身作为自身传递。
您也可以使用
b = BernoulliNB()
BernoulliNB.fit(b,...)