human
Write me a code to 3 given the following:
3.1 Training the Partial Models
Note the binned dataset is smaller than the original and it's surely possible to download it. Here we'll illustrate an in-DB parallel approach that will scale well for hundreds of such subjects. Note the use of filter with training=1 which will use only the "training" half of the data.
We train the models in parallel:"
Please write code following the instructions in jupyter notebook style.
assistant
#Here is code to 3.

import scidbstrm
class Train:
    model = None
    count = 0

    @staticmethod
    def map(df):
        dft = df[['acc_x_var','acc_y_var', 'acc_z_var', 'total_light']]
        Train.model.partial_fit(numpy.matrix(dft),
                                df['sleep'],
                                [1,2])
        Train.count += len(df)
        return None

    @staticmethod
    def finalize():
        if Train.count == 0:
            return None
        buf = io.BytesIO()
        sklearn.externals.joblib.dump(Train.model, buf)
        return pandas.DataFrame({
            'count': [Train.count],
            'model': [buf.getvalue()]})

ar_fun = db.input(upload_data=scidbstrm.pack_func(Train)).store()

#Once again, don't forget our environment variables:
python_run = """'
export VIRTUAL_ENV="/home/scidb/anaconda2/envs/snowflakes"
export PATH="$VIRTUAL_ENV/bin:$PATH"
python -uc "
import io
import numpy
import pandas
import scidbstrm
import sklearn.externals
import sklearn.linear_model

Train = scidbstrm.read_func()
Train.model = sklearn.linear_model.SGDClassifier()
scidbstrm.map(Train.map, Train.finalize)
"'"""

que = db.stream(
    db.filter(
        db.arrays.IHI_BINNED_FEATURES, 
        #Note: computed variance can be NULL if a bin input segment (1 minute) has only a single value in it
        "training=1 and acc_x_var is not null and acc_y_var is not null and acc_z_var is not null"
    ),
    python_run,
    "'format=feather'",
    "'types=int64,binary'",
    "'names=count,model'",
    '_sg({}, 0)'.format(ar_fun.name)
).store(
    db.arrays.IHI_PARTIAL_MODEL)