Memo

メモ > 技術 > プログラミング言語: Python > scikit-learn(機械学習)

■scikit-learn(機械学習)
scikit-learn(サイキット・ラーン)は、Python向けの機械学習フレームワーク 様々なアルゴリズムに対応しており、すぐに機械学習を試せるようにサンプルデータも含まれている 機械学習でよく使われる他のライブラリ(PandasやNumPyなど)との親和性が高い BSDライセンスのオープンソースのため、無償で商用利用が可能 ■インストール
$ sudo pip3 install sklearn $ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from sklearn.svm import LinearSVC >>> from sklearn.metrics import accuracy_score >>> exit()
機械学習のライブラリ!scikit-learnとは【初心者向け】 | TechAcademyマガジン https://techacademy.jp/magazine/17375 使用すべきアルゴリズムについても、上記ページで紹介されている 以下のプログラムでは、中小規模のクラス分類手法である Linear SVC を使用している
from sklearn.svm import LinearSVC from sklearn.metrics import accuracy_score # 学習用データと結果の準備 learn_data = [[0, 0], [1, 0], [0, 1], [1, 1]] learn_label = [0, 0, 0, 1] # アルゴリズムの指定(LinearSVC) clf = LinearSVC() # 学習用データと結果の学習 clf.fit(learn_data, learn_label) # テストデータによる予測 test_data = [[0, 0], [1, 0], [0, 1], [1, 1]] test_label = clf.predict(test_data) # 予測結果の評価 print(test_data, " の予測結果: ", test_label) print("正解率: ", accuracy_score([0, 0, 0, 1], test_label))
■インストール時にエラー インストール時に以下のようなエラーになることがあった
$ sudo pip3 install sklearn ERROR: Command errored out with exit status 1: /usr/local/bin/python3.8 /usr/local/lib/python3.8/site-packages/pip install
Python - pythonでscikit-learnがインストールできない|teratail https://teratail.com/questions/223561 Pythonの新しいバージョンに対応できていない可能性があるので、バージョンを下げて試す このときはPython3.8で上記エラーになったが、Python3.7だとインストールできた ■AnacondaのCGI環境で動作しないので試行錯誤中 Anaconda Prompt では動作するが、コマンドプロンプトやCGIだとscikitを認識できない? 以下の解説に習って「Add Anaconda to the system PATH environment variable」のチェックを外したが、それが問題になっているかも 【Python】非エンジニアWindowユーザー向けのAnacondaインストールの手順 https://tonari-it.com/python-anaconda-install/ 以下の記事では、チェックを入れるように案内されている Windows10でpythonを使う環境を作る?:AnacondaとPycharm - プロクラシスト https://www.procrasist.com/entry/2016/10/04/200000 チェックを入れて再インストールすると、コマンドプロンプトからは sklearn を呼び出せた …と思ったが、C:\Users\refirio\Anaconda3 の場所で実行するか否かによって違う? コマンドプロンプトでもアナコンダプロンプトでも同じ? ただしCGI経由だと以下のエラーになる Pythonのパスは #!/Users/refirio/Anaconda3/python から変える必要はないか
[Tue Sep 24 00:19:40.842477 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] End of script output before headers: sklearn.py [Tue Sep 24 00:19:40.842477 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: Traceback (most recent call last):\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py [Tue Sep 24 00:19:40.842477 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: File "C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py", line 9, in <module>\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py [Tue Sep 24 00:19:40.843483 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: from sklearn.svm import LinearSVC\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py [Tue Sep 24 00:19:40.843483 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: File "C:\\localhost\\home\\raspberrypi\\public_html\\cgi\\sklearn.py", line 9, in <module>\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py [Tue Sep 24 00:19:40.843483 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: from sklearn.svm import LinearSVC\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py [Tue Sep 24 00:19:40.843483 2019] [cgi:error] [pid 11380:tid 1900] [client ::1:50053] AH01215: ModuleNotFoundError: No module named 'sklearn.svm'; 'sklearn' is not a package\r: C:/localhost/home/raspberrypi/public_html/cgi/sklearn.py
以下、試行錯誤
pip list pip install sklearn pip install sklearn.svm pip install scikit-learn
【Python】アナコンダプロンプトだけでなくコマンドプロンプトでもPythonを使えるようにする|ぷんたむの悟りの書 https://punhundon-lifeshift.com/python_command_prompt anaconda 環境を設定しscikit-learnを実行する - Qiita https://qiita.com/KENOSIN/items/f9fef4da20cdd4489950 Pythonおよび機械学習勉強用のRaspberryPiの構築 - Qiita https://qiita.com/rhene/items/71b92c253d5ac2a4cc52 いったん諦めて、常に Raspberry Pi 環境で実行することにした ■Raspberry PiのCGI環境で動作しないので試行錯誤中
$ sudo pip3 install sklearn $ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from sklearn.svm import LinearSVC >>> from sklearn.metrics import accuracy_score >>> exit() $ python3 test.py この環境でも、コマンドラインからは呼び出せるが、CGI経由だと以下のエラーになる CGI経由の場合は環境変数がセットされないので、何らかの対策が必要? [Wed Sep 25 00:03:37.128014 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: Traceback (most recent call last):: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.128256 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: File "/var/www/cgi-bin/test/sklearn.py", line 5, in <module>: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.128320 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: from sklearn.svm import LinearSVC: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.128409 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: File "/var/www/cgi-bin/test/sklearn.py", line 5, in <module>: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.128469 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: from sklearn.svm import LinearSVC: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.128578 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] AH01215: ModuleNotFoundError: No module named 'sklearn.svm'; 'sklearn' is not a package: /var/www/cgi-bin/test/sklearn.py [Wed Sep 25 00:03:37.143283 2019] [cgi:error] [pid 19171:tid 1952445472] [client 192.168.1.110:50839] End of script output before headers: sklearn.py

Advertisement