def drawMarkerOnMap(df): df_ = __getDfWithLoaction(df) popUpList = __makePopUpList(df_) post_locations = [post['location'] for post in popUpList] info_box_template = """ <dl> <dt>User</dt><dd>{owner_name}</dd> <dt>Is_Video</dt><dd>{is_video}</dd> <dt>Likes</dt><dd>{likes}</dd> <dt>Time</dt><dd>{time}</dd> <dt>Location Name</dt><dd>{loc_name}</dd> <dt>Comment Count</dt><dd>{comment_cnt}</dd> <dt>View</dt><dd>{video_view_count}</dd> <dt>Tags</dt><dd>{tags}</dd> </dl> """ post_info = [info_box_template.format(**post) for post in popUpList] marker_layer = gmaps.marker_layer(post_locations, info_box_content=post_info) fig = gmaps.figure(center=(37.532600, 127.024612), zoom_level=10) fig.add_layer(marker_layer) print(len(post_info)) return fig
def googleMaps(dataset): ownAPIKey= 'go_get_it_from_google_maps_api' gmaps.configure(api_key=ownAPIKey) # create the info box template info_box_template = """ <dl> <dt>Name</dt><dd>{name}</dd> <dt>ID</dt><dd>{id}</dd> <dt>Score</dt><dd>{score}</dd> <dt>Location</dt><dd>{location}</dd> <dt>Availability (%)</dt><dd>{available}</dd> <dt>URL</dt><dd>{listing_url}</dd> </dl> """ dataset.drop(columns=['description'], inplace=True) # drop description as it is too long gmap_dict= dataset.to_dict('records') # convert each row into a dictionary of the list gmap_locations =dataset['location'].to_list() # to show the markers on the map gmap_info = [info_box_template.format(**id) for id in gmap_dict] #map the gmap_dict with the info box template marker_layer = gmaps.marker_layer(gmap_locations, info_box_content=gmap_info) # create the markers to be shown on google map fig = gmaps.figure() fig.add_layer(marker_layer) # combine with the current map embed_minimal_html('map.html', views=[fig])
def myfun1(po): #argument:dataframe(df) lat_list = list(po["latitude"]) long_list = list(po["longitude"]) gmaps.configure(api_key="AIzaSyDmXhcX8z4d4GxPxIiklwNvtqxcjZoWsWU") fig = gmaps.figure() var1 = json.dumps( [{'lat': country, 'lng': wins} for country, wins in zip(lat_list, long_list)] ) markers = gmaps.marker_layer(list(zip(lat_list, long_list))) fig.add_layer(markers) data1 = embed_snippet(views=[fig]) return data1,var1
def _plot_on_gmaps(_df): _marker_locations = [] for i in range(len(_df)): _marker_locations.append( (_df['latitude'].iloc[i], _df['longitude'].iloc[i])) _fig = gmaps.figure() _markers = gmaps.marker_layer(_marker_locations) _fig.add_layer(_markers) 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 render_closures(data): # import modules for the python script: import pandas as pd import gmaps import time # layout options: draw a comfortable-sized map figure_layout = { 'width': '700px', 'height': '400px', 'padding': '1px', 'margin': '0 auto 0 auto' } # initialize map fig = gmaps.figure(layout=figure_layout) # establish list for map markers marker_coords = [] marker_labels = [] # draw road closures for index, _ in data.iterrows(): # The parameters are mostly self-explanatory, but I will note that this uses # walking directions in case start and end points are contrary to one-way streets close = gmaps.directions_layer(start=data.iloc[index][0], end=data.iloc[index][1], travel_mode='DRIVING', show_markers=False, stroke_color='Red', stroke_opacity=0.8) # add this closure's layer fig.add_layer(close) # check for label text: if data.iloc[index][2] is not None: # append label info (I'm not really happy with how these end up looking, but # we're on a time crunch. Function over form, I say!) marker_coords.append(data.iloc[index][0]) marker_labels.append(data.iloc[index][2]) # Google Maps API limits to 50 requests per second time.sleep(0.3) # if marker_labels isn't empty if marker_labels != []: # establish labeled markers marker_layer = gmaps.marker_layer(locations=marker_coords, label=marker_labels) # and add them to the map fig.add_layer(marker_layer) return fig
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 add_marker(self, fig, locations): ''' Add a 'list' of markers to the map arguments: ---------- fig - figure object locations - list of locations return: ------- fig - enriched figure object ''' marker_layer = gmaps.marker_layer(locations) fig.add_layer(marker_layer) return fig
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 map_marker(event): gmaps.configure(api_key) location=get_location(event['event_location']) event_location=[location] info_box_template = ''' <dl> <dt>Event Name</dt><dd>{event_name}</dd> <dt>Event Borough</dt>{event_borough}</dd> <dt>Event Location</dt>{event_location}</dd> <dt>Event Type</dt>{event_type}</dd> <dt>Event Borough</dt>{event_borough}</dd> <dt>Event Start</dt>{start_date_time}</dd> <dt>Event End</dt>{end_date_time}</dd> </dl> ''' event_info = [info_box_template.format(**event)] marker_layer = gmaps.marker_layer(event_location,info_box_content=event_info) fig=gmaps.figure() fig.add_layer(marker_layer) return fig
def funclu(po): lat_list = list(po["latitude"]) print(len(lat_list)) long_list = list(po["longitude"]) v_plate = list(po["plateNumber"]) v_status = list(po["status"]) gmaps.configure(api_key="AIzaSyDmXhcX8z4d4GxPxIiklwNvtqxcjZoWsWU") fig = gmaps.figure() var1 = json.dumps([{ 'lat': country, 'lng': wins, 'plate': num, 'status': v_sta } for country, wins, num, v_sta in zip( lat_list, long_list, v_plate, v_status)]) markers = gmaps.marker_layer(list(zip(lat_list, long_list))) fig.add_layer(markers) data1 = embed_snippet(views=[fig]) return data1, var1
def map_output(): # get address info user_input = request.args.get('input_address') address = user_input address += "; NY" address_coords = geocoder.google(address, key=api_key) address_point_df = pd.DataFrame(data={'geometry': \ [Point(address_coords.latlng[1], address_coords.latlng[0])]}) address_point_map = gpd.GeoDataFrame(address_point_df, geometry='geometry', crs=nbhd_map.crs) combined = gpd.tools.sjoin(address_point_map, nbhd_map, how='left') if combined.isna().loc[0,'Neighborho']: return render_template("error.html") else: address_dict = [{'input_address': user_input, 'nbhd': combined.loc[0, 'Neighborho'], 'location': (address_coords.latlng[0], address_coords.latlng[1])}] info_box_template = """ <dl> <dt>Address</dt><dd>{input_address}</dd> <dt>Neighborhood</dt><dd>{nbhd}</dd> </dl> """ address_info = [info_box_template.format(**address) for address in address_dict] address_location = [address['location'] for address in address_dict] marker_layer = gmaps.marker_layer(address_location, info_box_content=address_info, hover_text='Click for Information') # add to the figure fig.add_layer(marker_layer) # make the html map file embed_minimal_html('flaskexample/templates/export_marker.html', views=[fig]) # flask code to render output file return render_template("output.html")
def park(): ''' Annocate parking lots ''' df = pd.read_csv('parking-citations-processed.csv', usecols=['Latitude_WGS', 'Longitude_WGS'], low_memory=True) dfmark = clean_data('City_Owned_Parking_Lots.csv') parking_locations = dfmark[['Y', 'X']].loc[:1e6] locationsh = df[['Latitude_WGS', 'Longitude_WGS']] locationsh = locationsh.groupby(['Latitude_WGS', 'Longitude_WGS']) freq1 = df.groupby(['Latitude_WGS', 'Longitude_WGS']).size().reset_index(name='freq') locationsh = freq1[['Latitude_WGS', 'Longitude_WGS']] fig = gmaps.figure(center=los_angeles_coordinates, zoom_level=12) markers = gmaps.marker_layer(parking_locations) fig.add_layer(markers) fig.add_layer(gmaps.heatmap_layer(locationsh, weights=freq1['freq'])) # fig.add_layer(gmaps.traffic_layer()) return (fig)
def add_station_maps(API='AIzaSyASHzuwtrEHNRuadF-MhNbARUnSyFfRA9Q'): """ This function displays a google map of the locations of the three stations with the tidal data """ # Put in the locations of the tidal stations NB = [48 + 22.2/60, -(124 + 36.1/60)] PA = [48 + 7.5/60, -(123 + 26.5/60)] PT = [48 + 6.8/60, -(122 + 45.6/60)] latlon = [tuple(NB), tuple(PA), tuple(PT)] # Generate the google map try: import gmaps gmaps.configure(api_key=API) m = gmaps.Map() markers = gmaps.marker_layer(latlon) m.add_layer(markers) return m except ImportError: raise ImportError('Please install gmaps package') else: return None
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)
######################################################################## # Get Coordinates from Tweets related to Traffic # and then display to map ######################################################################## #df=pd.read_csv('./datasets/last24hr_tweets.csv') df = get_lat_lng(df, api_key) pd.set_option('display.max_colwidth',-1) df_no_na = df.dropna() df_no_na.to_csv('./datasets/tweet_geo_to_map.csv') gmaps.configure(api_key) fig = gmaps.figure() #fig = gmaps.figure(center=locations, zoom_level=13, map_type='ROADMAP') locations = df_no_na[['latitude','longitude']] info_box = [] for i, row in df_no_na.iterrows(): row_text = row['tweet'] #print(row_text) info_box.append(row_text) markers = gmaps.marker_layer(locations,info_box_content=info_box) fig.add_layer(markers) fig # In[ ]:
row['Country']) response = requests.get(url + append_url).json() cast_tour.loc[ index, 'Lat'] = response['results'][0]['geometry']['location']['lat'] cast_tour.loc[ index, 'Long'] = response['results'][0]['geometry']['location']['lng'] cast_tour # In[79]: marker_locations = cast_tour[['Lat', 'Long']] info_box_template = """ <dl> <dt>City</dt><dd>{City}</dd> <dt>State</dt><dd>{State}</dd> <dt>Country</dt><dd>{Country}</dd> </dl> """ # Store the DataFrame Row tour_info = [ info_box_template.format(**row) for index, row in cast_tour.iterrows() ] fig = gmaps.figure() markers = gmaps.marker_layer(marker_locations, info_box_content=tour_info) fig.add_layer(markers) fig # In[ ]:
def Demo(parking_lots): gmaps.configure(api_key=api_key) fig = gmaps.figure() markers = gmaps.marker_layer(parking_lots) fig.add_layer(markers) embed_minimal_html('templates/export.html', views=[fig])
import gmaps key = 'yourkey' gmaps.configure(api_key=key) marker_locations = [ (35.040617, 135.729071), (35.027294, 135.798195), (35.002098, 135.759172), (34.986095, 135.758799), (35.014323, 135.677888), (34.967386, 135.772682), (35.026626, 135.762361), (35.015056, 135.748239), (34.992253, 135.758629), (35.016527, 135.782448), (35.026429, 135.780822), ] fig = gmaps.figure() label = ['금각사', '은각사', '호텔', '교토역', '도월교', '후시미', '교토황궁', '니조성', '히가시 혼간지', '헤이안 신궁', '교토대학'] markers = gmaps.marker_layer(marker_locations, label=label) fig.add_layer(markers) fig
sleep(0.5) df = pd.read_json("greenPParking2015.json") for i in tqdm(range(100)): sleep(0.01) print("Data successfully collected from 206.130.170.39") print("\n") parks = df.carparks lat = [float(p['lat']) for p in parks] lng = [float(p['lng']) for p in parks] price = [float(p['rate_half_hour']) for p in parks] park_features = c = np.c_[(lat, lng)] park_locations = park_features[:, :2] park_markers = gmaps.marker_layer(park_locations) fig_plocations = gmaps.figure() fig_plocations.add_layer(park_markers) # ## Green P Parking Spots # In[4]: fig_plocations # ## Finding Clusters of Parking Locations # In[10]: park_features = c = np.c_[(lat, lng)] if factor_price:
# In[76]: figure_layout3 = { 'width': '400px', 'height': '300px', 'border': '1px solid black', 'padding': '1px', 'margin': '0 auto 0 auto' } fig3 = gmaps.figure(layout=figure_layout3) # In[77]: # Assign the marker layer to a variable markers3 = gmaps.marker_layer(coordinates3) # Add the layer to the map fig.add_layer(markers3) #fig # In[78]: fig = gmaps.figure() fig.add_layer(heat_layer) fig.add_layer(markers3) fig # ### Education vs Low IMR by counties
from ipywidgets.embed import embed_minimal_html import gmaps from requests import get url = 'https://apex.oracle.com/pls/apex/raspberrypi/weatherstation/getallstations' stations = get(url).json()['items'] gmaps.configure(api_key="PUT_YOUR_API_KEY_HERE") ws_locations = [(station['weather_stn_lat'], station['weather_stn_long']) for station in stations] info_box_template = """ <dl> <dt>Station ID</dt><dd>{weather_stn_id}</dd> <dt>Name</dt><dd>{weather_stn_name}</dd> </dl> """ ws_info = [info_box_template.format(**station) for station in stations] marker_layer = gmaps.marker_layer(ws_locations, info_box_content=ws_info) fig = gmaps.figure(zoom_level=2, center=(25, 0)) fig.add_layer(marker_layer) embed_minimal_html('stations.html', views=[fig]) fig
info_box_template = """ <dl> <dt>Hotel Name</dt><dd>{Hotel Name}</dd> <dt>City</dt><dd>{City}</dd> <dt>Country</dt><dd>{Country}</dd> <dt>Max Temp</dt><dd>{Max Temp}</dd> </dl> """ # Store the DataFrame Row hotel_info = [ info_box_template.format(**row) for index, row in hotel_df.iterrows() ] # %% # Add a heatmap of temperature for the vacation spots. locations = hotel_df[["Lat", "Lng"]] max_temp = hotel_df["Max Temp"] fig = gmaps.figure(center=(30.0, 31.0), zoom_level=1.5) heat_layer = gmaps.heatmap_layer(locations, weights=max_temp, dissipating=False, max_intensity=300, point_radius=4) marker_layer = gmaps.marker_layer(locations, info_box_content=hotel_info) fig.add_layer(heat_layer) fig.add_layer(marker_layer) # Call the figure to plot the data. fig # %%
hotel_request = requests.get(base_url, params=params) hotel_response = hotel_request.json() try: hotels.append(hotel_response["results"][0]["name"]) except: hotels.append("Nearest hotel not found") # Dataframe with nearest hotel indexed_perfect_vacation["Nearest Hotel"] = hotels indexed_perfect_vacation # Using the template add the hotel marks to the heatmap info_box_template = """ <dl> <dt>Name</dt><dd>{Nearest Hotel}</dd> <dt>City</dt><dd>{City}</dd> <dt>Country</dt><dd>{Country}</dd> </dl> """ # Store the DataFrame Row # NOTE: be sure to update with your DataFrame name hotel_info = [info_box_template.format(**row) for index, row in indexed_perfect_vacation.iterrows()] locations = indexed_perfect_vacation[["Latitude", "Longitude"]] # Add marker layer ontop of heat map markers = gmaps.marker_layer(locations, info_box_content= [f"Nearest Hotel: {hotel}" for hotel in hotels]) vaca_fig.add_layer(markers) # Display Map vaca_fig
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])
title="Activity Distribution by Month({}, {})".format(bs.city, year)) plt.show() #%% data["Weekday"] =data.starttime.dt.weekday data.groupby("Weekday").count()["starttime"].plot(x = "Weekday", y = "starttime", kind="bar", color="blue", alpha=0.6, title="Activity Distribution by Weekday({})".format(bs.city)) plt.show() #%% data.usertype.value_counts().plot(kind='pie', figsize=(6, 6), legend = True, title="Distribution of Registered / Casual Trips",) plt.show() for usertp in data.usertype.unique(): subdata = data[(data.Year > 2013) & (data.Year < 2017) & (data.usertype == usertp)] subdata.groupby("Month").count()["starttime"].plot(x = "Month", y = "starttime", kind="bar", color="blue", alpha=0.6, title="Activity Distribution by Month (Aggregated 2014-2016)({}, {})".format(bs.city, usertp)) plt.show() for usertp in data.usertype.unique(): data[data.usertype == usertp].groupby("Weekday").count()["starttime"].plot(x = "Weekday", y = "starttime", kind="bar", color="blue", alpha=0.6, title="Activity Distribution by Weekday({}, {})".format(bs.city, usertp)) plt.show() #%% station_latlngs = [bs.locate(st) for st in bs.mp.index] fig = gmaps.figure() markers = gmaps.marker_layer(station_latlngs) fig.add_layer(markers) fig
def map(self, coordinates, zoomlevel): """Return a display()-able map for coordinates""" figure = gmaps.figure(center=coordinates, zoom_level=zoomlevel) figure.add_layer(gmaps.marker_layer([coordinates])) return figure
import gmaps import gmaps.datasets from ipywidgets.embed import embed_minimal_html import pandas as pd gmaps.configure(api_key='AIzaSyCKxP9RqeiMwqU4lrS0WwNabD55fjs-XHE' ) # Fill in with your API key #marker_locations = [ # (-34.0, -59.166672), # (-32.23333, -64.433327), # (40.166672, 44.133331), # (51.216671, 5.0833302), # (51.333328, 4.25) #] df = pd.read_csv('output/UpdateTripadvisorRestaurantLocation.csv', encoding='ISO-8859-1') df.dropna(axis=0, inplace=True) marker_locations = [(lat, lng) for lat, lng in zip(df['Latitude'], df['Longitude'])] #print(marker_locations) fig = gmaps.figure(center=(18.785585, 98.981766), zoom_level=13) markers = gmaps.marker_layer(marker_locations) fig.add_layer(markers) embed_minimal_html('output/export.html', views=[fig])
gmaps.configure(api_key="AIzaSyCo99awBRG0JvRCoJC8M12-3EiAoLfElSM") fig = gmaps.figure() from PIL import Image im = Image.open("/Users/Aakash/36.070403, 68.629560, 68.673354.png") inputim = im.filter(ImageFilter.ModeFilter(8)) inputim.show() pix = inputim.load() imout = im.copy() pixout = imout.load() deltax = 68.673354 - 68.629560 locations = [] xlist = [] ylist = [] for i in range(0, inputim.size[0], 8): #x-axis search for j in range(0, inputim.size[1], 8): #y-axis search if pix[i, j][1] > 140: pixout[i, j] = (255, 0, 0) xlist = xlist + [-j / 1000 * delta + 36.070403] ylist = ylist + [i / 1000 * delta + 68.629560] for k in range(0, len(xlist), 5): locations = locations + [(xlist[k], ylist[k])] imout.show() marker = gmaps.marker_layer(locations) fig.add_layer(marker) embed_minimal_html('export3.html', views=[fig])
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