我的代码如下:
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import SGDClassifier
from sklearn.grid_search import GridSearchCV
class Ensamble_lastre:
def __init__(self, Csvm, Kn, alp):
self.svm = SVC(C=Csvm,probability=True)
self.neighbors = KNeighborsClassifier(n_neighbors=Kn)
self.linear_model = SGDClassifier(alpha=alp,n_iter=15000, penalty='l2', loss='modified_huber')
self.Csvm=Csvm
self.Kn=Kn
self.alp=alp
def fit(self, X, y):
self.svm.fit(X, y)
self.neighbors.fit(X, y)
self.linear_model.fit(X, y)
def get_params(self, deep=True):
self.dict={'Csvm':self.Csvm, 'Kn':self.Kn, 'alp':self.alp}
return self.dict
def predict(self,X):
self.pred1=self.svm.predict(X)
self.pred2=self.neighbors.predict(X)
self.pred3=self.linear_model.predict(X)
self.des = self.pred1 + self.pred2 + self.pred3
self.des2=[]
for self.j in range (len(self.des)):
if self.des[self.j] >= 1:
self.des2.append(1)
else:
self.des2.append(-1)
return self.pred1
X = [[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]]
y = [-1, -1, -1, 1, 1, 1]
X=np.array(X)
y=np.array(y)
tuned_parameters = [{'Csvm': [0.02, 0.03, 0.04]}, {'Kn':[1, 2, 3]}, {'alp':[0.01, 0.03, 0.05]}]
clf=GridSearchCV(Ensamble_lastre, tuned_parameters, cv=10)
clf.fit(X, y) #This is my error
这是我的错误:
klass = estimator.__class__
AttributeError: class Ensamble_lastre has no attribute '__class__'
为什么 .fit 会产生此错误?gridsearchCV 捕捉到了好的参数。
谢谢...