end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

scikit-learn(sklearn)for python によるSVM分類予測 - svm.SVC()

https://ailog.site/2019/08/04/sklearn2/

SVM(サポートベクターマシン)に関する上記urlの写経です。

#!python
# -*- coding: utf-8 -*-
# refer to https://ailog.site/2019/08/04/sklearn2/

from sklearn import model_selection, svm, metrics
import matplotlib.pyplot as plt
import pandas as pd
import random

def main():
    lst_label, lst_height_weight = make_data()

    # 学習データとテストデータを分割
    data_train, data_test, label_train, label_test = \
        model_selection.train_test_split(lst_height_weight,
                                         lst_label,
                                         test_size=0.2 )
    clf = svm.SVC()                     # SVM
    clf.fit(data_train, label_train)    # 学習
    predict = clf.predict(data_test)    # 予測
    # 結果確認
    print('正解率=', metrics.accuracy_score(label_test, predict))
    print('レポート=\n', metrics.classification_report(label_test, predict))

def calc_bmi(height, weight):
    bmi = weight / (height / 100) ** 2
    if bmi < 18.5:
        return '痩せ'
    if bmi < 25:
        return '普通'
    return '肥満'

def make_data():
    lst_label         = []
    lst_height_weight = []
    for i in range(10000):
        height = random.randint(100, 200)   # 100cm~200cmの身長
        weight = random.randint(30, 100)    # 30kg~60kgの体重
        label = calc_bmi(height, weight)

        lst_height_weight.append([height, weight])
        lst_label.append(label)
    return lst_label, lst_height_weight
    
if __name__ == '__main__':
    main()

↑こう書くと、↓こう表示されます

正解率= 0.99
レポート=
               precision    recall  f1-score   support

          普通       0.96      0.99      0.97       377
          痩せ       1.00      0.97      0.99       443
          肥満       1.00      1.00      1.00      1180

    accuracy                           0.99      2000
   macro avg       0.98      0.99      0.99      2000
weighted avg       0.99      0.99      0.99      2000