def predictSingle(self, imgFile, dataDir, ployGrid=None): ''' Predicts softmax ouput by trained model for single image and plots it imgFile has to look like: # eg: <gridNo>+<lat,long>+<imageNo_date>.jpg # eg: 60+48.4271513,-110.5611851+0_2009-06.jpg ''' xx,yy = self.readData([imgFile], dataDir) yp = self.model.predict(xx)[0] yn = list(map(lambda x:x/max(yp), yp)) dist, start, end = self.gridDist(ployGrid[np.argmax(yy[0])],ployGrid[np.argmax(yp)]) if ployGrid: mx = max(yn) mn = min(yn) plt.plot([start[1],end[1]], [start[0],end[0]], color='black', label="Distance: {} miles".format(round(dist,3))) for k,i in ployGrid.items(): if k==np.argmax(yy[0]): plt.plot(i[:,1],i[:,0],color='blue',label="Actual Grid", alpha=1) else: plt.plot(i[:,1],i[:,0],color='black', alpha=0.7) plt.fill(i[:,1],i[:,0],color='red', alpha=yn[k]) plt.legend(loc="lower left") plt.show() gPoly = [] gLine = gmaps.Line( start=start, end=end, stroke_color = 'blue' ) for grid, polygon in ployGrid.items(): gPoly.append(gmaps.Polygon( list(polygon), stroke_color='black', fill_color='red', fill_opacity=float(yn[grid]) )) fig = gmaps.figure(center=(39.50,-98.35), zoom_level=4) fig.add_layer(gmaps.drawing_layer(features=gPoly)) fig.add_layer(gmaps.drawing_layer(features=[gLine])) fig.add_layer(gmaps.symbol_layer([start], scale=3, fill_color='green',stroke_color='green', info_box_content='Expected')) fig.add_layer(gmaps.symbol_layer([end], scale=3, fill_color='yellow', stroke_color='yellow', info_box_content='Predicted: {}'.format(dist))) embed_minimal_html('gmap.html', views=fig) webbrowser.open('gmap.html',new=1) return dist
def plot_clusters(res): locations = [tuple(bs.locate(i)) for i, _, _ in res] colors = ['red' if float(coef) > 0 else 'blue' for _, coef, _ in res] fig = gmaps.figure() layer = gmaps.symbol_layer(locations, fill_color=colors, stroke_color=colors) fig.add_layer(layer) top_ratios = ratio[:len(locations)] locations = [tuple(bs.locate(i)) for i in top_ratios.index] layer = gmaps.symbol_layer(locations, fill_color='black', stroke_color='black') fig.add_layer(layer) return fig
def add_markers( self, df_items=None, max_markers=1000, color='red', size=2, opacity=1.0, ): self._check_get_view_fig() if df_items is None: df_items = self.df_items marker_locs, marker_info = self._markers_with_info( df_items, max_markers=max_markers) self.fig.add_layer( gmaps.symbol_layer(marker_locs[self.loc_cols].as_matrix(), fill_color=color, stroke_color=color, fill_opacity=opacity, stroke_opacity=opacity, scale=size, info_box_content=marker_info)) return self
def hl_street(): ''' highlight streets with the most parking citations on Google map ''' df = pd.read_csv('parking-citations-processed.csv', usecols=['Latitude_WGS', 'Longitude_WGS', 'Street Name'], low_memory=True) freq = df.groupby([ 'Street Name' ]).size().reset_index(name='freq').sort_values(by=['freq'], ascending=False) most_streetslist = list(freq['Street Name'])[0:20] freq = df.groupby([ 'Street Name', 'Latitude_WGS', 'Longitude_WGS' ]).size().reset_index(name='freq').sort_values(by=['freq'], ascending=False) freq = freq.set_index('Street Name') most_streetpoint = freq.loc[most_streetslist] most_points = list( zip(list(most_streetpoint['Latitude_WGS']), list(most_streetpoint['Longitude_WGS']))) most_points = list(set(most_points)) fig = gmaps.figure(center=los_angeles_coordinates, zoom_level=12) most = gmaps.symbol_layer(most_points[::20], fill_color='blue', stroke_color='blue', scale=3, stroke_opacity=0) fig.add_layer(most) # fig.add_layer(gmaps.traffic_layer()) return (fig)
def plot(locations): m = gmaps.Map() pts_layer = gmaps.symbol_layer( locations, fill_color="red", stroke_color="red", scale=1 ) m = gmaps.Map() m.add_layer(pts_layer) return m
def _render_map(self, initial_maxIntensity, initial_radius): # fig = gmaps.figure(map_type='HYBRID') # fig = gmaps.figure(map_type='ROADMAP') fig = gmaps.figure() locations = self._locations weights = self._probabilities self._heatmap = gmaps.heatmap_layer( locations, weights=weights, max_intensity=initial_maxIntensity, opacity=0.8, point_radius=initial_radius, dissipating=True, ) fig.add_layer(self._heatmap) l_weights = len(weights) N1 = int(l_weights / 100) # N1 = min(N1,20) # N1 = int(l_weights/2) # N1 = l_weights N1 = 1000 N1 = min(N1, len(weights)) indicesForMarkers = np.argsort(weights)[-N1:] weightsMarked = weights[indicesForMarkers] locationsMarked = locations[indicesForMarkers] textForMarked = [ 'proba: {}\nlat: {}\nlon: {}'.format(a, b[0], b[1]) for a, b in zip(weightsMarked, locationsMarked) ] self._symbolLayer = gmaps.symbol_layer( locationsMarked, hover_text=textForMarked, info_box_content=textForMarked, # stroke_opacity=1, # does not do anything # fill_opacity=1, # does not do anything stroke_color="rgba(255, 0, 0, 0.0)", # workaround for not working opacity fill_color= "rgba(255, 0, 0, 0.0)", # workaround for not working opacity scale=3) fig.add_layer(self._symbolLayer) # self._markerLayer = gmaps.marker_layer(locationsMarked, # hover_text=textForMarked, # info_box_content=textForMarked, # ) # fig.add_layer(self._markerLayer) return fig
def _render_map(self, LatitudeLongitude): """ Render the initial map """ self.symbols_locations = LatitudeLongitude PlaceCom_coordinates = (43.608536, 3.879582) place = [[43.6162094554924, 3.8744080066680895],[43.6096992492676, 3.89693999290466],[43.61465, 3.8336],[43.5907, 3.81324], [43.615741799999995, 3.9096322], [43.626611, 3.895644], [43.6266977, 3.8956288],[43.6138841, 3.8684671], [43.57926, 3.93327], [43.578829999999996, 3.93324]] fig = gmaps.figure(center=PlaceCom_coordinates, zoom_level=12) self.Gsymbols = gmaps.symbol_layer(self.symbols_locations, fill_color = 'green', stroke_color = 'green', scale = 1) staticMarkers = gmaps.marker_layer(place) fig.add_layer(self.Gsymbols) fig.add_layer(staticMarkers) return fig
def plot_clusters(self, cluster_res, n_cluster): names, labels = cluster_res locations = [tuple(self.bikesystem.locate(i)) for i in names] colormap = [ rgb2hex(i[:3]) for i in cm.rainbow(np.linspace(0, 1, n_cluster)) ] colors = [colormap[i] for i in labels] fig = gmaps.figure() layer = gmaps.symbol_layer(locations, fill_color=colors, stroke_color=colors) fig.add_layer(layer) return fig, colormap
def map_figure(dict_results, location_near, location_cheap, lat_current, lng_current, mode): locations = [(rows_result['geometry.location.lat'], rows_result['geometry.location.lng']) for rows_result in dict_results] restaurant_info = pin_template.template(dict_results) fig = gmaps.figure() now_location = [(lat_current, lng_current)] marker_layer = gmaps.marker_layer(now_location) fig.add_layer(marker_layer) symbol_layer = gmaps.symbol_layer(locations, info_box_content=restaurant_info, scale=5, stroke_color="red") index_free = int("".join([ str(integer) for integer in [i for i in range(len(locations)) if locations[i] == location_near] ])) symbol_layer.markers[index_free].stroke_color = 'blue' google_maps_route.route((lat_current, lng_current), location_near, fig, 'blue', 5.0, mode) print( "---Results retrieved! Please, find attached an html file with the map with all the info you need.---" ) if location_near != location_cheap: index_near = int("".join([ str(integer) for integer in [ i for i in range(len(locations)) if locations[i] == location_cheap ] ])) symbol_layer.markers[index_near].stroke_color = 'green' google_maps_route.route((lat_current, lng_current), location_cheap, fig, 'green', 5.0, mode) print( "\n---The cheapest gluten free restaurant is the one marked in the blue route" " and the nearest one on the green route---\n") else: print( "\n---The nearest one is also the cheapest one! The route is marked in blue---\n" ) fig.add_layer(symbol_layer) embed_minimal_html('./maps/routes.html', views=[fig])
def gmaps_plot(df_results, color_array): # Read api_key from txt file with open('gmaps_apikey.txt', 'a') as f: api_key = f.readline() gmaps.configure(api_key=api_key) #Set up your map fig = gmaps.figure() colors = list(df_results['color'].values) locations = list(zip(df_results['INTPTLAT'], df_results['INTPTLONG'])) symbols = gmaps.symbol_layer(locations, fill_color=colors, stroke_color=colors, scale=2) fig.add_layer(symbols) fig.show()
def get_gmap_figure(LAT, LON, filename = 'apikey.txt'): # Reference: https://jupyter-gmaps.readthedocs.io/en/latest/tutorial.html#basic-concepts # read google maps API with open(filename) as f: my_api_key = f.readline() f.close # get the base map gmaps.configure(api_key=my_api_key) # Fill in with your API key # zoom the map around the center center_of_all_sensors = (np.mean(LAT),np.mean(LON)) # set the figure properties figure_layout = { 'width': '600px', 'height': '600px', 'border': '1px solid black', 'padding': '1px', 'margin': '0 auto 0 auto' } # plot the base map fobj = gmaps.figure(center=center_of_all_sensors, layout=figure_layout, zoom_level=13, map_type='TERRAIN') # Note: #'ROADMAP' is the default Google Maps style, #'SATELLITE' is a simple satellite view, #'HYBRID' is a satellite view with common features, such as roads and cities, overlaid, #'TERRAIN' is a map that emphasizes terrain features. # add point locations on top of the base map locations = list(zip(LAT,LON)) # provide the latitudes and longitudes sensor_location_layer = gmaps.symbol_layer(locations, fill_color='red', stroke_color='red', scale=2) fobj.add_layer(sensor_location_layer) leuven_city_center_layer = gmaps.marker_layer(list(zip([50.879800], [4.700500]))) fobj.add_layer(leuven_city_center_layer) return fobj
def prediction(filename, location): output_list = [] image_file = filename location_input = location print(location_input) predicted_word = output(image_file) geolocator = Nominatim(user_agent="Natural Disaster") location = geolocator.geocode(location_input) print(("Location Latitude, Longitude", location.latitude, location.longitude)) print("predicted_word", predicted_word) latitude = location.latitude longitude = location.longitude if os.path.isfile("run_demo.pickle"): output_list = pickle.load(open("run_demo.pickle", "rb")) output_list.append( (image_file, location.latitude, location.longitude, predicted_word)) gmaps.configure(api_key="AIzaSyA8QY3k_68BaSlDTehnWd0Kf73h5z7cIjA") fig = gmaps.figure(map_type="SATELLITE") for image, lat, lon, prediction in output_list: color = "" if (prediction == "severe"): color = 'red' elif (prediction == "mild"): color = "yellow" else: color = "green" layer = gmaps.symbol_layer([(lat, lon)], fill_color=color, stroke_color=color, hover_text="location_input") fig.add_layer(layer) pickle.dump(output_list, open("run_demo.pickle", "wb")) new_name = os.getcwd() + '/templates/' + 'export.html' embed_minimal_html(new_name, views=[fig])
def gmap_heat_map(loc_list, info): """ Take in a list of tuples (latitude, longitude) heat map using gmap. gmap's API key need to be stored in api_keys.py in the same folder. Just put in a variable gmap_key = 'AI...' :param loc_list: List of tuples (latitude, longitude) to be plotted :type loc_list: list of tuples :param info: The string to be shown on the plot :type info: str """ import api_keys assert len(loc_list) > 0, 'Location list cannot be empty!' gmaps.configure(api_key=api_keys.gmap_key) fig = gmaps.figure(map_type='HYBRID') fig.add_layer(gmaps.heatmap_layer(loc_list, opacity=0.8, point_radius=20)) fig.add_layer( gmaps.symbol_layer([(10.8, -83.0)], fill_color='red', stroke_color='red', info_box_content=info, scale=1)) return fig
def scatterplots(): try: df['Full Location'].replace('', np.nan, inplace=True) df['Location'].replace('', np.nan, inplace=True) df.dropna(subset=['Location', 'Full Location'], inplace=True) except: print("...") lat = df['Latitude'].values lon = df['Longitude'].values """ gmap=gmplot.GoogleMapPlotter(54.02,-0.94,6.5) gmap.apikey = "AIzaSyAhzINZ3b25CuR5ldJOdouaQLRE8_lUD78" gmap.scatter(lat,lon,'#3B0B39', size=140, marker=False) gmap.draw( "scatterplotmap.html" ) """ gmaps.configure(api_key='AIzaSyAhzINZ3b25CuR5ldJOdouaQLRE8_lUD78') figure_layout = { 'width': '1600px', 'height': '900px', 'border': '1px solid black', 'padding': '1px' } fig = gmaps.figure(layout=figure_layout, map_type='TERRAIN') symbol_layer = gmaps.symbol_layer(df[['Latitude', 'Longitude']], fill_color='rgba(0, 150, 0, 0.4)', stroke_color='rgba(0, 150, 0, 0.4)', scale=2) fig.add_layer(symbol_layer) embed_minimal_html('scatterplotmap.html', views=[fig]) txt.config(state=NORMAL) txt.insert(INSERT, "\n Done! ") txt.insert(INSERT, "\n Maps exported as interactive HTML - scatterplotmap.html ") txt.config(state=DISABLED)
def plot_dropoff_distribution(): timeLocMap = ddict(list) with open('params/dropmap', 'r') as data: reader = csv.reader(data, delimiter=',') for row in reader: dot = centerize_grid(((row[0], row[1]), (row[2], row[3]))) locs = row[5].split(' ') for loc in locs: coords = loc.split(':') timeLocMap[(int(row[4]), dot)].append( (float(coords[1]), float(coords[0]))) # lat, lon gmap = gmaps.Map() start = random.choice(timeLocMap.keys()) print start print timeLocMap[start] pickup_layer = gmaps.symbol_layer([(start[1][1], start[1][0])], fill_color='green', stroke_color='green', scale=2) dropoff_layer = gmaps.Heatmap(data=timeLocMap[start]) gmap.add_layer(pickup_layer) gmap.add_layer(dropoff_layer) gmap
<dt>State Name</dt><dd>{Abbreviation}</dd> <dt>Median Income</dt><dd>{Median Household Income}</dd> <dt>Population Density</dt><dd>{Population Density}</dd> <dt>Total Cases</dt><dd>{Total Test Results}</dd> </dl> """ # Store the DataFrame Row corona_info = [ info_box_template.format(**row) for index, row in final_df.iterrows() ] #set the locations to use for the marker layer locations = final_df[["Latitude", "Longitude"]] #make the marker layer markers = gmaps.marker_layer(locations) #make the info box layer corona_markers = gmaps.symbol_layer(locations, info_box_content=corona_info) #add both of the layers to the heat map fig = gmaps.figure() fig.add_layer(markers) fig.add_layer(corona_markers) #this is used to get the max intensity for the heat map # for state in final_df: # print(final_df["Positive"]/final_df["Population"]) #create the heat Layer per_capita_infection_rate = final_df["Positive"] / final_df[ "Population"].astype(float) heat_layer = gmaps.heatmap_layer(locations, weights=per_capita_infection_rate, dissipating=False, max_intensity=.118,
#%% [markdown] # # Displaying locations with dots #%% import pandas as pd import gmaps.datasets from ipywidgets.embed import embed_minimal_html df = pd.read_csv('clusters_cologne.csv') dataframe = df[['LAT', 'LONG']] clusters_layer = gmaps.symbol_layer(dataframe, fill_color="red", stroke_color="red", stroke_opacity=0.0, scale=30, fill_opacity=0.6) fig = gmaps.figure() fig.add_layer(clusters_layer) embed_minimal_html('export.html', views=[fig]) # %% # #%% [markdown] # # # Heatmap that may be useful # #%% # # Get the dataset # earthquake_df = gmaps.datasets.load_dataset_as_df('earthquakes')
def heatmapVis(): #Get connection to crimes database conn = sql.connect("Crimes_-_2001_to_present.db") cur = conn.cursor() #select all data for chicago cur.execute( "SELECT Latitude, Longitude FROM data WHERE Year >= 2006 AND Latitude IS NOT NULL AND Longitude IS NOT NULL" ) #Get all values crimeLocation = cur.fetchall() #Cleanup db connection cur.close() conn.close() #randomly shuffles the crime location to remove bias when graphing in map np.random.seed(100) np.random.shuffle(crimeLocation) locations = [] for i in range(0, 30000, 2): locations.append(crimeLocation[i]) #Get connection to crimes database conn = sql.connect("City-Owned_Land_Inventory.db") cur = conn.cursor() #select all data for chicago cur.execute( "SELECT Latitude, Longitude FROM data WHERE Latitude IS NOT NULL AND Longitude IS NOT NULL" ) #Get all values landLocation = cur.fetchall() #Cleanup db connection cur.close() conn.close() #randomly shuffles the crime location to remove bias when graphing in map np.random.seed(100) np.random.shuffle(landLocation) landlocations = [] for i in range(0, len(landLocation), 2): landlocations.append(landLocation[i]) gmaps.configure(api_key=GOOGLE_MAPS_API_KEY) #centers map on Illinois illinoisLocation = (41.881832, -87.623177) fig = gmaps.figure(center=illinoisLocation, zoom_level=10) #creates heat map from crime locations crimeHeatMap = gmaps.heatmap_layer(locations) #creates marker from land location landSymbol = gmaps.symbol_layer(landlocations, fill_color="rgba(0,0,200,0.4)", stroke_color="rgba(0,0,200,0.4)", scale=2) #plots everything on figure fig.add_layer(crimeHeatMap) fig.add_layer(landSymbol) return fig
<dl> <dt>Name</dt><dd>{address}</dd> <dt>Ave. unitprice</dt><dd>{Ave_unitprice}</dd> </dl> """ '''Storing address and ave unitprice into a list named unit_price_list''' '''This list will be used to show data in the template info_box_template we've set up before''' unit_price_list = [] for i in select_db(db_name, cmd_getting_ave): unit_price_dict = dict() unit_price_dict['address'] = i[0] unit_price_dict['Ave_unitprice'] = i[1] # print(unit_price_dict) unit_price_list.append(unit_price_dict) # print(unit_price_list) house_price_info = [info_box_template.format(**i) for i in unit_price_list] '''Plotting a figure of googlemap which could demonstrate each communities by coordinates collected from googlemap''' '''Second, demonstrating each communities house_price and address in the info_box_content''' fig = gmaps.figure() markers = gmaps.symbol_layer(marker_locations, info_box_content=house_price_info, fill_color='green', stroke_color='green', scale=3) fig.add_layer(markers) fig # In[ ]:
def steps(src, from_dt, to_dt): import sys MODULES_PATH = '../code/' if MODULES_PATH not in sys.path: sys.path.append(MODULES_PATH) import mfuncs import pandas as pd import numpy as np from tqdm import tqdm tqdm.pandas() pd.options.display.max_columns = 1000 import lightgbm as lgb from sklearn.neighbors import NearestNeighbors # start of step 01 df_train = pd.read_csv('../data/train_set.csv') df_test = pd.read_csv('../data/test_set.csv') rnm = { 'atm_address_lat': 'atm_lat', 'atm_address_lon': 'atm_lon', 'pos_adress_lat': 'pos_lat', 'pos_adress_lon': 'pos_lon', 'home_add_lat': 'home_lat', 'home_add_lon': 'home_lon', 'work_add_lat': 'work_lat', 'work_add_lon': 'work_lon', } df_train.rename(columns=rnm, inplace=True) df_test.rename(columns=rnm, inplace=True) # start of step 02 df_train['target_work'] = df_train.progress_apply(mfuncs.add_poswork_target, axis=1) df_train['target_home'] = df_train.progress_apply(mfuncs.add_poshome_target, axis=1) # start of step 03 df_train.to_csv('../data/train_1.csv', index=None) # start of step 04 df_train.info() # start of step 05 df_train.head() # start of step 06 df_train.country.value_counts(normalize=True)[:10] print(df_train.shape, df_test.shape) df_train = df_train[df_train.country.isin(['RUS', 'RU'])] df_test = df_test[df_test.country.isin(['RUS', 'RU'])] print(df_train.shape, df_test.shape) del df_train['country'], df_test['country'] # start of step 07 print(df_train.shape, df_train.currency.value_counts(normalize=True)) df_train = df_train[df_train.currency == 643] print(df_train.shape) del df_train['currency'] # start of step 08 print(df_train.shape, df_train.currency.value_counts(normalize=True)) df_train = df_train[df_train.currency == 643] print(df_train.shape) del df_train['currency'] # start of step 09 print(df_train.shape) gb = df_train.groupby('customer_id')['work_lat'].agg('nunique') cid_incorrect = gb[gb == 2].index df_train = df_train[~df_train.customer_id.isin(cid_incorrect.values)] print(df_train.shape) gb = df_train.groupby('customer_id')['home_lat'].agg('nunique') cid_incorrect = gb[gb == 2].index df_train = df_train[~df_train.customer_id.isin(cid_incorrect.values)] print(df_train.shape) # start of step 10 print(df_train.shape) df_train = df_train[df_train[['atm_lat', 'pos_lat']].isnull().sum(axis=1) == 1] print(df_train.shape) df_train['type'] = 'atm' df_train.loc[~df_train['pos_lat'].isnull(), 'type'] = 'pos' df_train['type'].value_counts() # start of step 11 cid = df_train.sample(1)['customer_id'].values[0] df_an = df_train[df_train.customer_id == cid] df_point_dup = df_an.groupby(['pos_lat', 'pos_lon']).agg('size').reset_index() df_point_dup.columns = ['pos_lat', 'pos_lon', 'pos_customer_freq'] df_an = pd.merge(df_an, df_point_dup, on=['pos_lat', 'pos_lon'], how='left') df_an.head() # start of step 12 df_train.head() df_train[df_train.type == 'pos'].drop_duplicates(['pos_lat', 'pos_lon']).groupby(['terminal_id']).agg('size').value_counts() df_train[df_train.type == 'atm'].drop_duplicates(['atm_lat', 'atm_lon']).groupby(['terminal_id']).agg('size').value_counts() df_train[df_train.terminal_id == '1e15d02895068c3a864432f0c06f5ece']['atm_address'].unique() df_train[df_train.type == 'atm'].drop_duplicates(['atm_lat', 'atm_lon']).groupby(['terminal_id']).agg('size') import gmaps API_KEY = 'AIzaSyCG_RL0_kavuEaJAqEN5xXbU4h0VJUbA9M' gmaps.configure(api_key=API_KEY) # Your Google API key cid = '0dc0137d280a2a82d2dc89282450ff1b' cid = df_train.sample(1)['customer_id'].values[0] df_an = df_train[df_train.customer_id == cid] center_home = df_an[['home_lat', 'home_lon']].drop_duplicates().values center_work = df_an[['work_lat', 'work_lon']].drop_duplicates().values points_pos = df_an[['pos_lat', 'pos_lon']].dropna().values points_atm = df_an[['atm_lat', 'atm_lon']].dropna().values print(center_home.shape, center_work.shape, points_pos.shape, points_atm.shape) gmap = gmaps.Map() if len(points_pos) > 0: gmap.add_layer(gmaps.symbol_layer(points_pos, hover_text='pos', fill_color="blue", stroke_color="blue", scale=3)) if len(points_atm) > 0: gmap.add_layer(gmaps.symbol_layer(points_atm, hover_text='atm', fill_color="red", stroke_color="red", scale=3)) if not np.isnan(center_home)[0][0]: gmap.add_layer(gmaps.marker_layer(center_home, label='home')) if not np.isnan(center_work)[0][0]: gmap.add_layer(gmaps.marker_layer(center_work, label='work')) gmap center_home = df_train[['home_lat', 'home_lon']].dropna().values center_work = df_train[['work_lat', 'work_lon']].dropna().values gmap = gmaps.Map() gmap.add_layer(gmaps.symbol_layer(center_home, fill_color="red", stroke_color="red")) gmap np.isnan(center_home) df_train.groupby(['customer_id']).agg('size').sort_values().value_counts() df_test.customer_id.drop_duplicates().isin(df_train.customer_id.unique()).mean() df_train['duplicated'] = df_train.duplicated() df_pos = df_train[df_train['type'] == 'pos'] # target == pos in df_pos['target_work'] = df_pos.progress_apply(mfuncs.add_poswork_target, axis=1) df_pos['target_home'] = df_pos.progress_apply(mfuncs.add_poshome_target, axis=1) df_pos['target_work'].mean(), df_pos['target_home'].mean() df_pos.to_csv('../data/df_pos.csv', index=None) df_pos = pd.read_csv('../data/df_pos.csv') df_point_dup = df_pos.groupby(['customer_id', 'pos_lat', 'pos_lon']).agg('size').reset_index() df_point_dup.columns = ['customer_id', 'pos_lat', 'pos_lon', 'pos_customer_freq'] df_pos = pd.merge(df_pos, df_point_dup, on=['customer_id', 'pos_lat', 'pos_lon'], how='left') dfs = [] for cid in tqdm(df_pos.customer_id.unique()): df_an = df_pos[df_pos.customer_id == cid] df_an = mfuncs.add_dist_to_neighbours(df_an) dfs.append(df_an) df_pos['transaction_date'] = pd.to_datetime(df_pos['transaction_date'], format='%Y-%m-%d') df_pos['month'] = df_pos.transaction_date.dt.month df_pos['day'] = df_pos.transaction_date.dt.day df_pos['dayofyear'] = df_pos.transaction_date.dt.dayofyear df_pos['dayofweek'] = df_pos.transaction_date.dt.dayofweek df_pos.transaction_date.dtype df_gb = df_pos.groupby('customer_id') coord_stat_df = df_gb[['amount', 'pos_lat', 'pos_lon']].agg(['mean', 'max', 'min']) coord_stat_df['transactions_per_user'] = df_gb.agg('size') coord_stat_df.columns = ['_'.join(col).strip() for col in coord_stat_df.columns.values] coord_stat_df.reset_index(inplace=True) df_pos = pd.merge(df_pos, coord_stat_df, on='customer_id', how='left') cols = ['pos_lat', 'pos_lon'] types = ['min', 'max', 'mean'] for c in cols: for t in types: df_pos['{}_diff_{}'.format(c, t)] = np.abs(df_pos[c] - df_pos['{}_{}'.format(c, t)]) df_pos = pd.concat([df_pos, pd.get_dummies(df_pos['mcc'], prefix='mcc')], axis=1) del df_pos['mcc'] df_pos.head() drop_cols = ['customer_id', 'terminal_id', 'target_home', 'target_work', 'atm_address', 'pos_address', 'work_add_lat', 'work_add_lon', 'home_add_lat', 'home_add_lon', 'city', 'type', 'transaction_date'] drop_cols += ['atm_address', 'atm_address_lat', 'atm_address_lon'] df_pos.drop(drop_cols, 1, errors='ignore').head() # drop_cols = ['pos_address', 'pos_address_lat', 'pos_address_lon'] from sklearn.model_selection import train_test_split, StratifiedKFold, KFold df_pos_id = df_pos.customer_id.drop_duplicates().reset_index(drop=True) skf_id = list(KFold(n_splits=5, shuffle=True, random_state=15).split(df_pos_id)) skf = [] for train_ind, test_ind in skf_id: train_ind_ = df_pos[df_pos.customer_id.isin(df_pos_id.loc[train_ind].values)].index.values test_ind_ = df_pos[df_pos.customer_id.isin(df_pos_id.loc[test_ind].values)].index.values skf.append([train_ind_, test_ind_]) df_pos['target_work'].mean() df_pos.head() cid = '442fd7e3af4d8c3acd7807aa65bb5e85' df_an = df_pos[df_pos.customer_id == cid] df_an = mfuncs.add_dist_to_neighbours(df_an) df_pos.customer_id.unique if np.array([1]).size: print(1) lgb_train = lgb.Dataset(df_pos.drop(drop_cols, 1, errors='ignore'), df_pos['target_home']) params = { 'objective': 'binary', 'num_leaves': 511, 'learning_rate': 0.05, # 'metric' : 'error', 'feature_fraction': 0.8, 'bagging_fraction': 0.8, 'bagging_freq': 1, 'num_threads': 12, 'verbose': 0, } gbm = lgb.cv(params, lgb_train, num_boost_round=2000, folds=skf, verbose_eval=10, early_stopping_rounds=500) df_pos.loc[i2].shape i1, i2 = skf[0] df_pos[df_pos.loc[i1]]['customer_id'].unique df_pos.loc[i1] df_pos.dtypes res = df_pos return res
def createHeatMap(self): gmaps.configure(api_key='API KEY HERE') # covid19_data=requests.get('https://api.rootnet.in/covid19-in/stats/latest').json() covid19_data=self.summaryData() covid19_contact=requests.get('https://api.rootnet.in/covid19-in/contacts').json() #covid19_data state_data={'Andaman and Nicobar Islands': {'coordinates': [11.7400867, 92.6586401]}, 'Andhra Pradesh': {'coordinates': [15.9128998, 79.7399875]}, 'Arunachal Pradesh': {'coordinates': [28.2179994, 94.7277528]}, 'Assam': {'coordinates': [26.2006043, 92.9375739]}, 'Bihar': {'coordinates': [25.0960742, 85.31311939999999]}, 'Chandigarh': {'coordinates': [30.7333148, 76.7794179]}, 'Chhattisgarh': {'coordinates': [21.2786567, 81.8661442]}, 'Dadra And Nagar Haveli': {'coordinates': [20.1808672, 73.0169135]}, 'Delhi': {'coordinates': [28.7040592, 77.10249019999999]}, 'Goa': {'coordinates': [15.2993265, 74.12399599999999]}, 'Haryana': {'coordinates': [29.0587757, 76.085601]}, 'Himachal Pradesh': {'coordinates': [31.1048294, 77.17339009999999]}, 'Jammu and Kashmir': {'coordinates': [34.0233028, 75.7738873]}, 'Ladakh': {'coordinates': [34.2996176, 78.2931706]}, 'Jharkhand': {'coordinates': [23.6101808, 85.2799354]}, 'Karnataka': {'coordinates': [15.3172775, 75.7138884]}, 'Kerala': {'coordinates': [10.8505159, 76.2710833]}, 'Lakshadweep': {'coordinates': [10.3280265, 72.78463359999999]}, 'Madhya Pradesh': {'coordinates': [22.9734229, 78.6568942]}, 'Maharashtra': {'coordinates': [19.7514798, 75.7138884]}, 'Manipur': {'coordinates': [24.6637173, 93.90626879999999]}, 'Meghalaya': {'coordinates': [25.4670308, 91.366216]}, 'Mizoram': {'coordinates': [23.164543, 92.9375739]}, 'Nagaland': {'coordinates': [26.1584354, 94.5624426]}, 'Odisha': {'coordinates': [20.9516658, 85.0985236]}, 'Puducherry': {'coordinates': [11.9415524, 79.8082865]}, 'Punjab': {'coordinates': [31.1471305, 75.34121789999999]}, 'Rajasthan': {'coordinates': [27.0238036, 74.21793260000001]}, 'Sikkim': {'coordinates': [27.5329718, 88.5122178]}, 'Tamil Nadu': {'coordinates': [11.1271225, 78.6568942]}, 'Tripura': {'coordinates': [23.9408482, 91.9881527]}, 'Uttar Pradesh': {'coordinates': [26.8467088, 80.9461592]}, 'Uttarakhand': {'coordinates': [30.066753, 79.01929969999999]}, 'West Bengal': {'coordinates': [22.9867569, 87.8549755]}, 'Telengana': {'coordinates': [18.1124372, 79.01929969999999]}, 'Gujarat': {'coordinates': [22.258652, 71.1923805]}} for state in state_data: state_data[state]['weight']=0 state_data[state]['stat']='<u>{}</u><br><br><b>NO INFO ON YET!!</b>'.format(state) # In[56]: if covid19_data['success']==True: for state in covid19_data['data']['regional']: state_data[state['loc']]['weight']=(state['confirmedCasesIndian']+state['confirmedCasesForeign']) state_data[state['loc']]['stat']="<u>{}</u><br><br><b>Confimed Cases Indian:</b> {}<br><br><b>Confirmed Cases Foreign:</b> {}<br><br><b>Deaths:</b> {}<br><br><b>Discharged:</b> {}".format(state['loc'],state['confirmedCasesIndian'],state['confirmedCasesForeign'],state['deaths'],state['discharged']) if covid19_contact['success']==True: for state in covid19_contact['data']['contacts']['regional']: if state['loc'] in state_data: state_data[state['loc']]['stat']+="<br><br><b>Contact Detail:</b> {}<br>".format(state['number']) # In[58]: locations=[state['coordinates'] for state in state_data.values() ] locations # In[59]: weights=[state['weight'] for state in state_data.values() ] weights # In[64]: fig = gmaps.figure(center=(23.5936832, 78.962883), zoom_level=4) heatmap=gmaps.heatmap_layer(locations, weights=weights) heatmap.point_radius=45 fig.add_layer(heatmap) symbol_layer=gmaps.symbol_layer(locations,info_box_content=[state['stat'] for state in state_data.values()]) fig.add_layer(symbol_layer) # In[66]: embed_minimal_html('./templates/export.html', views=[fig])
#Slice the geo data oakland_df_geo = oakland_df[['start_station_latitude','start_station_longitude']] oakland_df_geo # In[58]: #use gmaps with my api key to create a map. gmaps.configure(api_key='secret') #plot our data locations = oakland_df_geo fig = gmaps.figure(map_type='ROADMAP') scatter_locations = gmaps.symbol_layer(locations,fill_color = "#2A65B1",stroke_color="#2A65B1",scale=2) fig.add_layer(scatter_locations) fig # <p><strong>Observations:</strong></p> # <ul> # <li>Using gmaps is much more efficient for this application.</li> # <li>We notice there are quite a few cities in the bay area that have excluded themselves from the program.</li> # </ul> # <p><strong>Question:</strong></p> # <ul> # <li>Where are the most popular start stations? (Top 10)</li> # <li>Where are the least popular start stations? (Top 10)</li> # </ul>
else: locations.append(loc) rows.append(row) figure_layout = {'height': '900px'} info_box_template = """ <dl> <dt>Name</dt><dd>{name}</dd> <dt>Website</dt><dd>{website}</dd> </dl> """ health_check_text = [item["name"] for item in rows] health_check_content = [info_box_template.format(**item) for item in rows] health_check_layer = gmaps.marker_layer(locations, hover_text=health_check_text, info_box_content=health_check_content) police_text = [item["name"] for item in police_rows] police_content = [info_box_template.format(**item) for item in police_rows] police_layer = gmaps.symbol_layer(police_location, fill_color="#006699", stroke_color="#006699", hover_text=police_text, info_box_content=police_content) fig = gmaps.figure(layout=figure_layout) fig.add_layer(health_check_layer) fig.add_layer(police_layer) embed_minimal_html('map.html', views=[fig])
import gmaps import matplotlib.pyplot as plt gmaps.configure(api_key='AIzaSyBNlCUex3Z4GcGNl-KU_35ccUPXmy0v5IM') negative_tweets = df[df['Sentiment'] == 0] negative_tweets_location = negative_tweets[['Latitude', 'Longitude']] neutral_tweets = df[df['Sentiment'] == 1] neutral_tweets_location = neutral_tweets[['Latitude', 'Longitude']] positive_tweets = df[df['Sentiment'] == 2] positive_tweets_location = positive_tweets[['Latitude', 'Longitude']] negative_layer = gmaps.symbol_layer(list(negative_tweets_location.values), fill_color='rgba(255, 0, 0, 0.8)', stroke_color='rgba(255, 0, 0, 0.8)', scale=5) neutral_layer = gmaps.symbol_layer(list(neutral_tweets_location.values), fill_color='rgba(250, 250, 210, 0.8)', stroke_color='rgba(255, 255, 102, 0.8)', scale=5) positive_layer = gmaps.symbol_layer(list(positive_tweets_location.values), fill_color='rgba(0, 100, 0, 0.8)', stroke_color='rgba(0, 100, 0, 0.8)', scale=5) #Green Color For POsitive tweets #Red Color For Negative Tweets #YelloW Color For Neutral Tweets fig = gmaps.figure()
def predictSingle(self, imgFile, dataDir, ployGrid): ''' Predicts softmax ouput by trained model for single image and plots it mgFiles: String that contains test location image triplet folder name. String has to look like: # <gridNo>+<lat,long> # 60+48.4271513,-110.5611851 dataDir: Directory that stores combined image files eg: "/dataCombinedSamples/" polyGrid: List of polygons that contain make up the USA split into grids. It can be loaded from eg: "infoExtraction/usaPolyGrid.pkl" ''' # read image triplets from single file xx, yy = self.readData([imgFile], dataDir) # predict single image triplet yp = self.model.predict(xx)[0] # normalize prediction for better visualization yn = list(map(lambda x: x / max(yp), yp)) # evaluate distance for single point dist, start, end = self.gridDist(ployGrid[np.argmax(yy[0])], ployGrid[np.argmax(yp)]) mx = max(yn) mn = min(yn) # plot result using matplotlib plt.plot([start[1], end[1]], [start[0], end[0]], color='black', label="Distance: {} miles".format(round(dist, 3))) for k, i in ployGrid.items(): if k == np.argmax(yy[0]): plt.plot(i[:, 1], i[:, 0], color='blue', label="Actual Grid", alpha=1) else: plt.plot(i[:, 1], i[:, 0], color='black', alpha=0.7) plt.fill(i[:, 1], i[:, 0], color='red', alpha=yn[k]) plt.legend(loc="lower left") plt.show() # plot result using google maps API gPoly = [] gLine = gmaps.Line(start=start, end=end, stroke_color='blue') for grid, polygon in ployGrid.items(): gPoly.append( gmaps.Polygon(list(polygon), stroke_color='black', fill_color='red', fill_opacity=float(yn[grid]))) fig = gmaps.figure(center=(39.50, -98.35), zoom_level=4) fig.add_layer(gmaps.drawing_layer(features=gPoly)) fig.add_layer(gmaps.drawing_layer(features=[gLine])) fig.add_layer( gmaps.symbol_layer([start], scale=3, fill_color='green', stroke_color='green', info_box_content='Expected')) fig.add_layer( gmaps.symbol_layer([end], scale=3, fill_color='yellow', stroke_color='yellow', info_box_content='Predicted: {}'.format(dist))) # save and display html page containing google maps API embed_minimal_html('gmap.html', views=fig) webbrowser.open('gmap.html', new=1) return dist
if factor_price: X = np.column_stack((X, np.zeros((len(X), 1)))) if factor_space: X = np.column_stack((X, np.zeros((len(X), 2)))) print("Identifying clusters based on user's historial parking data") sleep(0.5) kmeans = KMeans(n_clusters=4, random_state=0).fit(X) Xmean = kmeans.cluster_centers_ for i in tqdm(range(100)): sleep(0.02) print("successfully identified 4 clusters") print("\n") Xm = Xmean[:, :2] Xm_sym = gmaps.symbol_layer(Xm, fill_color='black', scale=2) fig_hm_cnt = gmaps.figure() fig_hm_cnt.add_layer(X1_hm) fig_hm_cnt.add_layer(X2_hm) fig_hm_cnt.add_layer(X3_hm) fig_hm_cnt.add_layer(X4_hm) fig_hm_cnt.add_layer(Xm_sym) # ## Clusters of Historical User Locations # In[6]: fig_hm_cnt # ## Finding Optimum Parking Spots
def gmap(): gmaps.configure(api_key="") geolocator = GoogleV3(api_key="") # 開啟CSV讀首行參數 with open(selection_file_name, newline='') as csvfile: reader = csv.reader(csvfile) for i, rows in enumerate(reader): if i == 0: row = rows # 開啟CSV 讀編號 with open(selection_file_name, newline='') as csvFile: reader = csv.reader(csvFile) number_index = row.index('編號') cloumn_number = [row[number_index] for row in reader] # 開啟CSV 讀地址 with open(selection_file_name, newline='') as csvFile: reader = csv.reader(csvFile) address_index = row.index('上班地點') cloumn_address = [row[address_index] for row in reader] # 開啟CSV讀職務名稱 with open(selection_file_name, newline='') as csvFile: reader = csv.reader(csvFile) jobname_index = row.index('職務名稱') cloumn_jobname = [row[jobname_index] for row in reader] # 開啟CSV讀公司名稱 with open(selection_file_name, newline='') as csvFile: reader = csv.reader(csvFile) comname_index = row.index('公司名稱') cloumn_jobcompany = [row[comname_index] for row in reader] # 開啟CSV讀職務連結 with open(selection_file_name, newline='') as csvFile: reader = csv.reader(csvFile) joburl_index = row.index('職務連結') cloumn_joburl = [row[joburl_index] for row in reader] # 住家地址轉經緯度 print('住家地址') home = geolocator.geocode(str(input())) home_lat, home_lon = home.latitude, home.longitude home_address = [] home_address.append((home_lat, home_lon)) # 將地址轉成經緯度 locations = [] loc_lon_lis = [] for i in range(1, len(cloumn_address)): location = geolocator.geocode(cloumn_address[i]) loc_lat, loc_lon = location.latitude, location.longitude if loc_lon in loc_lon_lis: while loc_lon in loc_lon_lis: loc_lon = loc_lon * 1.0000015 loc_lon_lis.append(loc_lon) else: loc_lon_lis.append(loc_lon) locations.append([loc_lat, loc_lon]) # 創立joblist存marker資訊 joblist = [] dict1 = {} for i in range(1, len(cloumn_address)): dict1['編號'] = cloumn_number[i] dict1['職務名稱'] = cloumn_jobname[i] dict1['地址'] = cloumn_address[i] dict1['公司名稱'] = cloumn_jobcompany[i] dict1['職務連結'] = cloumn_joburl[i] joblist.append(dict1) dict1 = {} # 將資訊轉成html格式 info_box_template = """ <dl> <dt>編號</dt><dd>{編號}</dd> <dt>職務名稱</dt><dd>{職務名稱}</dd> <dt>公司名稱</dt><dd>{公司名稱}</dd> <dt>地址</dt><dd>{地址}</dd> <a href="{職務連結}"target="_blank">職務連結</a> </dl> """ # 建立hover text job_all_info = [] jobname = '職缺名稱:' jobcompany = ' \n公司名稱:' for i in range(1, len(cloumn_address)): jobname += cloumn_jobname[i] jobcompany += cloumn_jobcompany[i] result = jobname + jobcompany job_all_info.append(result) jobname = '職缺名稱:' jobcompany = ' \n公司名稱:' result = '' # 將html分別放入每個職缺 job_infobox = [info_box_template.format(**job) for job in joblist] # 畫座標資訊 marker_layer = gmaps.marker_layer(locations, hover_text=job_all_info, info_box_content=job_infobox) global fig fig = gmaps.figure() fig.add_layer(marker_layer) symbols = gmaps.symbol_layer(home_address, fill_color='yellow', stroke_color='blue', scale=10) fig.add_layer(symbols)
# In[ ]: # Store the DataFrame Row # NOTE: be sure to update with your DataFrame name hotel_info = [ info_box_template.format(**row) for index, row in IdealWeather.iterrows() ] locations = hotel_df[["Lat", "Lng"]] # In[113]: #Hotel map hotel_layer = gmaps.symbol_layer(locations, fill_color='rgba(0, 150, 0, 0.4)', stroke_color='rgba(0, 0, 150, 0.4)', scale=2, info_box_content=[ info_box_template.format(**row) for index, row in IdealWeather.iterrows() ]) fig = gmaps.figure() fig.add_layer(hotel_layer) fig.add_layer(heat_layer) fig # In[ ]: # In[ ]: # In[ ]: # In[ ]:
(41.673009, 45.007487), (41.656818, 44.627870)] DidiDigomiPLY = [(41.801125, 44.726993), (41.821502, 44.776487), (41.814003, 44.784271), (41.787966, 44.784622), (41.787665, 44.770549), (41.760436, 44.768482), (41.766687, 44.709715)] DidiDigomi = [] poly = Polygon(TbilisiPLY) for Clat, Clng in coordinates: if poly.contains(Point(Clat,Clng)) == True: DidiDigomi.append([Clat,Clng]) for i in enumerate(): gmaps.configure(api_key=api_key) fig = gmaps.figure() fig.add_layer(gmaps.drawing_layer(features=[gmaps.Polygon(tbilisi_ply, stroke_color='red', fill_color=(255, 0, 132)) ])) fig.add_layer(gmaps.drawing_layer(features=[gmaps.Polygon(saburtalo_ply, stroke_color='blue', fill_color=(255, 0, 132)) ])) heatmap_layer = gmaps.heatmap_layer(coords) fig.add_layer(heatmap_layer) markers = gmaps.symbol_layer(coors, fill_color='green', stroke_color='green', scale=2) fig.add_layer(markers) embed_minimal_html('export.html', views=[fig])
size = int(row[2] / 5) if size == 0: size = 1 champ = row[3] newValue = int((champ * 255) / maxNum) r = newValue g = 0 b = 255 - newValue string1 = ('rgba(%d, %d, %d, 0.7)'% (r, g, b)) string2 = ('rgba(%d, %d, %d, 0)'% (r, g, b)) kfc_layer = gmaps.symbol_layer( location, fill_color=string1, stroke_color=string2, scale=size ) fig.add_layer(kfc_layer) fig teamData = allData[["key", "OPR", "DPR", "CCWM", "RankingPoints", "Wins", "Losses", "Ties"]] teamData = teamData.set_index("key") # print(champTeams) champKey = champTeams.set_index("key") final = champKey.join(teamData) final = final[["OPR", "DPR", "CCWM", "RankingPoints", "Wins", "Losses", "Ties"]]