Kaggle competition: Rock, Paper, Scissors
<SOS> metric: rps Kaggle data source: rock,-paper,-scissors<categorify>
%%writefile main.py
def random_agent_seeded(observation, configuration, seed=42):
if observation.step == 0:
random.seed(seed)
 action = random.randint(0, configuration.signs-1)
 print(f'random_agent_seeded({seed})= {action}')
 return int(action)
 def random_agent_unseeded(observation, configuration):
if observation.step == 0:
random.seed(None)
 action = random.randint(0, configuration.signs-1)
 print(f'random_agent_unseeded() = {action}')
 return int(action)
tt['Date_7Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 7] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_14Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 14] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_21Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 21] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_28Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 28] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_35Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 35] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_60Days_Since_First_Case'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Case'] == 60] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_7Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 7] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_14Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 14] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_21Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 21] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_28Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 28] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_35Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 35] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_60Days_Since_Ten_Cases'] = tt['Place'].map(tt.loc[tt['Days_Since_Ten_Cases'] == 60] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_7Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 7] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_14Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 14] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_21Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 21] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_28Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 28] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_35Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 35] \ 
.set_index('Place')['Date'] \ 
.to_dict())
 tt['Date_60Days_Since_First_Fatal'] = tt['Place'].map(tt.loc[tt['Days_Since_First_Fatal'] == 60] \ 
.set_index('Place')['Date'] \ 
.to_dict() )<feature_engineering>
%%writefile -a main.py
mp.dps = 2048
def encode_irrational(irrational: Union[str,mp.mpf], offset=0)-> List[int]:

 if isinstance(irrational, list)and all([ 0 <= n <= 2 for n in irrational ]):
return irrational
string = re.sub('[^1-9]', '', str(irrational))
sequence = [ 
(int(c)+ int(offset)) % 3
for c in string
]
assert len(sequence)>= 1000
return sequence
class IrrationalAgent() :

 irrationals = {
name: encode_irrational(irrational)
 for name, irrational in {
'pi': mp.pi() ,
'phi': mp.phi() ,
'e': mp.e() ,
'sqrt2': mp.sqrt(2),
'sqrt5': mp.sqrt(5),
'euler': mp.euler() ,
'catalan': mp.catalan() ,
'apery': mp.apery() ,
}.items()
}
def __init__(self, name='irrational', irrational: Union[str,mp.mpf] = None, offset=0, verbose=True):
if irrational is not None and(name == 'irrational' or name in self.irrationals.keys()):
name = 'secret'
if name == 'irrational':
irrational = self.generate_secure_irrational()
if name in self.irrationals.keys() :
irrational = self.irrationals[name]
self.irrational = self.encode_irrational(irrational, offset=offset)
 self.name = name
self.offset = offset
self.verbose = verbose
self.reset()
def reset(self):

 self.history = {
"action": [],
"opponent": []
}
if self.name == 'irrational':
irrational = self.generate_secure_irrational()
self.irrational = self.encode_irrational(irrational, offset=self.offset)
 def __call__(self, obs, conf):
return self.agent(obs, conf)
 def agent(self, obs, conf):

 if obs.step == 0:
self.reset()
if obs.step > 0:
self.history['opponent'].append(obs.lastOpponentAction)
 action = self.action(obs, conf)
 self.history['action'].append(action)
 return action
def action(self, obs, conf):

 action = int(self.irrational[ obs.step % len(self.irrational)])
 action =(action + self.offset)% conf.signs
if self.verbose:
name = self.__class__.__name__ + ':' + self.name +(f'+{self.offset}' if self.offset else '')
 opponent =(self.history['opponent'] or [None])[-1]
expected =(action - 1)% 3
print(f"{obs.step:4d} | {opponent}{self.win_symbol() } > action {action} | " +
f"{name}")
 return action
@staticmethod
def generate_secure_irrational() :

 irrational = sum([
mp.sqrt(n)*(time.monotonic_ns() % 2**32)
 for n in range(2, 5 +(time.monotonic_ns() % 1024))
])
 return irrational
@classmethod
def encode_irrational(cls, irrational: Union[str,mp.mpf], offset=0)-> List[int]:
if irrational is None:
irrational = cls.generate_secure_irrational()
return encode_irrational(irrational, offset)
 def win_symbol(self):

 action =(self.history['action'] or [None])[-1]
opponent =(self.history['opponent'] or [None])[-1]
if isinstance(action, int)and isinstance(opponent, int):
if action % 3 ==(opponent + 1)% 3: return '+'
if action % 3 ==(opponent + 0)% 3: return '|'
if action % 3 ==(opponent - 1)% 3: return '-'
return ' '
irrational_instance = IrrationalAgent(name='pi', offset=0)
 def irrational_agent(obs, conf):
return irrational_instance.agent(obs, conf)
tt['CC_7D_1stCase'] = tt.loc[tt['Date_7Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['CC_14D_1stCase'] = tt.loc[tt['Date_14Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['CC_21D_1stCase'] = tt.loc[tt['Date_21Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['CC_28D_1stCase'] = tt.loc[tt['Date_28Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['CC_35D_1stCase'] = tt.loc[tt['Date_35Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['CC_60D_1stCase'] = tt.loc[tt['Date_60Days_Since_First_Case'] == tt['Date']]['ConfirmedCases']
tt['F_7D_1stCase'] = tt.loc[tt['Date_7Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['F_14D_1stCase'] = tt.loc[tt['Date_14Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['F_21D_1stCase'] = tt.loc[tt['Date_21Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['F_28D_1stCase'] = tt.loc[tt['Date_28Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['F_35D_1stCase'] = tt.loc[tt['Date_35Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['F_60D_1stCase'] = tt.loc[tt['Date_60Days_Since_First_Case'] == tt['Date']]['Fatalities']
tt['CC_7D_10Case'] = tt.loc[tt['Date_7Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['CC_14D_10Case'] = tt.loc[tt['Date_14Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['CC_21D_10Case'] = tt.loc[tt['Date_21Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['CC_28D_10Case'] = tt.loc[tt['Date_28Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['CC_35D_10Case'] = tt.loc[tt['Date_35Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['CC_60D_10Case'] = tt.loc[tt['Date_60Days_Since_Ten_Cases'] == tt['Date']]['ConfirmedCases']
tt['F_7D_10Case'] = tt.loc[tt['Date_7Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['F_14D_10Case'] = tt.loc[tt['Date_14Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['F_21D_10Case'] = tt.loc[tt['Date_21Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['F_28D_10Case'] = tt.loc[tt['Date_28Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['F_35D_10Case'] = tt.loc[tt['Date_35Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['F_60D_10Case'] = tt.loc[tt['Date_60Days_Since_Ten_Cases'] == tt['Date']]['Fatalities']
tt['CC_7D_1Fatal'] = tt.loc[tt['Date_7Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['CC_14D_1Fatal'] = tt.loc[tt['Date_14Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['CC_21D_1Fatal'] = tt.loc[tt['Date_21Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['CC_28D_1Fatal'] = tt.loc[tt['Date_28Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['CC_35D_1Fatal'] = tt.loc[tt['Date_35Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['CC_60D_1Fatal'] = tt.loc[tt['Date_60Days_Since_First_Fatal'] == tt['Date']]['ConfirmedCases']
tt['F_7D_1Fatal'] = tt.loc[tt['Date_7Days_Since_First_Fatal'] == tt['Date']]['Fatalities']
tt['F_14D_1Fatal'] = tt.loc[tt['Date_14Days_Since_First_Fatal'] == tt['Date']]['Fatalities']
tt['F_21D_1Fatal'] = tt.loc[tt['Date_21Days_Since_First_Fatal'] == tt['Date']]['Fatalities']
tt['F_28D_1Fatal'] = tt.loc[tt['Date_28Days_Since_First_Fatal'] == tt['Date']]['Fatalities']
tt['F_35D_1Fatal'] = tt.loc[tt['Date_35Days_Since_First_Fatal'] == tt['Date']]['Fatalities']
tt['F_60D_1Fatal'] = tt.loc[tt['Date_60Days_Since_First_Fatal'] == tt['Date']]['Fatalities']<remove_duplicates>
%%writefile -a main.py
class IrrationalSearchAgent(IrrationalAgent):

 def __init__(self, name='irrational', irrational=None, search: List[Union[str, mp.mpf]]=None, verbose=True):
search = {
f'irrational
for n, number in enumerate(search)
 } if search is not None else {}
self.irrationals = { **self.__class__.irrationals, **search }
super().__init__(name=name, irrational=irrational, verbose=verbose)
 def action(self, obs, conf):
expected, irrational_name = self.search_irrationals(self.history['opponent'])
 if expected is not None:
action =(expected + 1)% conf.signs
opponent =(self.history['opponent'] or [None])[-1]
if self.verbose:
print(f"{obs.step:4d} | {opponent}{self.win_symbol() } > action {expected} -> {action} | " +
f"Found Irrational: {irrational_name}")
 else:
action = super().action(obs, conf)
 return action
def search_irrationals(self, sequence: List[int])-> Tuple[int, str]:

 expected, irrational_name = None, None
if len(sequence):
for offset in [0,1,2]:
for irrational_name, irrational in self.irrationals.items() :
irrational = [(n + offset)% 3 for n in irrational[:len(sequence)+1] ]
if irrational[:len(sequence)] == sequence:
expected = irrational[len(sequence)]
break
else: continue
break
return expected, irrational_name
irrational_search_instance = IrrationalSearchAgent()
def irrational_search_agent(obs, conf):
return irrational_search_instance.agent(obs, conf)
latest_summary_stats = tt.query('Date == @max_date')\
[['Country_Region',
'Place',
'Max_Cases_Per_100kPop',
'Max_Fatalities_Per_100kPop',
'Max_Confirmed_Cases',
'Population_final',
'Days_Since_First_Case',
'Days_Since_Ten_Cases']] \ 
.drop_duplicates()<init_hyperparams>
%%writefile -a main.py
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
if os.environ.get('GFOOTBALL_DATA_DIR', ''):
os.chdir('/kaggle_simulations/agent/')
 class RandomSeedSearch(IrrationalSearchAgent):

 methods = [ 'random' ]
cache_steps = 20
cache_seeds = int(4 * 100_000_000 / len(methods)/ cache_steps)
 cache = {
method: np.load(f'{method}.npy')
 for method in methods
if os.path.exists(f'{method}.npy')
 }
def __init__(self,
min_length=4,
max_offset=1000,
use_stats=True,
cheat=False,
no_submit=False,
verbose=True 
):

 self.use_stats = use_stats
self.cheat = cheat
self.min_length = min_length
self.max_offset = max_offset
self.no_submit = no_submit
self.conf = {'episodeSteps': 1000, 'actTimeout': 1000, 'agentTimeout': 15, 'runTimeout': 1200, 'isProduction': False, 'signs': 3}
super().__init__(verbose=verbose)
 self.print_cache_size()
def reset(self):

 if self.verbose >= 2: print(f'{self.__class__.__name__} | reset() ')
 super().reset()
self.candidate_seeds = defaultdict(lambda: defaultdict(dict))
self.repeating_seeds = defaultdict(lambda: defaultdict(int))
if self.cheat:
self.set_global_seed()
def action(self, obs, conf):
self.conf = conf
if self.no_submit and obs.step > 900:
raise Exception("Don't Submit To Competition")
 irrational, irrational_name = self.search_irrationals(self.history['opponent'])
 if irrational is not None and obs.step > self.min_length + 2:
return super().action(obs, conf)
 expected, seed, offset, method = self.search_cache(self.history['opponent'])
 if seed is None and irrational is not None:
action = super().action(obs, conf)
 elif expected is not None:
action =(expected + 1)% conf.signs
opponent =(self.history['opponent'] or [None])[-1]
if seed is None: seed = f"{'many':>12s}"
else: seed = f"{seed +(offset or 0)/1000:12.3f}"
if self.verbose:
print(
 f"{obs.step:4d} | {opponent}{self.win_symbol() } > action {expected} -> {action} | " +
f"Found RNG Seed: {method:6s} {seed} |",
self.log_repeating_seeds() 
)
 else:
action = super().action(obs, conf)
 return int(action)% conf.signs
def search_cache(self, history: List[int])\
-> Tuple[Optional[int], Optional[int], Optional[int], Optional[str]]:

 expected, seed, offset, method = None, None, None, None
for method in self.methods:
seed, expected, offset = self.search_cache_method(history, method=method)
 if expected is not None:
break
return expected, seed, offset, method
def search_cache_method(self, history: List[int], method='random')\
-> Tuple[Optional[int], Optional[int], Optional[int]]:

 time_start = time.perf_counter()
seed = None
expected = None
offset = None
if method in self.cache.keys() and len(history):
seeds, offset = self.find_candidate_seeds(history, method)
 sequence = history[offset:]
if self.use_stats and len(seeds)>= 2:
seed = None
stats = np.bincount(self.cache[method][seeds,len(sequence)])
 expected = np.argmax(stats)
 if np.count_nonzero(stats)== 1:
seed = np.min(seeds)
 elif len(seeds):
seed = np.min(seeds)
 size = len(sequence)
 if self.cache[method].shape[1] > size:
expected = self.cache[method][seed,size]
else:
expected = self.get_rng_sequence(seed, length=size + 1, method=method)[-1]
if seed is not None:
self.repeating_seeds[method][seed + offset/1000] += 1
if self.verbose >= 2:
time_taken =(time.perf_counter() - time_start)* 1000
print(f'{self.__class__.__name__} | search_cache({method}): {time_taken:.3f}ms')
 return seed, expected, offset
def find_candidate_seeds(self, history, method: str)-> Tuple[np.ndarray, int]:

 seeds_by_offset = {}
max_offset = min(self.max_offset, len(history))
for offset in range(max_offset):
sequence = history[offset:]
size = np.min([len(sequence), self.cache[method].shape[1]])
 if size < self.min_length: continue
if offset in self.candidate_seeds[method]:
candidates = self.candidate_seeds[method][offset]
if len(candidates)== 0:
seeds = candidates
else:
seeds_idx = np.where(np.all(
 self.cache[method][ candidates, :size ] == sequence[ :size ]
, axis=1)) [0]
seeds = candidates[seeds_idx]
else:
seeds = np.where(np.all(
 self.cache[method][ : , :size ] == sequence[ :size ]
, axis=1)) [0]
self.candidate_seeds[method][offset] = seeds
if len(seeds):
seeds_by_offset[offset] = seeds
if len(seeds_by_offset)== 0:
seeds = np.array([])
 offset = 0
else:
offset = sorted(seeds_by_offset.items() , key=lambda pair: len(pair[1])) [0][0]
seeds = seeds_by_offset[offset]
return seeds, offset
def get_rng_sequence(self, seed, length, method='random', use_cache=True)-> List[int]:

 sequence = []
if(use_cache
and method in self.cache.keys()
and seed < self.cache[method].shape[0]
and length < self.cache[method].shape[1] 
):
sequence = self.cache[method][seed][:length]
else:
if method == 'random':
seed_state = random.getstate()
random.seed(seed)
 sequence = [ random.randint(0,2)for _ in range(length)]
random.setstate(seed_state)
 elif method == 'np':
seed_state = np.random.get_state()
np.random.seed(seed)
 sequence = np.random.randint(0,2, length)
 np.random.set_state(seed_state)
 elif method == 'tf':
generator = tf.random.Generator.from_seed(seed)
 sequence = generator.uniform(( length,), minval=0, maxval=3, dtype=tf.dtypes.int32 ).numpy()
return list(sequence)
 def precache(self)-> List[str]:
return [ self.precache_method(method)for method in self.methods ]
def precache_method(self, method='random')-> str:

 filename = f'{method}.npy'
shape =(self.cache_seeds, self.cache_steps)
 if os.path.exists(filename)and np.load(filename ).shape == shape:
print(f'cached {filename:10s} =', shape, '=', naturalsize(os.path.getsize(filename)))
 else:
method_cache = np.array([
self.get_rng_sequence(seed=seed, length=self.cache_steps, method=method)
 for seed in range(self.cache_seeds)
 ], dtype=np.int8)
 np.save(filename, method_cache)
 print(f'wrote {filename:10s} =', method_cache.shape, '=', naturalsize(os.path.getsize(filename)))
 return filename
def log_repeating_seeds(self)-> dict:

 repeating_seeds = {}
for method in self.repeating_seeds.keys() :
repeating_seeds[method] = {
key: value
for key, value in self.repeating_seeds[method].items()
if value >= self.min_length
}
repeating_seeds[method] = dict(sorted(repeating_seeds[method].items() , key=itemgetter(1), reverse=True))
if len(repeating_seeds[method])== 0:
del repeating_seeds[method]
return repeating_seeds
def print_cache_size(self):

 if self.verbose:
filenames = [(filename + ' = ' + naturalsize(os.path.getsize(filename)))
 for filename in glob.glob('*')]
print('tar.gz =', filenames)
 print(f'{self.__class__.__name__}::cache.keys() ', list(self.cache.keys()))
for name, cache in self.cache.items() :
print(f'{self.__class__.__name__}::cache[{name}] = {cache.shape}')
 @staticmethod
def set_global_seed(seed=42):

 random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 random_seed_search_instance = RandomSeedSearch()
def random_seed_search_agent(obs, conf):
return random_seed_search_instance.agent(obs, conf)
SEED = 42
params = {'num_leaves': 8,
'min_data_in_leaf': 5,
'objective': 'regression',
'max_depth': 8,
'learning_rate': 0.02,
'bagging_freq': 5,
'bagging_fraction': 0.8,
'feature_fraction': 0.8201,
'bagging_seed': SEED,
'reg_alpha': 1,
'reg_lambda': 4.9847051755586085,
'random_state': SEED,
'metric': 'mse',
'verbosity': 100,
'n_estimators' : 500,
'min_gain_to_split': 0.02,
'min_child_weight': 5,
'num_threads': 6,
}<feature_engineering>
%run main.py
pd.set_option('max_columns', 500)
 try:
tt['healthexp'] = pd.to_numeric(tt['healthexp'].str.replace(',',''))
except:
pass
train = tt.loc[tt['Date'] < '25-March-2020']
test = tt.loc[tt['Date'] >= '25-March-2020']<define_variables>
%%time
RandomSeedSearch().precache()
FEATURES = ['doy', 'dow', 'hasProvidence',
'lat', 'lon', 'Days_Since_First_Case', 'Days_Since_Ten_Cases',
'Days_Since_Hundred_Cases', 'Days_Since_First_Fatal',
'Days_Since_Ten_Fatal', 'Days_Since_Hundred_Fatal', 'Smoking_Rate',
'Population_final',
'hospibed', 'smokers', 'sex0', 'sex14', 'sex25', 'sex54', 'sex64',
'sex65plus', 'sexratio', 'lung', 'femalelung', 'malelung', 'gdp2019',
'healthexp', 'healthperpop', 'fertility',
'CC_7D_1stCase',
'CC_14D_1stCase',
'CC_21D_1stCase',
'CC_28D_1stCase',
'CC_35D_1stCase',
'CC_60D_1stCase',
'F_7D_1stCase',
'F_14D_1stCase',
'F_21D_1stCase',
'F_28D_1stCase',
'F_35D_1stCase',
'F_60D_1stCase',
'CC_7D_10Case',
'CC_14D_10Case',
'CC_21D_10Case',
'CC_28D_10Case',
'CC_35D_10Case',
'CC_60D_10Case',
'F_7D_10Case',
'F_14D_10Case',
'F_21D_10Case',
'F_28D_10Case',
'F_35D_10Case',
'F_60D_10Case',
'CC_7D_1Fatal',
'CC_14D_1Fatal',
'CC_21D_1Fatal',
'CC_28D_1Fatal',
'CC_35D_1Fatal',
'CC_60D_1Fatal',
'F_7D_1Fatal',
'F_14D_1Fatal',
'F_21D_1Fatal',
'F_28D_1Fatal',
'F_35D_1Fatal',
'F_60D_1Fatal'
]
TARGET = ['ConfirmedCases_Log']<train_model>
%%time
%run main.py
reg = lgb.LGBMRegressor(**params)
 reg.fit(train[FEATURES], train[TARGET] )<predict_on_test>
%%time
random.seed(42)
 method = 'random'
size = RandomSeedSearch.cache[method].shape[1]
history = np.array([ random.randint(0,2)for _ in range(size)])
 np.where(np.all(
 RandomSeedSearch.cache[method][ : , :size ] == history[ :size ]
, axis=1)) [0]
test['ConfirmedCases_Log_Pred'] = reg.predict(test[FEATURES])
 test['ConfirmedCases_pred'] = test['ConfirmedCases_Log_Pred'].apply(np.expm1 )<import_modules>
%%time
%run main.py
from sklearn.linear_model import LinearRegression, ElasticNet<filter>
%%writefile test_RandomSeedSearch.py
@pytest.mark.parametrize("name", IrrationalSearchAgent.irrationals.keys())
 @pytest.mark.parametrize("offset", [0,1,2])
 def test_RandomSeedSearch_vs_named_irrational(name, offset):

 assert len(RandomSeedSearch.cache)
 episodeSteps = 100
results = evaluate(
 "rps",
[
IrrationalAgent(name=name, offset=offset, verbose=False),
RandomSeedSearch(verbose=False)
 ],
configuration={
"episodeSteps": episodeSteps,
"tieRewardThreshold": 1,
"actTimeout": 1000,
}, 
)
 results = np.array(results ).reshape(( -1,2))
assert len(results[ results == None ])== 0
assert(results[0][0] + episodeSteps/2.1)< results[0][1]
def test_RandomSeedSearch_vs_seeded_rng() :

 assert len(RandomSeedSearch.cache)
 episodeSteps = 100
results = evaluate(
 "rps",
[
random_agent_seeded,
RandomSeedSearch(verbose=False)
 ],
configuration={
"episodeSteps": episodeSteps,
"tieRewardThreshold": 1,
"actTimeout": 1000,
}, 
)
 assert results[0][0] < results[0][1], results
def test_RandomSeedSearch_vs_Irrational() :

 episodeSteps = 100
results = Parallel(-1 )(delayed(evaluate )(
 "rps",
[
IrrationalAgent(verbose=False),
RandomSeedSearch(verbose=False)
 ],
configuration={
"episodeSteps": episodeSteps,
"tieRewardThreshold": 1,
"actTimeout": 1000,
},
num_episodes=1 
)for _ in range(int(1000/episodeSteps)))
 results = np.array(results ).reshape(( -1,2))
assert len(results[ results == None ])== 0
totals = np.mean(results, axis=0)
 std = np.std(results, axis=0 ).round(2)
 winrate = [ np.sum(results[:,0] > results[:,1]),
np.sum(results[:,0] < results[:,1])]
print('results', results)
 print('totals', totals)
 print('std', std)
 print('winrate', winrate)
 assert np.abs(totals[0])< 0.2 * episodeSteps
assert np.abs(totals[1])< 0.2 * episodeSteps
assert np.abs(std[0])< 0.2 * episodeSteps
assert np.abs(std[1])< 0.2 * episodeSteps
def test_RandomSeedSearch_vs_unseeded_RNG() :

 episodeSteps = 100
results = Parallel(-1 )(delayed(evaluate )(
 "rps",
[
"rng/random_agent_unseeded.py",
RandomSeedSearch(verbose=False)
 ],
configuration={
"episodeSteps": episodeSteps,
"tieRewardThreshold": 1,
"actTimeout": 1000,
},
num_episodes=1 
)for _ in range(int(1000/episodeSteps)))
 results = np.array(results ).reshape(( -1,2))
assert len(results[ results == None ])== 0
totals = np.sum(results, axis=0)
 std = np.std(results, axis=0 ).round(2)
 winrate = [ np.sum(results[:,0] > results[:,1]),
np.sum(results[:,0] < results[:,1])]
print('results', results)
 print('totals', totals)
 print('std', std)
 print('winrate', winrate)
 assert winrate[0] <= winrate[1], winrate
assert totals[0] < totals[1], totals
myplace = "US_Florida"
tt.query('Place == @myplace and Days_Since_First_Fatal >= 0')[['Days_Since_First_Fatal','Fatalities_Log']]<prepare_x_and_y>
!pip install --upgrade -q kaggle_environments 2> /dev/null
for myplace in us_states:
try:
fig, axs = plt.subplots(1, 2, figsize=(15, 3))
dat = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','ConfirmedCases_Log']].dropna()
X = dat['Days_Since_Ten_Cases']
y = dat['ConfirmedCases_Log']
y = y.cummax()
dat_all = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','ConfirmedCases_Log']]
X_pred = dat_all['Days_Since_Ten_Cases']
en = ElasticNet()
en.fit(X.values.reshape(-1, 1), y.values)
 preds = en.predict(X_pred.values.reshape(-1, 1))
tt.loc[(tt['Place'] == myplace)&(tt['Days_Since_Ten_Cases'] >= 0), 'ConfirmedCases_Log_Pred1'] = preds
tt.loc[(tt['Place'] == myplace), 'ConfirmedCases_Pred1'] = tt['ConfirmedCases_Log_Pred1'].apply(np.expm1)
 pop_myplace = tt.query('Place == @myplace')['Population_final'].values[0]
tt.loc[(tt['Place'] == myplace)&(tt['ConfirmedCases_Pred1'] >(0.1 * pop_myplace)) , 'ConfirmedCases_Pred1'] =(0.1 * pop_myplace)
 ax = tt.query('Place == @myplace' ).set_index('Date')[['ConfirmedCases','ConfirmedCases_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[0])
 dat = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','Fatalities_Log']].dropna()
if len(dat)< 5:
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt.loc[(tt['Place'] == myplace)]['ConfirmedCases_Pred1'] * 0.001
elif tt.query('Place == @myplace')['Fatalities'].max() < 5:
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt.loc[(tt['Place'] == myplace)]['ConfirmedCases_Pred1'] * 0.001
else:
X = dat['Days_Since_Ten_Cases']
y = dat['Fatalities_Log']
y = y.cummax()
dat_all = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','Fatalities_Log']]
X_pred = dat_all['Days_Since_Ten_Cases']
en = ElasticNet()
en.fit(X.values.reshape(-1, 1), y.values)
 preds = en.predict(X_pred.values.reshape(-1, 1))
tt.loc[(tt['Place'] == myplace)&(tt['Days_Since_Ten_Cases'] >= 0), 'Fatalities_Log_Pred1'] = preds
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt['Fatalities_Log_Pred1'].apply(np.expm1)
 pop_myplace = tt.query('Place == @myplace')['Population_final'].values[0]
tt.loc[(tt['Place'] == myplace)&(tt['Fatalities_Pred1'] >(0.0005 * pop_myplace)) , 'Fatalities_Pred1'] =(0.0005 * pop_myplace)
 ax = tt.query('Place == @myplace' ).set_index('Date')[['Fatalities','Fatalities_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[1])
 plt.show()
except:
print(f'============= FAILED FOR {myplace} =============')
<drop_column>
!pytest -v test_RandomSeedSearch.py
myplace = 'US_Virgin Islands'
dat = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Place','Days_Since_Ten_Cases','ConfirmedCases']].dropna()<groupby>
env = make("rps", configuration={"episodeSteps": 10}, debug=True)
 env.run([ RandomSeedSearch(max_offset=1000), random_agent_seeded, ])
 env.render(mode="ipython", width=400, height=400 )
tt.loc[tt['Place'].isin(us_states)].groupby('Place')['Fatalities_Pred1'].max().sum()<feature_engineering>
env = make("rps", configuration={"episodeSteps": 20}, debug=True)
 env.run([ RandomSeedSearch(verbose=2, max_offset=1000), random_agent_unseeded, ])
 env.render(mode="ipython", width=400, height=400 )
tt.loc[tt['Place'].isin(constant_fatal_places), 'ConfirmedCases_Pred1'] = tt.loc[tt['Place'].isin(constant_fatal_places)]['Place'].map(tt.loc[tt['Place'].isin(constant_fatal_places)].groupby('Place')['ConfirmedCases'].max())
 tt.loc[tt['Place'].isin(constant_fatal_places), 'Fatalities_Pred1'] = tt.loc[tt['Place'].isin(constant_fatal_places)]['Place'].map(tt.loc[tt['Place'].isin(constant_fatal_places)].groupby('Place')['Fatalities'].max() )<create_dataframe>
env = make("rps", configuration={"episodeSteps": 20}, debug=True)
 env.run([ RandomSeedSearch(cheat=True, max_offset=0), random_agent_unseeded, ])
 env.render(mode="ipython", width=400, height=400 )
remaining_places = pd.DataFrame(tt.groupby('Place')['ConfirmedCases_Pred1'].max().isna() ).query('ConfirmedCases_Pred1' ).index.values
print(remaining_places)
 print(len(remaining_places))<prepare_x_and_y>
env = make("rps", configuration={"episodeSteps": 30}, debug=True)
 env.run([ RandomSeedSearch() , IrrationalAgent(name='pi', verbose=False), ])
 env.render(mode="ipython", width=400, height=400 )
for myplace in remaining_places:
try:
fig, axs = plt.subplots(1, 2, figsize=(15, 3))
dat = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','ConfirmedCases_Log']].dropna()
X = dat['Days_Since_Ten_Cases']
y = dat['ConfirmedCases_Log']
y = y.cummax()
dat_all = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','ConfirmedCases_Log']]
X_pred = dat_all['Days_Since_Ten_Cases']
en = ElasticNet()
en.fit(X.values.reshape(-1, 1), y.values)
 preds = en.predict(X_pred.values.reshape(-1, 1))
tt.loc[(tt['Place'] == myplace)&(tt['Days_Since_Ten_Cases'] >= 0), 'ConfirmedCases_Log_Pred1'] = preds
tt.loc[(tt['Place'] == myplace), 'ConfirmedCases_Pred1'] = tt['ConfirmedCases_Log_Pred1'].apply(np.expm1)
 pop_myplace = tt.query('Place == @myplace')['Population_final'].values[0]
tt.loc[(tt['Place'] == myplace)&(tt['ConfirmedCases_Pred1'] >(0.1 * pop_myplace)) , 'ConfirmedCases_Pred1'] =(0.1 * pop_myplace)
 ax = tt.query('Place == @myplace' ).set_index('Date')[['ConfirmedCases','ConfirmedCases_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[0])
 dat = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','Fatalities_Log']].dropna()
if len(dat)< 5:
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt.loc[(tt['Place'] == myplace)]['ConfirmedCases_Pred1'] * 0.001
elif tt.query('Place == @myplace')['Fatalities'].max() < 5:
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt.loc[(tt['Place'] == myplace)]['ConfirmedCases_Pred1'] * 0.001
else:
X = dat['Days_Since_Ten_Cases']
y = dat['Fatalities_Log']
y = y.cummax()
dat_all = tt.query('Place == @myplace and Days_Since_Ten_Cases >= 0')[['Days_Since_Ten_Cases','Fatalities_Log']]
X_pred = dat_all['Days_Since_Ten_Cases']
en = ElasticNet()
en.fit(X.values.reshape(-1, 1), y.values)
 preds = en.predict(X_pred.values.reshape(-1, 1))
tt.loc[(tt['Place'] == myplace)&(tt['Days_Since_Ten_Cases'] >= 0), 'Fatalities_Log_Pred1'] = preds
tt.loc[(tt['Place'] == myplace), 'Fatalities_Pred1'] = tt['Fatalities_Log_Pred1'].apply(np.expm1)
 pop_myplace = tt.query('Place == @myplace')['Population_final'].values[0]
tt.loc[(tt['Place'] == myplace)&(tt['Fatalities_Pred1'] >(0.0005 * pop_myplace)) , 'Fatalities_Pred1'] =(0.0005 * pop_myplace)
 ax = tt.query('Place == @myplace' ).set_index('Date')[['Fatalities','Fatalities_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[1])
 plt.show()
except:
print(f'============= FAILED FOR {myplace} =============')
 ax = tt.query('Place == @myplace' ).set_index('Date')[['ConfirmedCases','ConfirmedCases_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[0])
 ax = tt.query('Place == @myplace' ).set_index('Date')[['Fatalities','Fatalities_Pred1']].plot(figsize=(15, 5), title=myplace, ax=axs[1] )<feature_engineering>
%%time
agents = [ RandomSeedSearch(verbose=False), random_agent_unseeded ]
results = evaluate(
 "rps",
agents,
configuration={
"episodeSteps": 1000,
"tieRewardThreshold": 1,
"actTimeout": 1000,
},
num_episodes=30 
)
 results = np.array(results ).reshape(( -1,2))
results[ results == None ] = -1
totals = np.sum(results, axis=0)
 std = np.std(results, axis=0 ).round(2)
 winrate = [ np.sum(results[:,0]-20 > results[:,1]),
np.sum(results[:,0]+20 < results[:,1])]
print([ getattr(agent, '__name__', agent.__class__.__name__)for agent in agents ])
 print('winrate', winrate, '/', len(results))
print('totals ', totals)
 print('std ', std )
<sort_values><EOS> 
%%time
%%bash
GZIP=-9 tar cfz main.py.tar.gz main.py *.npy
ls -lah *.tar.gz *.npy *.py
tar -tvf *.tar.gz