This page is updated in real time

This is a feature testing notebook which contains code before it is tested and added to the library's other notebooks. This is a real-time look at what's under development - this page changes with every commit to the github repo.

Generic Utility Methods

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt, numpy as np
def aggregate_trajectory(player_bets, aggregation='daily', plot=False, variable='bet_size'):
    
    "Gradient of a linear regression fitted to the sequence of an aggregated behavioural measure. Aggregation level can be daily, weekly, or monthly."
    
    check_measures_data(player_bets, [variable])
    
    bets = aggregate_bets(player_bets, aggregation=aggregation)
    aggregates = [df[variable].sum() for df in bets]
    
    x = np.array(range(len(aggregates))).reshape((-1, 1)) + 1
    y = aggregates

    model = LinearRegression().fit(x, y)

    model_x = x
    model_y = model.coef_ * x + model.intercept_

    if plot:
        plt.figure()
        plt.scatter(x, y)
        plt.plot(model_x, model_y, color="r")
        plt.ylabel(variable + ' sum')
        plt.show()

    return model.coef_[0]


generic_trajectory([1,1,1,2,2,3,2,3,3,2,2,1,2,3,4], plot=True)
0.11785714285714284
#
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.stats import norm
from sklearn.neighbors import KernelDensity
from sklearn.utils.fixes import parse_version

N = 20
X = np.concatenate((np.random.normal(0, 1, int(0.3 * N)),
                    np.random.normal(5, 1, int(0.7 * N))))[:, np.newaxis]

print(X)

X_plot = np.linspace(-5, 10, 100)[:, np.newaxis]

true_dens = (0.3 * norm(0, 1).pdf(X_plot[:, 0])
             + 0.7 * norm(5, 1).pdf(X_plot[:, 0]))

fig, ax = plt.subplots()
ax.fill(X_plot[:, 0], true_dens, fc='black', alpha=0.2,
        label='input distribution')
lw = 2

kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(X)
log_dens = kde.score_samples(X_plot)
ax.plot(X_plot[:, 0], np.exp(log_dens), color='cornflowerblue', lw=lw,
        linestyle='-', label="kernel = '{0}'".format('gaussian'))

ax.legend(loc='upper left')

ax.set_xlim(-4, 9)
ax.set_ylim(-0.02, 0.4)
plt.show()
[[-0.08044329]
 [-0.32961096]
 [ 1.04284362]
 [ 0.87192073]
 [-1.29449416]
 [ 1.05314599]
 [ 5.44271123]
 [ 6.18768072]
 [ 4.70380959]
 [ 4.77826858]
 [ 7.3871867 ]
 [ 5.10640424]
 [ 4.11514832]
 [ 4.74674708]
 [ 5.22682059]
 [ 4.30387842]
 [ 4.51380655]
 [ 5.97180062]
 [ 6.75511866]
 [ 4.45611202]]