<jupyter_start><jupyter_text>Solving rabbit reproduction programming challengehttps://www.reddit.com/r/dailyprogrammer/comments/7s888w/20180122_challenge_348_easy_the_rabbit_problem/<jupyter_code>def rabbits(init_male, init_female, target):
    
    rabbits = np.zeros((96,2))
    rabbits[2] = [init_male, init_female]
    birthrate = np.array([5, 9])
    month = 0
    
    while rabbits.sum() < target:
        fertile = rabbits[4:, 1].sum()
        newborns = fertile * birthrate
        rabbits = np.vstack([newborns, rabbits[:-1]])
        month += 1
        
    return month<jupyter_output><empty_output><jupyter_text>Simulating results and plotting relationship between input parameters and output<jupyter_code>%%time
test_target = range(1, 5000, 50)
test_female = range(1, 100, 2)
test_male = range(1, 100, 2)

results = defaultdict(list)


for i, (male, female, target) in enumerate(itertools.product(test_male, test_female, test_target)):
    if i % 100000 == 0: print(i, 'records complete') 
    results['male'].append(male)
    results['female'].append(female)
    results['target'].append(target)
    results['y'].append(rabbits(male, female, target))
df = pd.DataFrame(results)
def plot_surface(df):
    data = [
        go.Surface(
            x=df.columns, 
            y=df.index, 
            z=df.as_matrix()
        )
    ]
    layout = go.Layout(
        title='Rabbits',
        scene = dict(          
            xaxis = dict(title=df.columns.name),
            yaxis = dict(title=df.index.name),
            zaxis = dict(title='Y'),
        ),
        autosize=False,
        width=750,
        height=500,
        margin=dict(
            l=10,
            r=10,
            b=50,
            t=50
        )
    )
    fig = go.Figure(data=data, layout=layout)
    iplot(fig)<jupyter_output><empty_output><jupyter_text>We can only plot 2 dimensions vs. target output so creating three plots where we fix one of our input variables and plotting the other two vs. output "Y" (number of days needed to reach target)<jupyter_code>df_female_target = df.loc[df.male == 1, ['female','target', 'y']].pivot(index='female', columns='target', values='y')
df_male_target = df.loc[df.female == 1, ['male','target', 'y']].pivot(index='male', columns='target', values='y') 
df_male_female = df.loc[df.target == 1001, ['female','male', 'y']].pivot(index='female', columns='male', values='y') 
plot_surface(df_female_target) 
plot_surface(df_male_target) 
plot_surface(df_male_female)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1403091557669441, 'fraction_numerical': 0.028141101862861673, 'mean_word_length': 3.299829642248722, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '593555', 'license': 'MIT', 'n_tokens_mistral': 930, 'n_tokens_neox': 831, 'n_words': 169, 'path': 'rabbits.ipynb', 'repo_name': 'david-macleod/davberto', 'size': '2523'}