scikit-learn の使い方 (その1)

次の記事と同じことを scikit-learn 0.23.1 で行いました。
[機械学習のライブラリ！scikit-learnとは【初心者向け】](https://techacademy.jp/magazine/17375)

## Arch Linux で必要なライブラリーのインストール

```bash
sudo pacman -S python-scikit-learn
sudo pacman -S python-matplotlib
```

## 確認したバージョン

```text
$ python
Python 3.11.3 (main, Jun  5 2023, 09:32:32) [GCC 13.1.1 20230429] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>> print(sklearn.__version__)
1.2.2
```

## データの確認

```py:show_data.py
#! /usr/bin/python
#
#	show_data.py
#
#						Sep/03/2020
#
from sklearn import datasets
import matplotlib.pyplot as plt

digits = datasets.load_digits()

plt.matshow(digits.images[0], cmap="Greys")
plt.show()
```

実行結果
![scikit_sep0301.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/5cd22f44-c00d-88a0-1934-6eedca9c01aa.png)

## SVM

```py:svm01.py
#! /usr/bin/python
#
#	svm01.py
#
#						Sep/03/2020
#
from sklearn import datasets
from sklearn import svm
import sklearn.metrics as metrics

digits = datasets.load_digits()


X = digits.data
y = digits.target

X_train, y_train = X[0::2], y[0::2]
X_test, y_test = X[1::2], y[1::2]

clf = svm.SVC(gamma=0.001)

clf.fit(X_train, y_train)

accuracy = clf.score(X_test, y_test)
print(f"正解率{accuracy}")

predicted = clf.predict(X_test)

print("classification report")
print(metrics.classification_report(y_test, predicted))
```

実行結果

```text
$ ./svm01.py 
正解率0.9866369710467706
classification report
              precision    recall  f1-score   support

           0       1.00      0.99      0.99        88
           1       0.98      1.00      0.99        89
           2       1.00      1.00      1.00        91
           3       1.00      0.98      0.99        93
           4       0.99      1.00      0.99        88
           5       0.98      0.97      0.97        91
           6       0.99      1.00      0.99        90
           7       0.99      1.00      0.99        91
           8       0.97      0.97      0.97        86
           9       0.98      0.97      0.97        91

    accuracy                           0.99       898
   macro avg       0.99      0.99      0.99       898
weighted avg       0.99      0.99      0.99       898
```

## ロジスティック回帰

```py:logistic01.py
#! /usr/bin/python
#
#	logistic.py
#
#						Sep/03/2020
#
from sklearn import datasets
import sklearn.metrics as metrics

from sklearn.linear_model import LogisticRegression

digits = datasets.load_digits()

X = digits.data
y = digits.target

X_train, y_train = X[0::2], y[0::2]
X_test, y_test = X[1::2], y[1::2]

clf = LogisticRegression(max_iter=2000)

clf.fit(X_train, y_train)

accuracy = clf.score(X_test, y_test)
print(f"正解率{accuracy}")

predicted = clf.predict(X_test)

print("classification report")
print(metrics.classification_report(y_test, predicted))
```

実行結果

```text
$ ./logistic01.py 
正解率0.9532293986636972
classification report
              precision    recall  f1-score   support

           0       1.00      0.98      0.99        88
           1       0.87      0.98      0.92        89
           2       0.97      1.00      0.98        91
           3       0.98      0.92      0.95        93
           4       0.93      0.98      0.96        88
           5       0.96      0.95      0.95        91
           6       0.97      0.99      0.98        90
           7       0.99      0.97      0.98        91
           8       0.95      0.88      0.92        86
           9       0.93      0.89      0.91        91

    accuracy                           0.95       898
   macro avg       0.95      0.95      0.95       898
weighted avg       0.95      0.95      0.95       898
```

## 関連記事
[scikit-learn の使い方 (その2)](https://qiita.com/ekzemplaro/items/301bbf2dc90ddfdeed19)
[scikit-learn の使い方 (その3)](https://qiita.com/ekzemplaro/items/328f6aec7d98db364a2f)
[scikit-learn の使い方 (その4)](https://qiita.com/ekzemplaro/items/93dfad62c0e5fbf1f74d)