[PYTHON] <Course> Machine Learning Chapter 6: Algorithm 2 (k-means)

Machine learning

table of contents Chapter 1: Linear Regression Model [Chapter 2: Nonlinear Regression Model] (https://qiita.com/matsukura04583/items/baa3f2269537036abc57) [Chapter 3: Logistic Regression Model] (https://qiita.com/matsukura04583/items/0fb73183e4a7a6f06aa5) [Chapter 4: Principal Component Analysis] (https://qiita.com/matsukura04583/items/b3b5d2d22189afc9c81c) [Chapter 5: Algorithm 1 (k-nearest neighbor method (kNN))] (https://qiita.com/matsukura04583/items/543719b44159322221ed) [Chapter 6: Algorithm 2 (k-means)] (https://qiita.com/matsukura04583/items/050c98c7bb1c9e91be71) [Chapter 7: Support Vector Machine] (https://qiita.com/matsukura04583/items/6b718642bcbf97ae2ca8)

Chapter 6: Algorithm 2 (k-means)

What is k-means?

k-means algorithm

(Practice) Classify into 3 clusters using k-means

#https://datahexa.com/kmeans-clustering-with-wine-dataset/reference
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import cluster, preprocessing, datasets

from sklearn.cluster import KMeans

wine = datasets.load_wine()
X = wine.data
X.shape

result



y=wine.target
y.shape

result


(178,)
wine.target_names

result


array(['class_0', 'class_1', 'class_2'], dtype='<U7')
model = KMeans(n_clusters=3)
labels = model.fit_predict(X)
df = pd.DataFrame({'labels': labels})
type(df)

result


pandas.core.frame.DataFrame
def species_label(theta):
    if theta == 0:
        return wine.target_names[0]
    if theta == 1:
        return wine.target_names[1]
    if theta == 2:
        return wine.target_names[2]
df['species'] = [species_label(theta) for theta in wine.target]
pd.crosstab(df['labels'], df['species'])
スクリーンショット 2019-12-13 17.46.05.png

Related Sites Chapter 1: Linear Regression Model [Chapter 2: Nonlinear Regression Model] (https://qiita.com/matsukura04583/items/baa3f2269537036abc57) [Chapter 3: Logistic Regression Model] (https://qiita.com/matsukura04583/items/0fb73183e4a7a6f06aa5) [Chapter 4: Principal Component Analysis] (https://qiita.com/matsukura04583/items/b3b5d2d22189afc9c81c) [Chapter 5: Algorithm 1 (k-nearest neighbor method (kNN))] (https://qiita.com/matsukura04583/items/543719b44159322221ed) [Chapter 6: Algorithm 2 (k-means)] (https://qiita.com/matsukura04583/items/050c98c7bb1c9e91be71) [Chapter 7: Support Vector Machine] (https://qiita.com/matsukura04583/items/6b718642bcbf97ae2ca8)

Recommended Posts

<Course> Machine Learning Chapter 6: Algorithm 2 (k-means)
<Course> Machine Learning Chapter 7: Support Vector Machine
<Course> Machine Learning Chapter 3: Logistic Regression Model
<Course> Machine Learning Chapter 1: Linear Regression Model
<Course> Machine learning Chapter 4: Principal component analysis
<Course> Machine Learning Chapter 2: Nonlinear Regression Model
Machine learning course memo
Machine learning algorithm (simple perceptron)
Machine learning algorithm (support vector machine)
Machine learning algorithm (logistic regression)
Python Machine Learning Programming Chapter 2 Classification Problems-Machine Learning Algorithm Training Summary
[Language processing 100 knocks 2020] Chapter 6: Machine learning
Machine learning algorithm (support vector machine application)
Machine learning algorithm (multiple regression analysis)
Machine learning algorithm (simple regression analysis)
100 Language Processing Knock 2020 Chapter 6: Machine Learning
Machine learning
Machine learning algorithm (gradient descent method)
Machine learning beginners take Coursera's Deep learning course
Machine learning algorithm (generalization of linear regression)
TensorFlow Machine Learning Cookbook Chapter 2 Personally Clogged
Machine learning algorithm (implementation of multi-class classification)
Machine learning algorithm classification and implementation summary
Machine learning algorithm (linear regression summary & regularization)
TensorFlow Machine Learning Cookbook Chapter 3 Personally Clogged
Dictionary learning algorithm
[Memo] Machine learning
Machine learning classification
Machine Learning sample
Gaussian mixed model EM algorithm [statistical machine learning]
TensorFlow Machine Learning Cookbook Chapter 6 (or rather, tic-tac-toe)
Machine learning tutorial summary
About machine learning overfitting
Machine learning ⑤ AdaBoost Summary
Machine Learning: Supervised --AdaBoost
Machine learning logistic regression
Python learning memo for machine learning by Chainer from Chapter 2
Machine learning support vector machine
Studying Machine Learning ~ matplotlib ~
Machine learning linear regression
Machine learning library dlib
Machine learning (TensorFlow) + Lotto 6
Somehow learn machine learning
Machine learning library Shogun
Machine learning rabbit challenge
Introduction to machine learning
Machine Learning: k-Nearest Neighbors
What is machine learning?
Talk about improving machine learning algorithm bottlenecks with Cython
Python learning memo for machine learning by Chainer Chapter 7 Regression analysis
Machine learning model considering maintainability
Machine learning learned with Pokemon
Data set for machine learning
Japanese preprocessing for machine learning
Machine learning in Delemas (practice)
<Course> Deep Learning: Day2 CNN
An introduction to machine learning
Machine learning / classification related techniques
Machine Learning: Supervised --Linear Regression
Basics of Machine Learning (Notes)
Machine learning beginners tried RBM