コード例 #1
0
def fit_model(model):
    data = generate_dataframe(220)
    ctx = data[['age', 'ARPU']]
    actions = data['action']
    rewards = data['reward']
    model.fit(ctx, actions, rewards)
    return model
コード例 #2
0
    def test_update(self):
        model = LinearBandits(
            3,
            4
        )
        context = np.array([76, 120, 654326, 2])
        action = 1
        reward = 14
        model.update(context, action, reward)

        df = generate_dataframe(500)

        contexts = df[['age', 'ARPU']]
        actions = df['action']
        rewards = df['reward']

        new_model = NeuralBandits(3, 2, layer_sizes=[50, 12], verbose=False)
        #call .fit method; num_updates will repeat training n times
        new_model.fit(contexts, actions, rewards)

        new_context = np.array([26.0, 98.456463])
        action = np.random.randint(0, 2)
        reward = np.random.random() * 10
        print(action)
        new_model.update(new_context, action, reward)
コード例 #3
0
def check_toy_problem():
    customer = get_customer()
    ctype, (age, ft) = customer
    assert isinstance(ctype, int)
    assert isinstance(age, int)
    assert isinstance(ft, float)
    reward = get_rewards(customer)
    assert reward.shape == (3, )
    fts, reward = get_cust_reward()
    df = generate_dataframe(10)
    assert isinstance(df, pd.DataFrame)
    return fts, reward
コード例 #4
0
 def test_neural_linear_model(self):
     model = NeuralBandits(num_actions=3,
                           num_features=2,
                           training_freq_network=200,
                           layer_sizes=[50])
     fts, reward = get_cust_reward()
     for i in range(300):
         action = model.action(fts)
         r = reward[action]
         model.update(fts, action, r)
     df = generate_dataframe(500)
     X = df[['age', 'ARPU']].values
     A = df['action'].values
     R = df['reward'].values
     model.fit(X, A, R)
     model.save('test_file')
     model = load_model('test_file')
     X = df[['age', 'ARPU']].sample(2).values
     model.predict(X, parallelize=False)
     os.remove('test_file')
コード例 #5
0
def predict_model(model):
    data = generate_dataframe(220)
    ctx = data[['age', 'ARPU']]
    predictions = model.predict(ctx)
    return predictions