<jupyter_start><jupyter_text>Trying first scenarioNot doing all scenarios at once yet, since looking up the CR for one scenario takes about 6 minutes.<jupyter_code>s0 = scenarios_encoded[0]
t = time()
cr0 = look_up_CR(s0)
print("Looking up CR: %.2fs" % (time() - t))
s0['CR'] = cr0.CR
s0_test = s0.drop(columns=['CR'], axis=1)
model = BayesianModel([('SLR', 'AE'),
                       ('VLM', 'AE'),
                       ('E', 'AE'),
                       ('E', 'LC'),
                       ('LC', 'CR'),
                       ('AE', 'CR')])
model.fit(s0, estimator=BayesianEstimator, prior_type="BDeu")<jupyter_output><empty_output><jupyter_text>Evaluating: binary predictions<jupyter_code>from sklearn.metrics import accuracy_score
binary_preds = model.predict(s0_test)
accuracy_score(s0['CR'], binary_preds)<jupyter_output><empty_output><jupyter_text>Evaluating: probability predictions<jupyter_code>from sklearn.metrics import mean_absolute_error
t = time()
n = 5000
prob_preds = model.predict_probability(s0_test[:n])
print(f"Making predictions for {n} points: %.2fs" % (time() - t))
mean_absolute_error(cr0['CR_1'][:n], prob_preds['CR_1'])
mean_absolute_error(cr0['CR_0'][:n], prob_preds['CR_0'])<jupyter_output><empty_output><jupyter_text>Smaller model<jupyter_code>model_small = BayesianModel([('LC', 'CR'), ('AE', 'CR')])
s0_small = s0[['AE', 'LC', 'CR']]
s0_test_small = s0[['AE', 'LC']]
model_small.fit(s0_small, estimator=BayesianEstimator, prior_type="BDeu")
binary_preds_small = model_small.predict(s0_test_small)
accuracy_score(s0['CR'], binary_preds_small)
t = time()
n = 1000
prob_preds_small = model_small.predict_probability(s0_test_small[:n])
print(f"Making predictions for {n} points: %.2fs" % (time() - t))
mean_absolute_error(cr0['CR_1'][:n], prob_preds_small['CR_1'])<jupyter_output><empty_output><jupyter_text>Looking up CR for all scenarios<jupyter_code># CRs = []
# t0 = time()
# for i in range(7):
#     t = time()
#     cr = look_up_CR(scenarios_encoded[i])
#     CRs.append(cr)
#     print(f"Looking up CR for scenario #{i}: %.2fs" % (time() - t))
# print("Looking up CR for all scenarios: %.2fs" % (time() - t0))<jupyter_output>Looking up CR for scenario #0: 710.26s
Looking up CR for scenario #1: 689.89s
Looking up CR for scenario #2: 702.87s
Looking up CR for scenario #3: 683.58s
Looking up CR for scenario #4: 691.71s
Looking up CR for scenario #5: 673.81s
Looking up CR for scenario #6: 667.34s
Looking up CR for all scenarios: 4819.47s<jupyter_text>Please find the corresponding CR_lookup csv files in this same folder instead of running the cell above.<jupyter_code>for i in range(7):
    CRs[i].to_csv(f"CR_lookup_{i}.csv")<jupyter_output><empty_output><jupyter_text>Selecting data for testing<jupyter_code>scenarios_encoded[0][scenarios_encoded[0].E.isin([2, 3, 4])]
pd.DataFrame(e_classes.Intervals.value_counts()).sort_index()
short_habitat = habitat_new[(habitat_new.Elevation >= -1) & (habitat_new.Elevation <= 5)]
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = short_habitat.plot(column='Elevation', ax=ax, legend=True, cmap='summer', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()
short_habitat.Habitats.value_counts()<jupyter_output><empty_output><jupyter_text>Inspecting probability predictions<jupyter_code>val = prob_preds.CR_1.value_counts().index[0]
same_prob = prob_preds[prob_preds.CR_1 == val]
same_prob
same_encoded = scenarios_encoded[0].loc[same_prob.index]
same_encoded
print(same_encoded.AE.value_counts())
print(same_encoded.LC.value_counts())<jupyter_output>1    1173
Name: AE, dtype: int64
3    1173
Name: LC, dtype: int64<jupyter_text>Trying caching the probability predictions<jupyter_code>def predict_prob_caching(model, test):
    cache_dict = {}
    prob_list = []
    for _, data_point in test.iterrows():
        keys = (data_point.AE, data_point.LC)
        if keys not in cache_dict:
            pred_df = model.predict_probability(pd.DataFrame([data_point]))
            pred_CR1 = pred_df.iloc[0].CR_1
            cache_dict[keys] = pred_CR1
        prob_list.append(cache_dict[keys])
    return pd.DataFrame(prob_list, columns=['CR_1'])
t = time()
n = 5000
prob_preds = model.predict_probability(s0_test[5000:5000+n])
print(f"Making predictions for {n} points: %.2fs" % (time() - t))
t = time()
n = 5000
prob_preds_cache = predict_prob_caching(model, s0_test[5000:5000+n])
print(f"Making predictions (with caching) for {n} points: %.2fs" % (time() - t))
np.unique(prob_preds.CR_1.values == prob_preds_cache.CR_1.values)
t = time()
n = habitat_new.shape[0]
prob_preds_cache = predict_prob_caching(model, s0_test[:n])
print(f"Making predictions (with caching) for {n} points: %.2fs" % (time() - t))
mean_absolute_error(cr0['CR_1'], prob_preds_cache['CR_1'])
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = habitat_new.plot(column='CR', ax=ax, legend=True, cmap='magma', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()
s0_geo = habitat_new[['geometry']]
s0_geo['CR_1'] = prob_preds_cache.CR_1
s0_geo['Labels'] = cr0.CR_1
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = s0_geo.plot(column='CR_1', ax=ax, legend=True, cmap='magma', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = s0_geo.plot(column='Labels', ax=ax, legend=True, cmap='magma', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()
prob_preds_cache.CR_1<jupyter_output><empty_output><jupyter_text>Scenario: 2100 high<jupyter_code>s5_geo = habitat_new[['geometry']]
s5_geo['CR_1_labels'] = CRs[5].CR_1<jupyter_output>/Users/ngochoang/opt/anaconda3/lib/python3.8/site-packages/geopandas/geodataframe.py:1322: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  super(GeoDataFrame, self).__setitem__(key, value)<jupyter_text>Training and testing a separate model on scenario 5<jupyter_code>s5 = copy.deepcopy(scenarios_encoded[5])
s5['CR'] = CRs[5].CR
model_5 = BayesianModel([('SLR', 'AE'),
                       ('VLM', 'AE'),
                       ('E', 'AE'),
                       ('E', 'LC'),
                       ('LC', 'CR'),
                       ('AE', 'CR')])
model_5.fit(s5, estimator=BayesianEstimator, prior_type="BDeu")
t = time()
n = habitat_new.shape[0]
s5_pred = predict_prob_caching(model_5, scenarios_encoded[5][:n])
print(f"Making predictions (with caching) for {n} points: %.2fs" % (time() - t))
mean_absolute_error(CRs[5].CR_1, s5_pred.CR_1)
s5_geo['CR_1_preds'] = s5_pred.CR_1
s5_geo
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = s5_geo.plot(column='CR_1_labels', ax=ax, legend=True, cmap='magma', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()
f, ax = plt.subplots(1, 1, figsize=(14, 10))
ax = s5_geo.plot(column='CR_1_preds', ax=ax, legend=True, cmap='magma', edgecolor="face", linewidth=0.)
# leg = ax.get_legend()
# leg.set_bbox_to_anchor((1.25, 1))
plt.show()<jupyter_output><empty_output><jupyter_text>Merging datasets of 7 scenarios<jupyter_code>for i in range(7):
    scenarios_encoded[i]['CR'] = CRs[i].CR
df_full = pd.concat(scenarios_encoded)
df_full.columns<jupyter_output><empty_output><jupyter_text>Training new model on full dataset<jupyter_code>model_full = BayesianModel([('SLR', 'AE'),
                            ('VLM', 'AE'),
                            ('E', 'AE'),
                            ('E', 'LC'),
                            ('LC', 'CR'),
                            ('AE', 'CR')])
t = time()
model_full.fit(df_full, estimator=BayesianEstimator, prior_type="BDeu")
print("Training model on 7 scenarios: %.2fs" % (time() - t))
df_full_test = df_full.drop(columns=['CR'], axis=1)
CR_1s = [cr.CR_1 for cr in CRs]
cr_true = pd.concat(CR_1s)
t = time()
i = 0
n = habitat_new.shape[0]
pred_0 = predict_prob_caching(model_full, df_full_test[i*n:i*n+n])
print(f"Making predictions (with caching) for scenario #{i}: %.2fs" % (time() - t))
mean_absolute_error(cr_true[i*n:i*n+n], pred_0)
t = time()
binary_preds_full = model_full.predict(df_full_test)
print("Making binary predictions for full dataset: %.2fs" % (time() - t))
accuracy_score(df_full.CR, binary_preds_full.CR)
from pgmpy.estimators import MaximumLikelihoodEstimator
model_full.fit(df_full, estimator=BayesianEstimator)
# t = time()
# i = 0
# n = habitat_new.shape[0]
# pred_0 = predict_prob_caching(model_full, df_full_test[i*n:i*n+n])
# print(f"Making predictions (with caching) for scenario #{i}: %.2fs" % (time() - t))
pred_0 = model_full.predict(df_full_test[:habitat_new.shape[0]])
accuracy_score(df_full[:habitat_new.shape[0]].CR, pred_0)
d = pd.DataFrame(data={'A': [0, 0, 1], 'B': [0, 1, 0], 'C': [1, 1, 0]})
m = BayesianModel([('A', 'C'), ('B', 'C')])
estimator = BayesianEstimator(m, d)
cpd_C = estimator.estimate_cpd('C', prior_type="dirichlet", pseudo_counts=[[1, 1, 1, 1], [2, 2, 2, 2]])
print(cpd_C)<jupyter_output>+------+------+------+------+--------------------+
| A    | A(0) | A(0) | A(1) | A(1)               |
+------+------+------+------+--------------------+
| B    | B(0) | B(1) | B(0) | B(1)               |
+------+------+------+------+--------------------+
| C(0) | 0.25 | 0.25 | 0.5  | 0.3333333333333333 |
+------+------+------+------+--------------------+
| C(1) | 0.75 | 0.75 | 0.5  | 0.6666666666666666 |
+------+------+------+------+--------------------+
{'fraction_non_alphanumeric': 0.18929490478641275, 'fraction_numerical': 0.0391147709727226, 'mean_word_length': 4.3034934497816595, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '388251', 'license': 'MIT', 'n_tokens_mistral': 3969, 'n_tokens_neox': 3725, 'n_words': 719, 'path': 'Ngoc/Training/Training data.ipynb', 'repo_name': 'sashanksilwal/CRSLR', 'size': '9715'}