Scikit python错误:AttributeError:'NoneType'对象没有属性'shape'

时间:2015-03-12 21:14:15

标签: python scikit-learn

我的任务是二进制分类。 我的模型看起来像这样:

elif info['task']=='binary.classification': 
        name0 = "LogisticRegression"
        name1 = "SGDClassifier"
        model0 = LogisticRegression() 
        model1 = SGDClassifier(n_iter = np.ceil(10**6 / self.train_num),loss = 'log', verbose = verbose)

        self.classifiers.update({name0:model0, name1:model1})

        for key in self.classifiers:
            self.model = self.classifiers[key]  
            self.predict_method = self.model.predict_proba

这就是我的Fit和Predict方法的样子:

def fit(self, Xtrain, Ytrain):
    self.model.fit(Xtrain, Ytrain)          

    # Train a calibration model postprocessor

    if self.task != 'regression'  and self.postprocessor!=None:
        Yhat = self.predict_method(Xtrain)
        if len(Yhat.shape)==1: # IG modif Feb3 2015
            Yhat = np.reshape(Yhat,(-1,1))           
        self.postprocessor.fit(Yhat, Ytrain)

    return self

def predict(self, X):
    print "predict(self, X)"

    prediction = self.predict_method(X)

    # Calibrate proba
    if self.task != 'regression' and self.postprocessor!=None:        
        prediction = self.postprocessor.predict_proba(prediction)
    # Keep only 2nd column because the second one is 1-first    
    if self.target_num==1 and len(prediction.shape)>1 and prediction.shape[1]>1:
        prediction = prediction[:,1]
    # Make sure the normalization is correct
    if self.task=='multiclass.classification':
        eps = 1e-15
        norma = np.sum(prediction, axis=1)
        for k in range(prediction.shape[0]):
            prediction[k,:] /= sp.maximum(norma[k], eps)  
    return prediction

我一直收到以下错误:

File "/Applications/anaconda/lib/python2.7/site-packages/sklearn/linear_model/base.py", line 193, in decision_function
n_features = self.coef_.shape[1]
AttributeError: 'NoneType' object has no attribute 'shape'

我该如何解决这个问题? 它与Predict和Fit方法有关。

0 个答案:

没有答案