![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
前回は「Matplotlib」ライブラリを使って、グラフを描くことに挑戦しましたね?
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
はい!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
今回も「Matplotlib」ライブラリと新しいライブラリを使います。
ライブラリを使って、データを確認する手法をお教えします!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
お願いします!
Scikit-learnについて知ろう
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
早速ですが、今回新たに使うライブラリは「Scikit-learn」です。
「サイキット・ラーン」と読みます。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
これはどのようなライブラリですか?
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
「Scikit-learn」ライブラリは機械学習に関するアルゴリズムやデータセットが入ったライブラリです。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
アルゴリズム?データセット?
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
新しい言葉が出てきましたね。
「アルゴリズム」とは、ある処理を行う際の手順や計算方法のことです。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
なるほど!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
そして、「データセット」について説明します。
AIに機械学習を行わせるために必要なデータの集まりのことを言います。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
AIに学習してもらうたくさんのデータのことを言うんですね!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
その通りです!
今回は、AIに手書きの数字を判別してもらおうとしていますが、手書きの数字のデータセットも「Scikit-learn」ライブラリに入っているため、今回はこのライブラリを使います。
- scikit-learnライブラリ=機械学習に関するアルゴリズムやデータセットが入ったライブラリ
- アルゴリズム=ある処理を行う際の手順や計算方法
- データセット=AIに機械学習を行わせるために必要なデータの集まり
準備をしよう
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
まずは「Scikit-learn」ライブラリを使うための準備をしましょう!
Jupyter Notebookでファイルを作ってください。
ファイル名は、「digits」としてください。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
作りました!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
それでは、まず最初に以下のコードを実行してください。
!pip install sklearn
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
何かたくさん出てきました(汗)
![](http://cplab.blog/wp-content/uploads/2022/05/スクリーンショット-2022-05-31-20.54.17-1024x579.png)
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
一番最後の行に「Successfully installed…」と出ていますね。
つまり、インストールに成功したと書いてあるので、これで大丈夫です。
これで「Scikit-learn」ライブラリが使えるようになりましたよ。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
やった!
データセットを確認してみよう
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
それでは、ここからは実際にコーディングして実行してみましょう。
以下のコードを実行してください。
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn import datasets
# 手書きの数字のデータを読み込む
x = datasets.load_digits()
# 手書きの数字のデータを画面に表示する
for i in range(10):
plt.subplot(2, 5, i+1)
plt.axis("off")
plt.title(str(x.target[i]))
plt.imshow(x.images[i], cmap="gray")
plt.show()
![](http://cplab.blog/wp-content/uploads/2022/05/スクリーンショット-2022-05-31-21.18.14-1024x478.png)
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
無事に画像が出てきました!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
よくできました!
では、コードの解説をしていきますね!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
お願いします!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
まず、「%matplotlib inline」と「import matplotlib.pyplot as plt」は前回やったので分かりますね?
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
はい!
「%matplotlib inline」はJupyter NotebookでMatplotlibライブラリを使うときに必要なコードで、「import matplotlib.pyplot as plt」はライブラリを読み込むコードですね。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
その通りです!
では、次にいきましょう!
次は、「from sklearn import datasets」ですが、これは「Scikit-learn」ライブラリの中の「datasets」モジュールを読み込むためのコードです。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
なるほど!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
次に、「# 〜」の一文ですが、これは分かりますか?
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
「# 〜」の文だけ色が他と違う色になってますが…
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
そうですね。
実はこれは、「コメントアウト」というものです。
プログラムに説明などのコメントを残したいときに、頭に#をつけて文を書きます。
コメントアウトの文は処理されないので、日本語で何か書いても大丈夫ですよ。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
分かりました!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
次に、「x = datasets.load_digits()」は、「datasets」モジュールに入っている手書きの数字のデータセットを読み込む命令です。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
この命令を書くことで、手書きの数字のデータが読み込まれるわけですね!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
そうです!
そして、それ以下のプログラムの説明ですが、これは繰り返しの処理で、またじっくり説明したいので、次回説明しますね。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
分かりました!
- from sklearn import datasets=「scikit-learn」ライブラリの中の「datasets」モジュールを読み込ませる
- #を頭につけるとコメントアウトができる
まとめ
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
今日は、「Scikit-learn」ライブラリの使い方について学びましたね。
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-man.png)
このライブラリは機械学習を行う上で重要なライブラリっぽいですね!
![](https://cplab.blog/wp-content/themes/cocoon-master/images/b-woman.png)
そうです!
これからAIを作る上でかなり必要になってきますので、覚えておいてくださいね。