<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Creating datasets for 2D
We begin by reading the csv file, into a data frame. This makes it easier to create.
Step1:  Then we want to filter the data set.
We do this by only taking the rows with the category PROSTITUTION as well as removing some rows with invalid Y coordinate.
Step2:  To reduce the amount of data we need to load on the page, we only extract the columns that we need.
In this case it is the district, longtitude and latitude.
If this file were written to the disk at this point, the size would be around 700KB (e.i. very small).
Step3:  Then we define a function that we use to calculate the clusters, as well as centoids.
Step4:  Now we calcualte all our K nearest neighbor, for 2..6.
Step5:  Here is a preview of our data, now enriched with K values.
Step6:  Write our result
Lastly we write our result to the disk, so that we can use it on our page.
Step7:  Below is the centoids printed
<ASSISTANT_TASK:>
Python Code:
data_path = '../../SFPD_Incidents_-_from_1_January_2003.csv'
data = pd.read_csv(data_path)
Explanation: Creating datasets for 2D
We begin by reading the csv file, into a data frame. This makes it easier to create.
End of explanation
mask = (data.Category == 'PROSTITUTION') & (data.Y != 90)
filterByCat = data[mask]
Explanation: Then we want to filter the data set.
We do this by only taking the rows with the category PROSTITUTION as well as removing some rows with invalid Y coordinate.
End of explanation
reducted = filterByCat[['PdDistrict','X','Y']]
Explanation: To reduce the amount of data we need to load on the page, we only extract the columns that we need.
In this case it is the district, longtitude and latitude.
If this file were written to the disk at this point, the size would be around 700KB (e.i. very small).
End of explanation
X = data.loc[mask][['X','Y']]
centers = {}
def knn(k):
    md = cluster.KMeans(n_clusters=k).fit(X)
    return md.predict(X),md.cluster_centers_
Explanation: Then we define a function that we use to calculate the clusters, as well as centoids.
End of explanation
for i in range(2,7):
    reducted['K'+str(i)], centers[i] = knn(i)
centers
Explanation: Now we calcualte all our K nearest neighbor, for 2..6.
End of explanation
reducted.head()
Explanation: Here is a preview of our data, now enriched with K values.
End of explanation
reducted.to_csv('week_8_vis_1.csv', sep=',')
Explanation: Write our result
Lastly we write our result to the disk, so that we can use it on our page.
End of explanation
centers
Explanation: Below is the centoids printed
End of explanation
<END_TASK>