human
3.3 Making Predictions
Now that we have our model, we can use it to make predictions. Below we'll run it on the remainder of the data, filtering for training = 0.

Can you provide a the Python code you requested?
assistant
#Here is the Python code you requested.
try:
    db.remove(db.arrays.IHI_PREDICTED_SLEEP)
except:
    print("Array not found")

class Predict:
    model = None
    @staticmethod
    def map(df):
        dfp = numpy.matrix(df[['acc_x_var','acc_y_var', 'acc_z_var', 'total_light']])
        #We're creating a new column; Arrow will complain if it's not Unicode:
        df[u'pred'] = Predict.model.predict(dfp)
        df = df [['subject', 'day', 'bin_millis', 'sleep', 'pred']]
        return df

ar_fun = db.input(
    upload_data=scidbstrm.pack_func(Predict),
    upload_schema=scidbpy.Schema.fromstring('<x:binary not null>[i]')
).cross_join(
    db.arrays.IHI_FINAL_MODEL
).store()

python_run = """'
python3 -uc "
import dill
import io
import numpy
import scidbstrm
import sklearn.externals

df = scidbstrm.read()
Predict = dill.loads(df.iloc[0, 0])
Predict.model = sklearn.externals.joblib.load(io.BytesIO(df.iloc[0, 2]))
scidbstrm.write()

scidbstrm.map(Predict.map)
"'"""

que = db.filter(
    db.arrays.IHI_BINNED_FEATURES,
    "training = 0 and acc_x_var is not null and acc_y_var is not null and acc_z_var is not null"
).stream(
    python_run,
    "'format=feather'",
    "'types=int64,int64,int64,double,int64'",
    "'names=subject,day,bin_millis,sleep,prediction'",
    '_sg({}, 0)'.format(ar_fun.name)
).store(
    db.arrays.IHI_PREDICTED_SLEEP)