๐Ÿ”Ž ๋‚˜์™€ ์ œ์ผ ๊ฐ€๊นŒ์šด ๋ฌด๋ฃŒ WiFi ๐Ÿ”

parkeuยท2022๋…„ 9์›” 5์ผ
0

ABC๋ถ€ํŠธ์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
18/55

๐Ÿผ ์ค€๋น„

# ํ•œ๊ธ€๊นจ์ง๋ฐฉ์ง€
# ํ•œ๊ธ€ ํŒจ์น˜
import matplotlib as mpl
import matplotlib.pyplot as plt

%config InlineBackend.figure_format = 'retina'

!apt -qq -y install fonts-nanum

import matplotlib.font_manager as fm

fontpath = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf'
font = fm.FontProperties(fname=fontpath, size=9)
plt.rc('font', family='NanumBarunGothic') 
mpl.font_manager._rebuild()

# ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
import folium
import pandas as pd
import plotly.express as ps

# ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
data = pd.read_csv('/content/dataset.csv', encoding='EUC-KR')
data.head()

# ์„œ๋ฏผ.๋ณต์ง€์‹œ์„ค , ์„œ๋ฏผ/๋ณต์ง€์‹œ์„ค ,  ์„œ๋ฏผยท๋ณต์ง€์‹œ์„ค , ์„œ๋ฏผ๋ณต์ง€์‹œ์„ค-> ์„œ๋ฏผ๋ณต์ง€์‹œ์„ค ( ์ „์ฒ˜๋ฆฌ )
data.loc[data.์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„.str.contains('์„œ๋ฏผ'), '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„'] = '์„œ๋ฏผ๋ณต์ง€์‹œ์„ค'

๐Ÿ—‚๏ธ ๋ฐ์ดํ„ฐ๋ถ„์„

# ์„ค์น˜์‹œ๋„๋ณ„ ๋ฌด๋ฃŒ ์™€์ดํŒŒ์ด ์„ค์น˜ํ˜„ํ™ฉ ์ˆ˜
data['์„ค์น˜์‹œ๋„๋ช…'].value_counts()

group_data = data.groupby(by=['์„ค์น˜์‹œ๋„๋ช…', '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„','์„œ๋น„์Šค์ œ๊ณต์‚ฌ๋ช…']).size().reset_index(name='์„ค์น˜์ˆ˜') # ์ง‘๊ณ„๋œ ๊ฒฐ๊ณผ๋ฅผ ์ปฌ๋Ÿผ์— ์ถ”๊ฐ€ํ•˜๊ธฐ์œ„ํ•จ
group_data.์„œ๋น„์Šค์ œ๊ณต์‚ฌ๋ช….unique() # ์ค‘๋ณต์ œ๊ฑฐ๋œ ์„œ๋น„์Šค ์ œ๊ณต๋ช… ๋ณด๊ธฐ

โฌ†๏ธ group_data


๐Ÿ“Š ๊ทธ๋ž˜ํ”„

# ์„ค์น˜์‹œ๋„๋ณ„ WiFi์„ค์น˜ํ˜„ํ™ฉ
px.bar(group_data, x='์„ค์น˜์‹œ๋„๋ช…', y='์„ค์น˜์ˆ˜')


px.histogram(group_data, x='์„ค์น˜์‹œ๋„๋ช…', y='์„ค์น˜์ˆ˜')


px.histogram(group_data, x='์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„', y='์„ค์น˜์ˆ˜')


๐Ÿ‘€ x์ถ•์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ๊ทธ๋ฆฐ๋‹ค



๐Ÿ’ธ ๋Œ€์ „๊ด‘์—ญ์‹œ ๋ฌด๋ฃŒ WiFi ํ˜„ํ™ฉ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐ ์‹œ๊ฐํ™”

dj_df = data.loc[data['์„ค์น˜์‹œ๋„๋ช…'] == '๋Œ€์ „๊ด‘์—ญ์‹œ']
dj_df.info()
# ์–ด๋–ค ๋ชฉ์ ์œผ๋กœ ์™€์ดํŒŒ์ด ์„ค์น˜ํ–ˆ๋Š”์ง€ ํ™•์ธ
fig = px.histogram(dj_df, x='์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…', color='์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„')
fig.show()

fig = px.histogram(data, x='์„ค์น˜์‹œ๋„๋ช…', color='์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„')
fig.show()


# ์ง‘๊ณ„ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ๋งŒ๋“ค ๋•Œ ๊ธฐ์ค€์ด ๋  ์ˆ˜ ์žˆ๋Š” ์ปฌ๋Ÿผ ๋ชจ๋‘ groupby
# ํ•ฉ์ณ์„œ ์‹œ๊ฐํ™” -> ์ฐจํŠธ์—์„œ ์ œ๊ณต
group_dj = dj_df.groupby(by=['์„ค์น˜๋…„์›”','์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…','์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„','์„œ๋น„์Šค์ œ๊ณต์‚ฌ๋ช…','๊ด€๋ฆฌ๊ธฐ๊ด€๋ช…']).size().reset_index(name='์„ค์น˜์ˆ˜')
group_dj


# ์‹œ๊ตฐ๊ตฌ๋ณ„ WiFi ์„ค์น˜ ์ˆ˜์™€ ์„ค์น˜ ์‹œ์„ค ๊ตฌ๋ฌธ 
fig = px.histogram(group_dj, x='์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…', y='์„ค์น˜์ˆ˜', color='์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„')
fig.show()


# ์‹œ๊ตฐ๊ตฌ๋ณ„ WiFi ์„ค์น˜ ํ˜„ํ™ฉ
fig = px.pie(group_dj, names='์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…', values='์„ค์น˜์ˆ˜')
fig.update_traces(textposition='inside',textinfo='percent+label')
fig.show()


๐Ÿš๏ธ ๋‚ด ์œ„์น˜์— ๊ฐ€๊นŒ์šด ๋ฌด๋ฃŒ WiFi์ฐพ๊ธฐ

๐Ÿผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  1. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
  2. wifi dataframe ์ค€๋น„(read_csv)
  3. ์ฃผ์†Œ๋ฅผ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•  ํ•จ์ˆ˜ ์ค€๋น„
  4. ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฃผ์†Œ ์ž…๋ ฅ๋ฐ›๊ธฐ(input)
  5. ์ฃผ์†Œ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ tuple ํ˜•ํƒœ๋กœ ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
  6. my_wifi dataframe์— ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•˜์—ฌ ๋‹ด๊ธฐ
  7. ๋‚ด ์œ„์น˜์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด wifi 10๊ฐœ ๋ฝ‘๊ธฐ(sort_values().head(10))
  8. ์ง€๋„ ์ค€๋น„
  9. ๋งˆ์ปค ํ‘œ์‹œ(WiFi, ์ž…๋ ฅ๋ฐ›์€ ์ฃผ์†Œ)
# 1) ๋‚ด ์œ„์น˜ ์ขŒํ‘œ ์ฐพ๊ธฐ
# 1-1) folium ์ง€๋„๋ฅผ clickํ•˜๋ฉด ์ขŒํ‘œ ์ •๋ณด๋ฅผ ํŒ์—…์œผ๋กœ ๋„์›Œ์ฃผ๊ณ  ์ •๋ณด๋ฅผ ํ™•์ธํ•˜์—ฌ ๋‚ด ์œ„์น˜ ์ขŒํ‘œ๋กœ ์‚ฌ์šฉ

# ์ง€๋„์— ClickForMarker ํ•จ์ˆ˜์™€ LatLngPopup ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์œ„์น˜๋ฅผ Markerํ‘œ์ถœ
import folium
m = folium.Map(location=[36.3511, 127.3866], zoom_start=14)

# ClickForMarker ํ•จ์ˆ˜์ ์šฉ
m.add_child(folium.ClickForMarker(popup='point'))

# ์œ„๋„/๊ฒฝ๋„ ํŒ์—… ํ™œ์„ฑํ™” ์‹œํ‚ค๋„๋ก ์ ์šฉ -> ์ง€๋„๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ขŒํ‘œ๊ฐ’ ์ฐพ๋Š”๋ฐ ๋„์›€
m.add_child(folium.LatLngPopup())

# Latitude: 36.3463 , Longitude: 127.3035
# 1-2) ๋„๋กœ๋ช…์ฃผ์†Œ -> ์ขŒํ‘œ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜
from geopy.geocoders import Nominatim

def geocoding(address):
    geolocoder = Nominatim(user_agent = 'South Korea', timeout=None)
    geo = geolocoder.geocode(address)
    crd = {"lat": str(geo.latitude), "lng": str(geo.longitude)}

    return crd
address = input('๋„๋กœ๋ช… ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ')
crd = geocoding(address)
print(crd['lat'])
print(crd['lng'])

myhome = folium.Map(location=[crd['lat'], crd['lng']], zoom_start=14)
folium.Marker([crd['lat'], crd['lng']], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(myhome)

# 2) ๋‚˜๋ž‘ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด WiFi์ฐพ๊ธฐ
from geopy.distance import geodesic

# 2-1) ๋ฌด๋ฃŒ WiFi ์œ„์น˜ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
# ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒˆ๋กœ๋งŒ๋“ค๊ธฐ
my_wifi = pd.DataFrame(columns=['์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…','์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„','์œ„๋„','๊ฒฝ๋„','๊ฑฐ๋ฆฌ'])
myhome = (36.3511, 127.3866)
for n in data.index:
  wifi_loc = (data.loc[n, '์œ„๋„'], data.loc[n, '๊ฒฝ๋„']) # tuple
  
  # my_wifi dataframe์— ๋‹ด๊ธฐ
  my_wifi.loc[n] = [data.loc[n,'์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…'], data.loc[n, '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„'], data.loc[n, '์œ„๋„'], data.loc[n, '๊ฒฝ๋„'], geodesic(myhome, wifi_loc).kilometers]
my_wifi.head()

์—…๋กœ๋“œ์ค‘.. ๐Ÿ‘€ ์„ค์ •ํ•œ ์œ„๋„๊ฒฝ๋„์™€ ์ง€์—ญ์˜ ๊ฑฐ๋ฆฌ์ฐจ๊ฐ€ my_wifi์— ๋“ค์–ด๊ฐ


# 3) ์ง€๋„ ์‹œ๊ฐํ™”
my_map = folium.Map(location=[36.350389,127.384594], zoom_start=14)

for n in my_wifi.index:
  folium.Marker([my_wifi.loc[n, '์œ„๋„'], my_wifi.loc[n, '๊ฒฝ๋„']], popup='<pre>'+my_wifi.loc[n, '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„']+'</pre>', icon=folium.Icon(icon='wifi', prefix='fa')).add_to(my_map)
folium.Marker([36.3511, 127.3866], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(my_map)
my_map

์—…๋กœ๋“œ์ค‘..


๐Ÿงฉ ํ•ฉ๋ณธ


# 1) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž„ํฌํŠธ
import folium
import pandas as pd
import plotly.express as px
from geopy.geocoders import Nominatim
from geopy.distance import geodesic

m = folium.Map(location=[36.3511, 127.3866], zoom_start=14)
m.add_child(folium.ClickForMarker(popup='point'))
m.add_child(folium.LatLngPopup())

# 2) dataframe ์ค€๋น„
data = pd.read_csv('/content/dataset.csv', encoding='EUC-KR')

# 3) ์ฃผ์†Œ๋ฅผ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•  ํ•จ์ˆ˜ ์ค€๋น„
def geocoding(address):
    geolocoder = Nominatim(user_agent = 'South Korea', timeout=None)
    geo = geolocoder.geocode(address)
    crd = {"lat": str(geo.latitude), "lng": str(geo.longitude)}

    return crd

# 4) ์‚ฌ์šฉ์ž์—๊ฒŒ ์ฃผ์†Œ ์ž…๋ ฅ๋ฐ›๊ธฐ
address = input('๋„๋กœ๋ช… ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” : ')
crd = geocoding(address)

myhome = folium.Map(location=[crd['lat'], crd['lng']], zoom_start=14)
folium.Marker([crd['lat'], crd['lng']], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(myhome)

my_wifi = pd.DataFrame(columns=['์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…','์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„','์œ„๋„','๊ฒฝ๋„','๊ฑฐ๋ฆฌ'])

# 5) ์ฃผ์†Œ๋ฅผ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ tuple ํ˜•ํƒœ๋กœ ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ
myhome = (crd['lat'], crd['lng'])

name = address.split(' ')[0]
data_name = data.loc[data.์„ค์น˜์‹œ๋„๋ช….str.contains(name)]


for n in data_name.index:
  wifi_loc = (data.loc[n, '์œ„๋„'], data.loc[n, '๊ฒฝ๋„']) # tuple
  
  # my_wifi dataframe์— ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•˜์—ฌ ๋‹ด๊ธฐ
  my_wifi.loc[n] = [data.loc[n,'์„ค์น˜์‹œ๊ตฐ๊ตฌ๋ช…'], data.loc[n, '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„'], data.loc[n, '์œ„๋„'], data.loc[n, '๊ฒฝ๋„'], geodesic(myhome, wifi_loc).kilometers]

# 7) ๋‚ด ์œ„์น˜์— ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด WiFi 10๊ฐœ ๋ฝ‘๊ธฐ
my_wifi = my_wifi.sort_values(by=['๊ฑฐ๋ฆฌ']).head(10)

# 8) ์ง€๋„ ์ค€๋น„
my_map = folium.Map(location=[crd['lat'], crd['lng']], zoom_start=14)

# 9) ๋งˆ์ปค ํ‘œ์‹œ
for n in my_wifi.index:
  folium.Marker([my_wifi.loc[n, '์œ„๋„'], my_wifi.loc[n, '๊ฒฝ๋„']], popup='<pre>'+my_wifi.loc[n, '์„ค์น˜์‹œ์„ค๊ตฌ๋ถ„']+'</pre>', icon=folium.Icon(icon='wifi', prefix='fa')).add_to(my_map)
folium.Marker([crd['lat'], crd['lng']], icon=folium.Icon(color='red', icon='glyphicon glyphicon-home')).add_to(my_map)
my_map

์—…๋กœ๋“œ์ค‘..

profile
๋ฐฐ๊ณ ํŒŒ์šฉ.

0๊ฐœ์˜ ๋Œ“๊ธ€