<jupyter_start><jupyter_text>DAT210x - Programming with Python for DS  Module5- Lab1 Start by importing whatever you need to import in order to make this lab work:<jupyter_code>import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
%matplotlib inline<jupyter_output><empty_output><jupyter_text>How to Get The Dataset 1. Open up the City of Chicago's [Open Data | Crimes](https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2) page.1. In the `Primary Type` column, click on the `Menu` button next to the info button, and select `Filter This Column`. It might take a second for the filter option to show up, since it has to load the entire list first.1. Scroll down to `GAMBLING`1. Click the light blue `Export` button next to the `Filter` button, and select `Download As CSV` Now that you have th dataset stored as a CSV, load it up being careful to double check headers, as per usual:<jupyter_code>gamblingdf = pd.read_csv('Datasets\Crimes_-_2001_to_present.csv')<jupyter_output><empty_output><jupyter_text>Get rid of any _rows_ that have nans in them:<jupyter_code>gamblingdf = gamblingdf.dropna(axis=0)<jupyter_output><empty_output><jupyter_text>Display the `dtypes` of your dset:<jupyter_code>gamblingdf.dtypes<jupyter_output><empty_output><jupyter_text>Coerce the `Date` feature (which is currently a string object) into real date, and confirm by displaying the `dtypes` again. This might be a slow executing process...<jupyter_code>gamblingdf.Date = pd.to_datetime(gamblingdf.Date, errors='coerce')
def doKMeans(df):
    # Let's plot your data with a '.' marker, a 0.3 alpha at the Longitude,
    # and Latitude locations in your dataset. Longitude = x, Latitude = y
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(df.Longitude, df.Latitude, marker='.', alpha=0.3)
    
    # TODO: Filter `df` using indexing so it only contains Longitude and Latitude,
    # since the remaining columns aren't really applicable for this lab:
    #
    location = df.iloc[:,-3:-1]


    # TODO: Use K-Means to try and find seven cluster centers in this df.
    # Be sure to name your kmeans model `model` so that the printing works.
    #
    model = KMeans(n_clusters=7)
    model.fit(location)
    label = model.predict(location)


    # Now we can print and plot the centroids:
    centroids = model.cluster_centers_
    print(centroids)
    ax.scatter(centroids[:,1], centroids[:,0], marker='x', c='red', alpha=0.5, linewidths=3, s=169)
    plt.show()
# Print & Plot your data
doKMeans(gamblingdf)<jupyter_output>[[ 41.89329806 -87.75695802]
 [ 41.6966916  -87.63117574]
 [ 41.77277207 -87.66471483]
 [ 41.9813113  -87.68540588]
 [ 41.75261037 -87.58254038]
 [ 41.87814182 -87.70960134]
 [ 41.80342592 -87.61948807]]<jupyter_text>Filter out the data so that it only contains samples that have a `Date > '2011-01-01'`, using indexing. Then, in a new figure, plot the crime incidents, as well as a new K-Means run's centroids.<jupyter_code>filtereddf = gamblingdf[gamblingdf.Date>'2011-01-01']
# Print & Plot your data
doKMeans(filtereddf)<jupyter_output>[[ 41.98387683 -87.68692804]
 [ 41.79168888 -87.61919322]
 [ 41.8898888  -87.7547907 ]
 [ 41.77552597 -87.66498142]
 [ 41.70542973 -87.63436969]
 [ 41.87540342 -87.71061436]
 [ 41.75092807 -87.57996522]]
{'fraction_non_alphanumeric': 0.10682226211849193, 'fraction_numerical': 0.09874326750448834, 'mean_word_length': 4.444625407166124, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '91682', 'license': 'MIT', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1165, 'n_words': 359, 'path': 'Module5/.ipynb_checkpoints/Module5 - Lab1-checkpoint.ipynb', 'repo_name': '3point14thon/DAT210x-master', 'size': '3342'}