Ejemplo n.º 1
0
    clf = tree.DecisionTreeRegressor(max_depth=n+1, min_samples_leaf=min_samples_leaf, random_state=0)
    clf.fit(X, y)
    pred_arr[:,n] = clf.predict(fips_data[['longitude','latitude']].values)
    
#%% generate pngs for reg-tree at each depth value
image_list_path = os.path.join(movie_dir,'image_list.txt')
F = open(image_list_path,'wb+')
pred_prc = scipy.percentile(pred_arr,[10, 90]) 
prc_range = pred_prc[1]-pred_prc[0]  # overall range of data

# MAKE COLORBAR FIGURE WITH FIXED RANGE
temp = pred_arr[:,-1].copy()
cur_range = scipy.percentile(temp,[10,90])  # range for this map
temp  = (temp - cur_range[0])*prc_range/np.diff(cur_range) + pred_prc[0]  # scale to have same range
fips_data['preds'] = temp
new_data = LCH.fill_missing_fips(ktree,map_coords,fips_data)
cbar_fig = LCH.paint_map(new_data['preds'], base_map, county_paths, fips_to_zip, color = 'cube', get_cbar=True)  
plt.savefig(movie_dir + 'tree_cbar.png', dpi=500, format='png')
plt.close()

for n in xrange(max_levels):
    cur_range = scipy.percentile(pred_arr[:,n],[10,90])  # range for this map
    pred_arr[:,n]  = (pred_arr[:,n] - cur_range[0])*prc_range/np.diff(cur_range)  # scale to have same range
    fips_data['preds'] = pred_arr[:,n]
    new_data = LCH.fill_missing_fips(ktree,map_coords,fips_data)
    LCH.paint_map(new_data['preds'], base_map, county_paths, fips_to_zip, color = 'cube', get_cbar=False)        
    title_path.string = 'Depth {} Tree'.format(n)
    bytestring = base_map.encode()
    out_file = os.path.join(movie_dir,'tree_depth%d.png' % n)
    cairosvg.svg2png(bytestring=bytestring, write_to = out_file)
    F.write(out_file + '\n')