Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im doing some analytics using python 3.5 and anaconda. in K means Clustering pyt

ID: 3896080 • Letter: I

Question

Im doing some analytics using python 3.5 and anaconda.

in K means Clustering python will not recognise ggplot, even after importing.

any alternatives to ggplot?

this is my code

import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from ggplot import aes
from ggplot import *

data = pd.read_csv(r'C:UsersuserAnaconda3envsssignland-area-population-density-london.csv')
data.columns = ["Codes","Ward names" ,"Borough", "Hectares", "Square Kilometres", "Population 2015", "Population per hectare 2015", "Population per square kilometre 2015",
"Census population(2011)", "Population per hectare 2011"]

#creating a matrix table

matrix = data.pivot_table(index=['Ward names'], columns=['Borough','Population per hectare 2015', 'Population per hectare 2011'])
print(matrix)

matrix = matrix.fillna(0).reset_index()
x_cols = matrix.columns[1:]


cluster = KMeans(n_clusters=5)

matrix['cluster']= cluster.fit_predict(matrix[matrix.columns[3:]])

pca = PCA(n_components=2)

matrix['x'] = pca.fit_transform(matrix[x_cols])[:,0]
matrix['y'] = pca.fit_transform(matrix[x_cols])[:,1]

matrix = matrix.reset_index()


customer_clusters = matrix [['Ward names', 'cluster', 'x', 'y']]
#works up to here

g = ggplot(data, aes(x='x', y='y',color='cluster')) +
geom_point(size=75) +
ggtitle("Clustering based on Area")

Explanation / Answer

The alternative to ggplot is matplotlib.

matplotlib is widely use to from plot, graph etc..

Scatter plot is most commonaly use to form a cluster.

import matplotlib.pyplot.scatter

Syntax:

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)

Parameters:

x, y : shape of array(n, )

s : scalar

c : color

marker : MarkerStyle

cmap : Colormap

norm : Normalize

vmin, vmax : scalar

alpha : scalar, values from 0 to 1

linewidths: width of line

verts : sequence of (x, y)

edgecolors : color of edge

Example: