ValueError:特殊指令必须是第一个条目

时间:2018-01-20 20:50:02

标签: python numpy machine-learning scikit-learn

为什么会出现此错误,这究竟是什么意思?

它出现在这段代码上(我只把机器学习的一部分,因为代码很长):

import numpy as np
from sklearn import neighbors
n_neighbors = 3

    if (automatic == 'true'):
        # import some data to play with
        home = Homes.query.filter_by(device_id = request.args.get('?device_id')).first()

        htng_orders = Heating_orders.query.filter_by(home_id = home.id).all()

        X_h = [[ho.timeInMinutes, ho.season, ho.ext_temp] for ho in htng_orders]
        y_h = [ho.instruction for ho in htng_orders] 

        clf_h = neighbors.KNeighborsClassifier(n_neighbors, weights='distance')
        clf_h.fit(X_h, y_h)

        new_time = datetime.datetime.now().time()
        new_timeInMinutes = (new_time.hour*60 + new_time.minute)
        new_season = get_season(date.today())
        new_ext_temp = getExtTemperature(home.city)
        new_data_h = np.c_[new_timeInMinutes, new_season, new_ext_temp]
        preddiction_h = clf_h.predict(new_data_h)

错误如下:

[...]

File "C:\[...]\FlaskREST\app.py", line 525, in get
    new_data_h = np.c_[new_timeInMinutes, new_season, new_ext_temp]
File "C:\Python\Python36-32\lib\site-packages\numpy\lib\index_tricks.py", line 289, in __getitem__
    raise ValueError("special directives must be the "
ValueError: special directives must be the first entry.

提前谢谢!

2 个答案:

答案 0 :(得分:0)

看看代码在做什么,我认为你根本不应该np.c_。该模型使用(TimeInMinutes, season, ext_temp)的三元组进行训练,因此您希望将相同的数据格式传递到.predict函数中。

new_data_h应该是

new_data_h = [new_timeInMinutes, new_season, new_ext_temp]

万一你好奇

https://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html

答案 1 :(得分:0)

我也遇到了同样的问题,ValueError:特殊指令必须来自numpy index_tricks.py文件。当我将错误的参数类型传递给sarimax时,就会出现此错误。

更正参数类型后,此错误已解决。从这种情况下检查,可能会有所帮助。