๐ผ ์ค๋น
# ํ๊ธ๊นจ์ง๋ฐฉ์ง # ํ๊ธ ํจ์น 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์ถ์ ๋ฐ๊ฟ๊ฐ๋ฉฐ ๊ทธ๋ฆฐ๋ค
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 dataframe ์ค๋น(read_csv)
- ์ฃผ์๋ฅผ ์ขํ๋ก ๋ณํํ ํจ์ ์ค๋น
- ์ฌ์ฉ์์๊ฒ ์ฃผ์ ์ ๋ ฅ๋ฐ๊ธฐ(input)
- ์ฃผ์ ์ขํ๋ก ๋ณํํ์ฌ tuple ํํ๋ก ๋ณ์์ ๋ด๊ธฐ
- my_wifi dataframe์ ๊ฑฐ๋ฆฌ ๊ณ์ฐํ์ฌ ๋ด๊ธฐ
- ๋ด ์์น์ ๊ฐ์ฅ ๊ฐ๊น์ด wifi 10๊ฐ ๋ฝ๊ธฐ(sort_values().head(10))
- ์ง๋ ์ค๋น
- ๋ง์ปค ํ์(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