<jupyter_start><jupyter_text>K-means / 접근성 분석<jupyter_code>import numpy as np
import math
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from scipy.spatial.distance import cdist, pdist
import warnings
warnings.filterwarnings('ignore')
all_center = pd.read_csv("data-set/KNN_data.csv", sep = ",", encoding="euc-kr")
x_test = all_center[all_center['Type'] == "국공립"] 
X = x_test.iloc[:, 14:16]
K = 150

def k_search():
    global K
    KM = [KMeans(n_clusters = k).fit(X) for k in K]
    ss = [silhouette_score(X, k.labels_, metric = 'euclidean') for k in KM]
    centroids = [k.cluster_centers_ for k in KM]
    D_k = [cdist(X, centrds, 'euclidean') for centrds in centroids]
    cIdx = [np.argmin(D, axis = 1) for D in D_k]
    dist = [np.min(D, axis = 1) for D in D_k]
    avgWithinSS = [sum(d) / X.shape[0] for d in dist]
    wcss = [sum(d**2) for d in dist]
    tss = sum(pdist(X)**2 / X.shape[0])
    bss = tss - wcss 
    '''
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(K, avgWithinSS, 'b*-')
    plt.grid(True)
    plt.xlabel("# of clusters")
    plt.ylabel("Avg of ss")
    
    fig2 = plt.figure()
    ax = fig2.add_subplot(111)
    ax.plt(K, bss/tss*100, 'b*-')
    plt.grid(True)
    plt.xlabel("# of cluster")
    plt.ylabel("var")
    plt.show()
    
    '''
    return ss
    
#ss = k_search() # k -- > 구별 25 / 100로 진행
k_means = KMeans(n_clusters = K, max_iter = 3000, random_state = 77)
k_means.fit(X)
k_cluster = k_means.predict(X)
x_test['k_cluster'] = k_cluster

ss = round(silhouette_score(X, k_means.labels_, metric = 'euclidean'),2)
print(ss)
from matplotlib import font_manager, rc
font_name = font_manager.FontProperties(fname = "c:/Windows/Fonts/malgun.ttf").get_name()
rc('font', family = font_name)
fig = plt.figure()
for i in range(K):
    scat = plt.scatter(x_test[x_test['k_cluster'] == i].iloc[:, 14], 
                       x_test[x_test['k_cluster'] == i].iloc[:, 15], s = 10)
fig.show()
center = k_means.cluster_centers_
groupby = x_test.sort_values(['k_cluster'])

def distance(a,b):
    lon1, lat1 = a[0], a[1]
    lon2, lat2 = float("%.6f" %b[0]), float("%.6f" %b[1])
    R = 6378.137
    dlat = (lat2 - lat1) * (np.pi / 180)
    dlon = (lon2 - lon1) * (np.pi / 180)
    a = np.sin((dlat/2))**2 + np.cos(lat1 * np.pi / 180) * np.cos(lat2 * np.pi / 180) * (np.sin(dlon/2)) ** 2
    c = 2 * np.math.atan2(np.sqrt(a), np.sqrt(1-a))
    d = R * c
    return d
               
def center_access():
    global k_means, center, K, groupby
#     pop = groupby[['201809']]
    groupby['center_access'] = 0.01
    for j in range(len(groupby)):
        tmpList = []
        for i in range(len(center)):
           gb = groupby[groupby['k_cluster'] == i]
           e = np.int(np.mean(gb['201809']))
           dist = distance(groupby.iloc[j, 14:16].values, center[i])
           tmpList.append(e * (dist * 1000) ** -1)
        groupby.iloc[j, -1] = np.sum(tmpList)
#     groupby['mean'] = groupby['center_access'] / K

def people_access():
    global k_means, center, K, groupby
    center = pd.DataFrame(center)
    center['people_access'] = 0.01
    for j in range(len(center)) :
        tmpList = []
        for i in range(len(groupby)):
            center_acc = groupby['center_access'].iloc[i]
            limit = groupby['Max'].iloc[i]
            dist = distance(groupby.iloc[i, 14:16].values, center.iloc[j, :-1].values)
            tmpList.append((limit * (dist * 1000) ** -1) / (center_acc))
        center.iloc[j, -1] = np.sum(tmpList)
               
center_access()
people_access()
groupby['people_access'] = 0
for i in range(len(groupby)):
    groupby.iloc[i, -1] = center['people_access'][groupby['k_cluster'].iloc[i]]
 
# groupby.to_csv("data-set/KK_k150.csv", encoding="euc-kr")

kk_150 = pd.read_csv("data-set/KK_k150.csv", sep = ",", encoding="euc-kr")
kk_150.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17313054499366287, 'fraction_numerical': 0.034727503168567805, 'mean_word_length': 3.030643513789581, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '288565', 'license': 'MIT', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1582, 'n_words': 335, 'path': 'KMeans_access/kmeans.ipynb', 'repo_name': 'dongkoull/Bigdata_contest', 'size': '3945'}