<jupyter_start><jupyter_text>서울시의 인구와 CCTV 비율의 상관관계 파악하기  라이브러리 불러오기<jupyter_code>import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import font_manager, rc
%matplotlib inline

plt.rcParams['axes.unicode_minus'] = False

rc('font', family='AppleGothic')<jupyter_output><empty_output><jupyter_text>데이터 인출 및 정제하기<jupyter_code>cctv_data = pd.read_csv('./seoul_cctv.csv', encoding='utf-8')
popularity = pd.read_excel('./seoul_popularity.xls', header=2, usecols='B, D, G, J, N', encoding='utf-8')
cctv_data.head()
popularity.head()
cctv_data.rename(columns={ cctv_data.columns[0]: "구별" }, inplace=True)
cctv_data['최근증가율'] = cctv_data.apply(lambda row: \
                    (row['2014년'] + row['2015년'] + row['2016년']) / row['2013년도 이전'] * 100, axis=1)

cctv_data.drop(["2013년도 이전", "2014년", "2015년", "2016년"], axis=1, inplace=True)

cctv_data.head()
popularity.rename(columns= \
        { popularity.columns[0]: "구별", popularity.columns[1]: "인구수", popularity.columns[2]: "한국인", popularity.columns[3]: "외국인", popularity.columns[4]: "고령자" }, \
                  inplace=True)

popularity['외국인 비율'] = popularity.apply(lambda row: row['외국인'] / row['인구수'] * 100, axis=1)
popularity['고령자 비율'] = popularity.apply(lambda row: row['고령자'] / row['인구수'] * 100, axis=1)

popularity.drop([0], inplace=True)

popularity.dropna(inplace=True)

popularity.head()
result = pd.merge(cctv_data, popularity, on="구별")

result.set_index("구별", inplace=True)
result['CCTV 비율'] = result.apply(lambda row: row['소계'] / row['인구수'] * 100, axis=1)

result.head()<jupyter_output><empty_output><jupyter_text>상관관계 파악하기<jupyter_code>correlation = result.corr(method='pearson')
correlation
sm_model = sm.OLS.from_formula("소계 ~ 인구수", result).fit()
sm_model.summary()<jupyter_output><empty_output><jupyter_text>회귀 분석하기<jupyter_code>coef = np.polyfit(result['인구수'], result['소계'], 1)
np_model = np.poly1d(coef)

bias = np.abs(result['소계'] - np_model(result['인구수']))<jupyter_output><empty_output><jupyter_text>시각화하기<jupyter_code>result["CCTV 비율"].sort_values().plot(kind='barh', grid=True, figsize=(10, 10))

plt.show()
plt.figure(figsize=(14, 10))

plt.scatter(result['인구수'], result['소계'], c=bias, s=60)

x = np.linspace(100000, 700000, 100)

plt.plot(x, np_model(x), lw=3, color='r')

for row in result.itertuples():
    plt.text(row.인구수 * 1.02, row.소계 * 0.98, row.Index, fontsize=15)

plt.xlabel('인구 수')
plt.ylabel('인구 당 CCTV 비율')

plt.colorbar()
plt.grid()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1873015873015873, 'fraction_numerical': 0.037698412698412696, 'mean_word_length': 6.244252873563219, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '395136', 'license': 'Apache-2.0', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1278, 'n_words': 174, 'path': 'correlation_between_population_and_cctv.ipynb', 'repo_name': 'Unperknown/DataScience', 'size': '2520'}