<jupyter_start><jupyter_text>K-Means on CDR (call detail records) of 10 users in Dallas, Texas* use call location to identify user home address<jupyter_code>import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import numpy as np
# load the CDR data
flc = '/Users/pinqingkan/Desktop/DataScience/PythonDataScience/DAT210x-master/Module5/Datasets/'
fname = flc + 'CDR.csv'
X = pd.read_csv(fname)
X.head()
# convert string to datetime & time delta
Z = pd.to_datetime(X.CallDate)
Z2 = pd.to_timedelta(X.CallTime)
X2 = X.drop(axis = 1, labels = ['CallDate', 'CallTime'])
X2 = pd.concat([X2, Z, Z2], axis = 1)
# obtain a list of distinct incoming phone calls
incoming = X2.In.unique().tolist()
incoming
# look at each user one by one
for iuser in range(len(incoming)):
    Y = X2[X.In == incoming[iuser]]
    
    # focus on those on weekends
    Y2 = Y[(Y.DOW == 'Sat') | (Y.DOW == 'Sun')]
    # & those at late hours (< 6am / > 10pm)
    Y2 = Y2[(Y2.CallTime < '06:00:00') | (Y2.CallTime > '22:00:00')]
    # show the call locations
    plt.scatter(Y.TowerLon, Y.TowerLat, c='gray', alpha=0.1)
    plt.scatter(Y2.TowerLon, Y2.TowerLat, c='g', marker='o')
    plt.title('Calls (Green: weekend, <6am or >10p)')
    plt.xlabel('Longitude')
    plt.ylabel('Latitude')
    # plt.show()
    
    # K cluster on the call location
    if iuser in [0, 4, 6, 8]:
        K = 2
    elif iuser in [3, 9]:
        K = 3
    else:
        K = 1
    Z = Y2.loc[:, ['TowerLon', 'TowerLat']]
    kmeans = KMeans(n_clusters = K)
    kmeans.fit(Z)
    KMeans(n_clusters = K, max_iter = 300, n_init = 10, init = 'k-means++', copy_x = True,
           n_jobs = 1, precompute_distances = 'auto', random_state = None, tol = 1e-4,
           verbose = 0)
    centroids = kmeans.cluster_centers_
    print(centroids)
    
    # visualize the clustering
    plt.scatter(Z.TowerLon, Z.TowerLat, c = 'b', marker = '.')
    plt.scatter(centroids[:,0], centroids[:,1], marker='x', c='red', linewidths=3, s=169)
    #xx = np.mean(Z.TowerLon)
    #yy = np.mean(Z.TowerLat)
    #flocation = str(round())
    #plt.text(xx-.1, yy+.05, r'')
    #plt.title('Weekend calls (<6am or >10p)')
    #plt.xlabel('Longitude')
    #plt.ylabel('Latitude')
    plt.show()<jupyter_output>[[-96.70944573  32.73164942]
 [-96.694722    32.750556  ]]
{'fraction_non_alphanumeric': 0.16079105760963028, 'fraction_numerical': 0.0468615649183147, 'mean_word_length': 3.185251798561151, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '525592', 'license': 'MIT', 'n_tokens_mistral': 998, 'n_tokens_neox': 927, 'n_words': 226, 'path': 'Lab5_1_Modeling_KMeans/assignment2.ipynb', 'repo_name': 'rongqingpin/Python_Data_Science_edX_DAT210x', 'size': '2326'}