TypeError:__call __()缺少1个必需的位置参数:“ inputs”

时间:2019-02-01 11:35:45

标签: python machine-learning keras deep-learning data-science

我试图根据“ input_data”中存在的功能预测收盘价(1或0)。但是,当我尝试运行代码时,出现以下错误,我不确定如何解决此问题。非常感谢您的任何帮助,谢谢

Traceback (most recent call last):
  File "F:/Machine Learning/SK_Learn/SVM_Stock.py", line 71, in <module>
    estimator.fit(x,y)
  File "C:\Python35\lib\site-packages\keras\wrappers\scikit_learn.py", line 210, in fit
    return super(KerasClassifier, self).fit(x, y, **kwargs)
  File "C:\Python35\lib\site-packages\keras\wrappers\scikit_learn.py", line 139, in fit
    **self.filter_sk_params(self.build_fn.__call__))
TypeError: __call__() missing 1 required positional argument: 'inputs'

代码如下:

class SVM_Stock:

    def __init__(self):
        pass

    def create_model(self):

        model = Sequential()
        model.add(Dense(14, input_dim=16, kernel_initializer='normal', activation='relu'))
        model.add(Dense(7, kernel_initializer='normal', activation='relu'))
        model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
        model.compile(loss='binary_crossentropy',optimizer='rmsprop', metrics=['accuracy'])
        return model


if __name__ == "__main__":

    desired_width = 450
    pd.set_option('display.width', desired_width)
    pd.set_option('display.max_columns', 17)

    ds = pd.read_csv('F:\\Machine Learning\\Linear Regression\\BIOCON-EQ.csv')

    ds = ds[['Date','Open','High','Low','Close','Volume','Slow VWMA','Fast VWMA']][14:].sort_values('Date')

    ds.loc[ds['Slow VWMA'] > ds['Fast VWMA'], 'Trend UP'] = 1
    ds.loc[ds['Slow VWMA'] < ds['Fast VWMA'], 'Trend UP'] = 0
    ds.loc[ds['Slow VWMA'] == ds['Fast VWMA'], 'Trend UP'] = -1

    ds.loc[ds['Slow VWMA'] < ds['Fast VWMA'], 'Trend Down'] = 1
    ds.loc[ds['Slow VWMA'] > ds['Fast VWMA'], 'Trend Down'] = 0
    ds.loc[ds['Slow VWMA'] == ds['Fast VWMA'], 'Trend Down'] = -1

    ds.loc[ds['Close'] > ds['Open'], 'Close Price'] = 1
    ds.loc[ds['Close'] < ds['Open'], 'Close Price'] = 0
    ds.loc[ds['Close'] == ds['Open'], 'Close Price'] = -1

    input_data = ds[['Date','Open','High','Low','Close','Trend UP', 'Trend 
    Down']]
    input_data.index = input_data.Date
    input_data.drop('Date', axis=1, inplace=True)
    target = ds[['Close Price']]

    scaler = MinMaxScaler(feature_range=(0, 1))
    x = scaler.fit_transform(input_data)
    y = target.values.ravel()

    # clf = svm.SVC(gamma=0.1, C=100)
    # clf.fit(x[:400], y[:400])
    # print(clf.score(x[:400], y[:400]))
    #
    # for i in range(420, len(x)):
    #     print("Prediction :", clf.predict(x[i].reshape(1, -1)))
    #     print(i, y[i])

    SS = SVM_Stock()
    estimator = KerasClassifier(build_fn=SS.create_model(), nb_epoch=10, verbose=0)
    estimator.fit(x,y)

    '''Cross Validate'''
    cv_scores = cross_val_score(estimator, x, y, cv=10)
    print(cv_scores.mean())

1 个答案:

答案 0 :(得分:2)

当创建估计你应该通过create_model功能而不调用它(即没有括号中):

estimator = KerasClassifier(build_fn=SS.create_model, nb_epoch=10, verbose=0)

我希望这可以解决问题。