prediction_array=[get_prediction_all_features(all_states[i],input_features_dic,default_year) for i in range(len(all_states))]
            
            prediction_by_state={'AL': prediction_array[0],'AK':prediction_array[1],'AZ': prediction_array[2],'AR': prediction_array[3],
                'CA': prediction_array[4],'CO': prediction_array[5],'CT': prediction_array[6],'DE': prediction_array[7],
                'DC': prediction_array[8],'FL': prediction_array[9],'GA': prediction_array[10],'HI':prediction_array[11],'ID': prediction_array[12],
                'IL': prediction_array[13],'IN': prediction_array[14],'IA': prediction_array[15],'KS': prediction_array[16],
                'KY': prediction_array[17],'LA': prediction_array[18],'ME': prediction_array[19],'MD': prediction_array[20],
                'MA': prediction_array[21],'MI': prediction_array[22],'MN': prediction_array[23],'MS': prediction_array[24],
                'MO': prediction_array[25],'MT': prediction_array[26],'NE': prediction_array[27],'NV': prediction_array[28],'NH': prediction_array[29],
                'NJ': prediction_array[30],'NM': prediction_array[31],'NY': prediction_array[32],'NC': prediction_array[33],'ND': prediction_array[34],'OH': prediction_array[35],
                'OK': prediction_array[36],'OR': prediction_array[37],'PA': prediction_array[38],'RI': prediction_array[39],
                'SC': prediction_array[40],'SD': prediction_array[41],'TN': prediction_array[42],'TX': prediction_array[43],'UT': prediction_array[44],'VT': prediction_array[45],
                'VA': prediction_array[46],'WA': prediction_array[47],'WV': prediction_array[48],'WI': prediction_array[49],
                'WY': prediction_array[50]}
            
            state_xs = [us_states[key]["lons"] for key in us_states.keys()]
            state_ys = [us_states[key]["lats"] for key in us_states.keys()]

            Map = figure(toolbar_location="left",plot_width=1100, plot_height=700)
            Map.patches(state_xs, state_ys, fill_color=[color_class(prediction_by_state[key],Reds) for key in us_states.keys()],fill_alpha=0.7, line_color="black", line_width=0.5)
            Map.xgrid.grid_line_color = None
            Map.ygrid.grid_line_color = None
            Map.axis.visible = False
            show(Map)

            print('Darker shades of red in the map indicate higher percentages of unresolved homicide.')

        vis_bool=input('Would you like to see a heatmap for all selected states and features? Please enter yes or no:')
        if vis_bool=='yes':
            heatmap_generate(all_states,input_features_dic,default_year)
            print('Darker shades of red indicate a higher value for the percentage of unresolved homicide.')
from bokeh.models.widgets import CheckboxButtonGroup, Select

from pyproj import Proj, transform

# Prep data
accidents = pd.read_pickle("fars_accidents.pickle")
drivers = pd.read_pickle("final_drivers.pickle")

#us_states = us_states.data.copy()
with open("us_states.pickle", "rb") as file:
    us_states = pickle.load(file)

state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

state_names = us_states.keys()

id_to_st = {
    1: "AL",
    2: "AK",
    4: "AZ",
    5: "AR",
    6: "CA",
    8: "CO",
    9: "CT",
    10: "DE",
    11: "DC",
    12: "FL",
    13: "GA",
    15: "HI",
    16: "ID",
Exemple #3
0
from bokeh.models.widgets import CheckboxButtonGroup, Select

from pyproj import Proj, transform

# Prep data
accidents = pd.read_pickle("fars_accidents.pickle")
drivers = pd.read_pickle("final_drivers.pickle")

#us_states = us_states.data.copy()
with open("us_states.pickle", "rb") as file:
    us_states = pickle.load(file)

state_xs = [us_states[code]["lons"] for code in us_states]
state_ys = [us_states[code]["lats"] for code in us_states]

state_names = us_states.keys()

id_to_st = {1:"AL", 2:"AK", 4:"AZ", 5:"AR", 6:"CA", 8:"CO", 9:"CT", 10:"DE",
            11:"DC", 12:"FL", 13:"GA", 15:"HI", 16:"ID", 17:"IL", 18:"IN",
            19:"IA", 20:"KS", 21:"KY", 22:"LA", 23:"ME", 24:"MD", 25:"MA",
            26:"MI", 27:"MN", 28:"MS", 29:"MO", 30:"MT", 31:"NE", 32:"NV",
            33:"NH", 34:"NJ", 35:"NM", 36:"NY", 37:"NC", 38:"ND", 39:"OH",
            40:"OK", 41:"OR", 42:"PA", 44:"RI", 45:"SC", 46:"SD", 47:"TN",
            48:"TX", 49:"UT", 50:"VT", 51:"VA", 53:"WA", 54:"WV", 55:"WI",
            56:"WY"}

with open("id_to_state.pickle", "wb") as f:
    pickle.dump(id_to_st, f)

st_to_id = {v: k for k, v in id_to_st.items()}
# -*- coding: utf-8 -*-

from bokeh.sampledata import us_states
from bokeh.plotting import figure, show, output_file

us_states = us_states.data.copy()

### we'll ignore Alaska and Hawai for now. 
del us_states["HI"]
del us_states["AK"]

state_xs = [us_states[code]["lons"] for code in us_states.keys()]
state_ys = [us_states[code]["lats"] for code in us_states.keys()]

# let's run a simple loop that will paint NY, CA and FL in blue, and all the rest in red.
state_colors= []
for state in us_states.keys():
    if state=='NY' or state=="CA" or state=="FL":
        state_colors.append("blue")
    else: 
        state_colors.append("red")

output_file("us_map.html", title="us_map.py example")

p = figure(title="US States", toolbar_location="left",
    plot_width=1100, plot_height=700)

p.patches(state_xs, state_ys, fill_color=state_colors, fill_alpha=0.7,
    line_color="white", line_width=0.5)
    
show(p)
# -*- coding: utf-8 -*-

from bokeh.sampledata import us_states
from bokeh.plotting import figure, show, output_file
import csv
import os
us_states = us_states.data.copy()

### let's ignore Alaska and Hawai for now.. 
del us_states["HI"]
del us_states["AK"]


state_xs = [us_states[code]["lons"] for code in us_states.keys()]
state_ys = [us_states[code]["lats"] for code in us_states.keys()]


os.chdir("C:\\Users\\Itamar_account\\Documents\\Python Scripts\\maps")  ## set your working director here

results = dict()
with open('elections.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:   ## first column - state Code, 2nd - obama, 3rd - romney        
        state_code=row[0] 
        obama_res=row[1]  
        romney_res=row[2]
        if state_code in us_states.keys():
            if obama_res=="1":
                results[state_code] = "OBAMA"
            else:
                results[state_code] = "ROMNEY"
sld_rate={}
num1={}
denom1={}
for state_key in us_states:
    num1[state_key]=loan_defs[loan_defs['State'] == state_key.lower()]['Num 1'].sum()
    denom1[state_key]=loan_defs[loan_defs['State'] == state_key.lower()]['Denom 1'].sum()
    sld_rate[state_key] = num1[state_key]/denom1[state_key]

sld_min, sld_max=np.min(sld_rate.values()), np.max(sld_rate.values())

for state_key in us_states:
    col_idx=(sld_rate[state_key]-sld_min)/(sld_max-sld_min)
    state_colors.append(rgb_to_hex(blues_grad(col_idx)))


state_keys=us_states.keys()
state_sld_rates=np.round(np.array(sld_rate.values())*100)

source = ColumnDataSource(data=dict(
    state_key=state_keys,
    sld_rate=state_sld_rates,
    students_failed=num1.values(),
    students_entered=denom1.values()
))

TOOLS="pan,wheel_zoom,box_zoom,reset,hover,save"

p = figure(title="Student Loan Defaults - 2012", toolbar_location="left", tools=TOOLS,
    plot_width=1100, plot_height=700)

p.patches(state_xs, state_ys, fill_color=state_colors, fill_alpha=0.8,