{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Machine Learning with scikit-learn" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import sklearn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. the iris dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The iris is a classic data set in the field of data recognition." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn import datasets\n", "iris = datasets.load_iris()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sklearn.utils._bunch.Bunch" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(iris)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".. _iris_dataset:\n", "\n", "Iris plants dataset\n", "--------------------\n", "\n", "**Data Set Characteristics:**\n", "\n", " :Number of Instances: 150 (50 in each of three classes)\n", " :Number of Attributes: 4 numeric, predictive attributes and the class\n", " :Attribute Information:\n", " - sepal length in cm\n", " - sepal width in cm\n", " - petal length in cm\n", " - petal width in cm\n", " - class:\n", " - Iris-Setosa\n", " - Iris-Versicolour\n", " - Iris-Virginica\n", " \n", " :Summary Statistics:\n", "\n", " ============== ==== ==== ======= ===== ====================\n", " Min Max Mean SD Class Correlation\n", " ============== ==== ==== ======= ===== ====================\n", " sepal length: 4.3 7.9 5.84 0.83 0.7826\n", " sepal width: 2.0 4.4 3.05 0.43 -0.4194\n", " petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\n", " petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n", " ============== ==== ==== ======= ===== ====================\n", "\n", " :Missing Attribute Values: None\n", " :Class Distribution: 33.3% for each of 3 classes.\n", " :Creator: R.A. Fisher\n", " :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n", " :Date: July, 1988\n", "\n", "The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\n", "from Fisher's paper. Note that it's the same as in R, but not as in the UCI\n", "Machine Learning Repository, which has two wrong data points.\n", "\n", "This is perhaps the best known database to be found in the\n", "pattern recognition literature. Fisher's paper is a classic in the field and\n", "is referenced frequently to this day. (See Duda & Hart, for example.) The\n", "data set contains 3 classes of 50 instances each, where each class refers to a\n", "type of iris plant. One class is linearly separable from the other 2; the\n", "latter are NOT linearly separable from each other.\n", "\n", ".. topic:: References\n", "\n", " - Fisher, R.A. \"The use of multiple measurements in taxonomic problems\"\n", " Annual Eugenics, 7, Part II, 179-188 (1936); also in \"Contributions to\n", " Mathematical Statistics\" (John Wiley, NY, 1950).\n", " - Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n", " (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n", " - Dasarathy, B.V. (1980) \"Nosing Around the Neighborhood: A New System\n", " Structure and Classification Rule for Recognition in Partially Exposed\n", " Environments\". IEEE Transactions on Pattern Analysis and Machine\n", " Intelligence, Vol. PAMI-2, No. 1, 67-71.\n", " - Gates, G.W. (1972) \"The Reduced Nearest Neighbor Rule\". IEEE Transactions\n", " on Information Theory, May 1972, 431-433.\n", " - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al\"s AUTOCLASS II\n", " conceptual clustering system finds 3 classes in the data.\n", " - Many, many more ...\n" ] } ], "source": [ "print(iris.DESCR)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us take a look at the data." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = iris.data\n", "type(data)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(150, 4)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data is a two dimensional array, of 150 rows and 4 columns." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ydata = iris.target\n", "type(ydata)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(150,)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ydata.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us see how many different target values we have." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.unique(ydata)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are three different values for the target." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. finding the clusters in the data set" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from sklearn import cluster" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In running the k-means algorithm, we have to given the number of clusters." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "k_means = cluster.KMeans(n_clusters=3, n_init='auto')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\jan\\miniconda3\\lib\\site-packages\\sklearn\\cluster\\_kmeans.py:1436: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "
KMeans(n_clusters=3, n_init='auto')In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
KMeans(n_clusters=3, n_init='auto')
KNeighborsClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
KNeighborsClassifier()