<jupyter_start><jupyter_text>Learning avalanche problems by meteorological factors<jupyter_code>df_train_data2 = pd.read_csv('varsel_nordvestlandet_17_18.csv', index_col=0)

pprint(df_train_data2.columns.values)
### Remove the "2|" in column Rainfall_Average
df_train_data2 = df_train_data2[df_train_data2['Rainfall_Average'] != '2|']        
#print(df_train_data['Rainfall_Average'].unique())
for col in df_train_data2.columns.values:
    print(col, ': ', df_train_data2[col].unique())
# remove unwanted columns
df_train_target2 = df_train_data2.filter(['AvalancheProblems_0_Class_AvalancheProblemTypeId'], axis=1) 
df_train_data2 = df_train_data2.filter(['Rainfall_Most_exposed_area',
                                        'Rainfall_Average',
                                        'Wind_Speed_Num',
                                        'Wind_Direction_Num',
                                        'Temperature_Min',
                                        'Temperature_Max',
                                        'Temperature_masl',
                                        'Freezing_Level_masl'], axis=1)
# create new data columns with previous days weather data
df_train_data2['Rainfall_Most_exposed_area_-1day'] = 0 # precip on the day before

for index, row in df_train_data2.iterrows():
    try:
        df_train_data2.loc[index, 'Rainfall_Most_exposed_area_-1day'] = df_train_data2.loc[index-1, 'Rainfall_Most_exposed_area']
    except KeyError:
        print(index-1)
# get the correct target labels
with open(r'../config/snoskred_keys.json') as jdata:
    snoskred_keys = json.load(jdata)
    
enc = LabelEncoder()
label_encoder = enc.fit(df_train_target2['AvalancheProblems_0_Class_AvalancheProblemTypeId'])
print ("Categorical classes:", label_encoder.classes_)
class_names2 = []
for l in label_encoder.classes_:
    class_names2.append(snoskred_keys['Class_AvalancheProblemTypeName'][str(l)])
print(class_names2)
data2 = np.array(df_train_data2.values, dtype=float)
target2 = np.array(df_train_target2.values, dtype=float)

clf2 = tree.DecisionTreeClassifier(min_samples_leaf=8)
clf2 = clf2.fit(data2, target2)

dot_data2 = tree.export_graphviz(clf2, out_file=None,
                                feature_names = df_train_data2.columns.values,
                                class_names = class_names2,
                                #proportion = True, # show precentages instead of members
                                label = "root",
                                filled=True, rounded=True, special_characters=True                                
                                )
graph2 = graphviz.Source(dot_data2) 
graph2.render("avalanche_problem_meteo")<jupyter_output><empty_output><jupyter_text>For example on split into test and traininng data to run a prediction see Case_Nordvestlandet_Meteo_Problem.ipynb  Same, but with DecisionTreeRegressor<jupyter_code>clf2 = tree.DecisionTreeRegressor()
clf2 = clf2.fit(data, target)

dot_data2 = tree.export_graphviz(clf2, out_file=None,
                                feature_names = df_train_data.drop(['ValidFrom', 'ValidTo'], axis=1).columns.values,
                                filled=True, rounded=True, special_characters=True                                
                                )
graph2 = graphviz.Source(dot_data2) 
graph2.render("varsom_reg")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09080153800650695, 'fraction_numerical': 0.018041999408459037, 'mean_word_length': 2.605543710021322, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '357641', 'license': 'MIT', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1016, 'n_words': 164, 'path': 'aps/notebooks/varsom_tree.ipynb', 'repo_name': 'kmunve/APS', 'size': '3381'}