# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.6
#     language: python
#     name: python3
# ---

# <h1>Table of contents</h1>
#
# <div class="alert alert-block alert-info" style="margin-top: 20px">
#     <ul>
#         <li><a href="#random_generated_dataset">k-Means on a randomly generated dataset</a></li>
#             <ol>
#                 <li><a href="#setting_up_K_means">Setting up K-Means</a></li>
#                 <li><a href="#creating_visual_plot">Creating the Visual Plot</a></li>
#             </ol>
#         <li><a href="#customer_segmentation_K_means">Customer Segmentation with K-Means</a></li>
#             <ol>
#                 <li><a href="#pre_processing">Pre-processing</a></li>
#                 <li><a href="#modeling">Modeling</a></li>
#                 <li><a href="#insights">Insights</a></li>
#             </ol>
#     </ul>
# </div>
# <br>
# <hr>

# +
#Import libraries
import random 
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn.cluster import KMeans 
from sklearn.datasets.samples_generator import make_blobs 
# %matplotlib inline

print('imported')
# -

# <h1 id="random_generated_dataset">k-Means on a randomly generated dataset</h1>
# Lets create our own dataset for this lab!

#set-up random seed using numpy's random.seed() function starting at 0
np.random.seed(0)

# Next, <i> random clusters </i> of points by using the <b> make_blobs </b> class. The <b> make_blobs </b> class can take in many inputs. 
# <br> <br>
# <b> <u> Input </u> </b>
#
# <ul>
#     <li> <b>n_samples</b>: The total number of points equally divided among clusters. </li>
#     <ul> <li> Value will be: 5000 </li> </ul>
#     <li> <b>centers</b>: The number of centers to generate, or the fixed center locations. </li>
#     <ul> <li> Value will be: [[4, 4], [-2, -1], [2, -3],[1,1]] </li> </ul>
#     <li> <b>cluster_std</b>: The standard deviation of the clusters. </li>
#     <ul> <li> Value will be: 0.9 </li> </ul>
# </ul>
# <br>
# <b> <u> Output </u> </b>
# <ul>
#     <li> <b>X</b>: Array of shape [n_samples, n_features]. (Feature Matrix)</li>
#     <ul> <li> The generated samples. </li> </ul> 
#     <li> <b>y</b>: Array of shape [n_samples]. (Response Vector)</li>
#     <ul> <li> The integer labels for cluster membership of each sample. </li> </ul>
# </ul>
#

# define mak_blobs class
X, y = make_blobs(n_samples=5000, centers=[[4,4], [-2, -1], [2, -3], [1, 1]], cluster_std=0.9)

# display the plot of the randomly generated data
plt.scatter(X[:, 0], X[:, 1], marker='.')

# <b>The scatter plot have 4 segments</b>

# <h2 id="setting_up_K_means">Setting up K-Means</h2>
# Now that we have our random data, let's set up our K-Means Clustering.

# The KMeans class has many parameters that can be used,I will be using these three:
#
# <ul>
#     <li> <b>init</b>: Initialization method of the centroids. </li>
#     <ul>
#         <li> Value will be: "k-means++" </li>
#         <li> k-means++: Selects initial cluster centers for k-mean clustering in a smart way to speed up convergence.</li>
#     </ul>
#     <li> <b>n_clusters</b>: The number of clusters to form as well as the number of centroids to generate. </li>
#     <ul> <li> Value will be: 4 (since we have 4 centers)</li> </ul>
#     <li> <b>n_init</b>: Number of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of n_init consecutive runs in terms of inertia. </li>
#     <ul> <li> Value will be: 12 </li> </ul>
# </ul>
#
# Initialize KMeans with these parameters, where the output parameter is called <b>k_means</b>.
#

k_means = KMeans(init = "k-means++", n_clusters = 4, n_init = 12)

# fit the KMeans model with features matrix - X
k_means.fit(X)

# fit KMeans .labels_ attribute and save it as k_means_labels
k_means_labels = k_means.labels_
k_means_labels

# build cendroid KMeans .cluster_centers_ , and save it as k_means_cluster_centers
k_means_cluster_centers = k_means.cluster_centers_
k_means_cluster_centers

# <h2 id="creating_visual_plot">Creating the Visual Plot</h2>
# So now that we have the random data generated and the KMeans model initialized, let's plot them and see what it looks like!

# +
# Initialize the plot with the specified dimensions.
fig = plt.figure(figsize=(10, 8))

# Colors uses a color map, which will produce an array of colors based on the number of labels there are. We use set(k_means_labels) to get the
# unique labels.
colors = plt.cm.Spectral(np.linspace(0, 1, len(set(k_means_labels))))

# Create a plot
ax = fig.add_subplot(1, 1, 1)

# For loop that plots the data points and centroids. k will range from 0-3, which will match the possible clusters that each
# data point is in.
for k, col in zip(range(len([[4,4], [-2, -1], [2, -3], [1, 1]])), colors):

    # Create a list of all data points, where the data poitns that are 
    # in the cluster (ex. cluster 0) are labeled as true, else they are
    # labeled as false.
    my_members = (k_means_labels == k)
    
    # Define the centroid, or cluster center.
    cluster_center = k_means_cluster_centers[k]
    
    # Plots the datapoints with color col.
    ax.plot(X[my_members, 0], X[my_members, 1], 'w', markerfacecolor=col, marker='.')
    
    # Plots the centroids with specified color, but with a darker outline
    ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,  markeredgecolor='k', markersize=8)

# Title of the plot
ax.set_title('K-Means')

# Remove x-axis ticks
ax.set_xticks(())

# Remove y-axis ticks
ax.set_yticks(())

# Show the plot
plt.show()

# -

# <h1 id="customer_segmentation_K_means">Customer Segmentation with K-Means</h1>
#

# download data
# !wget -O Cust_Segmentation.csv https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/ML0101ENv3/labs/Cust_Segmentation.csv

# +
# Import library
import pandas as pd
print('imported')

# read the .csv file
cust_df = pd.read_csv("Cust_Segmentation.csv")
cust_df.head()
# -

# <h2 id="pre_processing">Pre-processing</h2

# k-Mean algorithm isn't applicable to categorical variables (Address). Drop the feature
df = cust_df.drop('Address', axis=1)
df.head()

# <h2 id="normalizing">Normalizing</h2>
# Normalization is a statistical method that helps mathematical-based algorithms to interpret features with different magnitudes and distributions equally. We use ***StandardScaler()*** to normalize our dataset.   

# +
from sklearn.preprocessing import StandardScaler

X = df.values[:,1:]
X = np.nan_to_num(X)
Clus_dataSet = StandardScaler().fit_transform(X)
Clus_dataSet
# -

# <h2 id="modeling">Modeling</h2>
# Without k-Mean algorithm it would be the same as guessing. Using k-Means clustering we can do all this process much easier.
# <br/>
# <br/>
# Apply k-Means for a cluster labels.

# +
clusterNum = 4

k_means = KMeans(init = "k-means++", n_clusters = clusterNum, n_init = 12)

k_means.fit(X)
labels = k_means.labels_

print(labels)
# -

# ### Insights

# assign the labels to each row in dataframe
df['Clus_group'] = labels
df.head()

# the centroid mean values in each cluster
df.groupby('Clus_group').mean()

# +
# plot the distribution of customers based on their age & income in 2D
area = np.pi * ( X[:, 1])**2  

plt.scatter(X[:, 0], X[:, 3], s=area, c=labels.astype(np.float), alpha=0.5)

plt.xlabel('Age', fontsize=18)
plt.ylabel('Income', fontsize=16)

plt.show()

# +
from mpl_toolkits.mplot3d import Axes3D 

fig = plt.figure(1, figsize=(12, 10))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

plt.cla()

ax.set_xlabel('Education', fontsize=18)
ax.set_ylabel('Age',fontsize=18)
ax.set_zlabel('Income',fontsize=18)

ax.scatter(X[:, 1], X[:, 0], X[:, 3], c= labels.astype(np.float))
# -



StarcoderdataJupyterScriptsDedupFiltered