Skip to content
Advertisement

ValueError: special directives must be the first entry

why this error appears and what does it mean exactly?

It appears on this code (I put only the part of machine learning, because the code is so long):

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)

The error is the following:

[...]

File "C:[...]FlaskRESTapp.py", line 525, in get
    new_data_h = np.c_[new_timeInMinutes, new_season, new_ext_temp]
File "C:PythonPython36-32libsite-packagesnumpylibindex_tricks.py", line 289, in __getitem__
    raise ValueError("special directives must be the "
ValueError: special directives must be the first entry.

Thank you in advance!

Advertisement

Answer

Looking at what the code is doing I don’t think you should have np.c_ there at all. The model is trained on a triplets of (TimeInMinutes, season, ext_temp), so you’ll want to pass that same data format into the .predict function.

new_data_h should be

new_data_h = [new_timeInMinutes, new_season, new_ext_temp]

just in case you’re curious

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

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement