import logging import pandas as pd import numpy as np import os from sklearn.externals import joblib from sklearn.preprocessing import Imputer from sklearn.impute import SimpleImputer logger = logging.getLogger(__name__) def streams(pop_5, playlists): """Apply streams model based on popularity after 5 days and playlist that the track exists on, including the playlist position. NOTE: there is a hardcoded path included here! Parameters ---------- pop_5: int Popularity after 5 days playlists: dict( playlistId -> position on playlist ) Playlists and their position Returns ------- Predicted total streams after 5 days INCLUDING the first five days """ # TODO: Fix this hardcoded path??? # Version hardcodes NA play lists with 1000 model_spec_file = os.path.join(os.path.dirname(__file__), 'models/tpot_20_20_xgboos_fill_na_1000.pkl') # model_spec_file = 'models/tpot_20_20_xgboos_fill_na_1000.pkl' model_spec = joblib.load(model_spec_file) pipe_line = model_spec['model'] feature_columns = model_spec['feature_cols'] imputer = SimpleImputer(strategy='constant', fill_value=1000) unknown_playlist_ids = [x for x in playlists.keys() if x not in feature_columns] if len(unknown_playlist_ids) > 0: logger.warning("Unknown playlists when modelling") df = pd.DataFrame(columns=feature_columns) playlists['pop_5'] = pop_5 # Hmm this should sit somewhere else values = pd.DataFrame({k: [v] for (k, v) in playlists.items() if k in feature_columns}) in_df = pd.concat([df, values], sort=False) imputer.fit(in_df) in_df = imputer.transform(in_df) np.exp(pipe_line.predict(in_df))[0] return np.exp(pipe_line.predict(in_df))[0]