<jupyter_start><jupyter_text>base<jupyter_code>X, Y = [], []
for _ in irange(10000):
    s = get0()
    while draw(s):
        while True:
            X.append(pack(s))
            Y.append(play(s, bot0(s)))
            if not Y[-1]: break
            shift(s)
        
X = np.r_[X]
Y = pack(np.r_[Y])
X.shape, X.dtype, Y.shape, Y.dtype            
np.savez_compressed('base.npz', X=X, Y=Y)
X = unpack(np.load('base.npz')['X'])
Y = np.unpackbits(np.load('base.npz')['Y'], count=X.shape[0]).astype(bool)
X.shape, X.dtype, Y.shape, Y.dtype
def bot_factory(X, Y):
    clf = RandomForestClassifier()
    clf.fit(X, Y)
    def bot_(s):
        return np.random.rand() > clf.predict_proba(s.reshape((1,-1)))[0,0]
    def bot(s, n=128):
        c0, c1 = 0, 0
        for _ in range(128):
            s0, s1 = s.copy(), s.copy()
            t0, t1 = 0, 0
            play(s0, False)
            play(s1, True)
            
            while draw(s0):
                while play(s0, bot_(s0)):
                    shift(s0)
                    t0 += 1
            while t0 % 5 > 0:
                shift(s0)
                t0 += 1
                    
            while play(s1, bot_(s1)):
                shift(s1)
                t1 += 1
            while draw(s1):
                while play(s1, bot_(s1)):
                    shift(s1)
                    t1 += 1
            while t1 % 5 > 0:
                shift(s1)
                t1 += 1
                
            c0 += getp(s0)[-1]
            c1 += getp(s1)[-1]
            
        return c0 < c1
    return bot_, bot

def test_bot(bots):
    s = get0()
    t = 0
    while draw(s):
        while play(s, bots[t](s)):
            shift(s)
            t += 1
            t %= 5
    while t < 5:
        shift(s)
        t += 1
    disp(s)
    for i in range(5):
        score = getp(s)
        print(i, '>', bots[i], f'> score={score[2]}, reward={score[3]}, total={score[4]}')
        shift(s)
bot1 = bot_factory(X,Y)
test_bot([bot1, bot1, bot1, bot1, bot1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15843520782396087, 'fraction_numerical': 0.041075794621026895, 'mean_word_length': 1.325, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '277103', 'license': 'MIT', 'n_tokens_mistral': 792, 'n_tokens_neox': 756, 'n_words': 162, 'path': 'lab/nothanks.ipynb', 'repo_name': 'sonjh02/nothanks-gym', 'size': '2045'}