{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "s1", "content", "l1" ] }, "source": [ "# Unsupervised Learning\n", "\n", "## Unsupervised Learning - K-Means\n", "\n", "### Introduction to Unsupervised Learning\n", "\n", "We learnt about supervised learning and its methods. To recap, supervised learning involves training a machine learning model with data mapped to a target. The model learns the mapping and given a test data outside of the training data, it is able to make predictions. Unsupervised learning necessitates no training data mapped to a target. Infact, there is no training involved. The algorithms are able to generate various insights into the data and hence called unsupervised learning. One popular example of unsupervised learning is clustering.\n", "\n", "## Introduction to Clustering\n", "\n", "Finding groups based on a criterion in the dataset is called clustering. The criterion could be a spatial distribution involving distance between nearby points or could involve another metric such as density based on presence of neighbors. A basic clustering algorithm is called k-means clustering which is a spatial clustering algorithm. \n", "\n", "### k-means clustering\n", "\n", "k-means clustering takes in k number of clusters that could potentially exist in a data set and outputs associations of each member of the dataset to a cluster in the range [1, k]. k-means initially randomly associates each data point to a random cluster in the range [1, k] and determines the centroid of each cluster. Later, in an iterative manner, the members associations as well as cluster centroids of k clusters are updated till a convergence criterion is achieved. \n", "\n", "#### The Iris Dataset\n", "\n", "The Iris dataset is a famous dataset used by Statisticians, Machine Learning Engineers and Data Scientists in his 1936 paper \"The use of multiple measurements in taxonomic problems\". The data consists of Iris flowers that belong to three different species. \n", "\n", "The data set consists of various samples with four features per data point. These are:\n", "\n", " - length of the sepals (cm)\n", " - lenght of the petals (cm)\n", " - width of the sepals (cm) and\n", " - width of the petals (cm).\n", "We shall learn to use these features and perform clustering using k-means to see various cluster realizations.\n", "\n", "Load all relevant libraries:\n", "```python\n", "import pandas as pd\n", "import plotly.tools as tls\n", "from sklearn.cluster import KMeans\n", "from sklearn.cross_validation import train_test_split\n", "iris = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris.csv')\n", "iris.head()\n", "```\n", "\n", "\n", "\n", "









\n", "Let us perform clustering by selecting number of clusters as 5 using the KMeans function. The max iteration can be specified which allows the K-Means algorithm to stop at a certain point if it doesnt reach a tolerance level (tol) below 0.0001 in that many steps.\n", "```python\n", "features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']\n", "iris_train, iris_test = train_test_split(iris, train_size=0.9)\n", "k_means = KMeans(n_clusters=5, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1)\n", "```\n", "We can now fit the instantiated k_means to the features:\n", "\n", "```python\n", "k_means.fit(iris_train[features])\n", "```\n", "The predict function provides predictions on which cluster the data in the test set will be associated to: \n", "\n", "```python\n", "iris_y = k_means.predict(iris_test[features])\n", "```\n", "\n", "\n", "## Exercise:\n", "\n", "K-Means associates each data point to a cluster. We can see these associations post clustering in labels_ variable.\n", "\n", "- What is the type of labels_ data structure?\n", "- Map the associations of each data point in X to the cluster it belongs to by appending it to the dataframe. Create a new column called 'cluster' that contains the cluster number or id that the row associates itself with.\n", "- Print the first 5 rows of the training dataset with the cluster column." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "s1", "ce", "l1" ] }, "outputs": [], "source": [ "from sklearn.cluster import KMeans\n", "from sklearn.cross_validation import train_test_split\n", "\n", "import pandas as pd\n", "\n", "iris = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris.csv')\n", "\n", "features = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth']\n", "\n", "iris_train, iris_test = train_test_split(iris, train_size=0.9)\n", "\n", "k_means = KMeans(n_clusters=5, init='k-means++', n_init=10, max_iter=300, tol=0.0001, precompute_distances='auto', verbose=0, random_state=None, copy_x=True, n_jobs=1)\n", "k_means.fit(iris_train[features])\n", "iris_y = k_means.predict(iris_test[features])\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "s1", "l1", "hint" ] }, "source": [ "

Use dataframe .assign function.

" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "s1", "l1", "ans" ] }, "outputs": [], "source": [ "iris_train = iris_train.assign(cluster = k_means.labels_)\r\n", "iris_train.head(5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "s1", "hid", "l1" ] }, "outputs": [], "source": [ "ref_tmp_var = False\n", "\n", "\n", "try:\n", " ref_assert_var = False\n", " import numpy as np\n", " \n", " if np.all(iris_train['cluster'] == k_means.labels_):\r\n", " ref_assert_var = True\r\n", " out = iris_train.head(5)\r\n", " else:\r\n", " ref_assert_var = False\n", " \n", "except Exception:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "else:\n", " if ref_assert_var:\n", " ref_tmp_var = True\n", " else:\n", " print('Please follow the instructions given and use the same variables provided in the instructions.')\n", "\n", "\n", "assert ref_tmp_var" ] } ], "metadata": { "executed_sections": [], "rf_version": 1 }, "nbformat": 4, "nbformat_minor": 2 }