連立一次方程式


A = np.array([[2,4,6,8,10],
			[3,8,7,6,2],
			[5,7,21,44,25]])
b = np.array([[6],[15],[24]])

piA = np.linalg.pinv(A)

x = np.dot(piA, b)

print(x)
print(np.dot(A, x))

[vagrant@localhost python]$ python myapp.py
[[ 0.34401851]
[ 1.0376735 ]
[ 0.68374091]
[ 0.33377396]
[-0.56113681]]
[[ 6.]
[15.]
[24.]]

連立一次方程式、というか、行列の計算か。
keep going, do not stop

numpyの基本2

sortを操る

x = np.random.randint(0, 10, size=10)

print(x)
print(np.sort(x))

[vagrant@localhost python]$ python myapp.py
[7 3 3 2 9 1 4 9 1 5]
[1 1 2 3 3 4 5 7 9 9]

こうすると逆になる
print(np.sort(x)[::-1])

numpyの基本

四則計算

import numpy as np

res1 = np.add(20,30)
res2 = np.subtract(20,55)
res3 = np.multiply(40,25)
res4 = np.divide(500,5)

print(
	res1,
	res2,
	res3,
	res4,
)

[vagrant@localhost python]$ python myapp.py
50 -35 1000 100.0

addとsubtractは行列にも適用できる。行列の掛け算はnp.dotでしたね。

累乗と平方根

import numpy as np

res1 = np.power(2,4)
res2 = np.sqrt(2)
res3 = np.sqrt(4)

print(
	res1,
	res2,
	res3,
)

[vagrant@localhost python]$ python myapp.py
16 1.4142135623730951 2.0
なるほどー、power, sqrtか。

res1 = np.sin(30)
res2 = np.cos(30)
res3 = np.tan(30)

[vagrant@localhost python]$ python myapp.py
-0.9880316240928618 0.15425144988758405 -6.405331196646276

引数はradian。通りでおかしなことになってるわけだ。
180°がπ radian

30°を表したいなら

res1 = np.sin(np.pi * 1/6)
res2 = np.cos(np.pi * 1/6)
res3 = np.tan(np.pi * 1/6)

[vagrant@localhost python]$ python myapp.py
0.49999999999999994 0.8660254037844387 0.5773502691896257
あああああああ、誤差がああああああ、というか、これ、途方も無いな。。
全然先が見えないんだが。。ま、phpよりもpythonの方が遥かに計算の相性が良いことがわかってきた。

numpyとは?

-numpyはpythonで学術計算をする為のライブラリ
-機械学習でも頻繁に使われるライブラリ
-基本的な計算はPythonだけでも出来るが、numpyを使うとその計算が高速になったり、より楽になったりする

import numpy as np

vec1 = np.array([1,2,3])
print(vec1)

(anaconda3-2019.03) [vagrant@localhost python]$ pyenv global 3.6.4
[vagrant@localhost python]$ python -V
Python 3.6.4
[vagrant@localhost python]$ python myapp.py
[1 2 3]

ん、配列のカンマが消えてる?
あ、あ、行ベクトルを表現してるのかな。いきなりつまづいたなー

vec1 = np.array([[1,2,3],[4,5,6]])

二次元の行列。配列を入れ子にする。
[vagrant@localhost python]$ python myapp.py
[[1 2 3]
[4 5 6]]

ベクトルの足し算、引き算

vecx = np.array([[1,2,3],[4,5,6]])
vecy = np.array([[4,5,6],[7,8,9]])
vecz = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(vecx + vecy)
print(vecx - vecy)

[vagrant@localhost python]$ python myapp.py
[[ 5 7 9]
[11 13 15]]
[[-3 -3 -3]
[-3 -3 -3]]

ベクトルの掛け算

vecx = np.array([[1,2,3],[4,5,6]])
vecy = np.array([[1,2],[2,3]])
vecz = np.array([[1,2,3],[4,5,6],[7,8,9]])
res = np.dot(vecx, vecz)
print(res)

[vagrant@localhost python]$ python myapp.py
[[30 36 42]
[66 81 96]]

行分の列がないと掛け算は成立しませんね。
vecz = np.array([[1],[4],[7]])

anaconda

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

x = np.arange(0,11)
y = 4*x+3
plt.plot(x,y)
plt.grid()

[vagrant@localhost python]$ python myapp.py
File “myapp.py”, line 3
%matplotlib inline
^
SyntaxError: invalid syntax

ん?これって、anacondaが入ってないって事?

[vagrant@localhost python]$ pyenv -v
pyenv 1.2.13

[vagrant@localhost python]$ pyenv install anaconda3-2019.03
Downloading Anaconda3-2019.03-Linux-x86_64.sh…
-> https://repo.continuum.io/archive/Anaconda3-2019.03-Linux-x86_64.sh
Installing Anaconda3-2019.03-Linux-x86_64…
Installed Anaconda3-2019.03-Linux-x86_64 to /home/vagrant/.pyenv/versions/anaconda3-2019.03

[vagrant@localhost python]$ python –version
Python 3.7.3
(anaconda3-2019.03)

(anaconda3-2019.03) [vagrant@localhost python]$ python myapp.py
File “myapp.py”, line 3
% matplotlib inline
^
SyntaxError: invalid syntax

これはどうやらコマンドラインで画像を描画できないからっぽいですね。
まあよかった、原因がわかって。

centosのpythonをupdateしよう

from sklearn.datasets import load_boston
boston = load_boston()

import pandas as pd
boston_df = pd.DataFrame(boston.data, columns = boston.feature_names)
boston_df['MEDV'] = boston.target

boston_df.head()

TypeError: descriptor ‘__subclasses__’ of ‘type’ object needs an argument

[vagrant@localhost python]$ sudo yum install -y https://centos6.iuscommunity.org/ius-release.rpm

まず、python36を入れます。
[vagrant@localhost python]$ sudo yum install -y python36*

[vagrant@localhost python]$ pyenv versions
system
* 3.5.2 (set by /home/vagrant/.pyenv/version)
[vagrant@localhost python]$ pyenv install 3.6.4
Downloading Python-3.6.4.tar.xz…
-> https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Installing Python-3.6.4…
Installed Python-3.6.4 to /home/vagrant/.pyenv/versions/3.6.4

[vagrant@localhost python]$ pyenv global 3.6.4
[vagrant@localhost python]$ python -V
Python 3.6.4

[vagrant@localhost python]$ pip list
Package Version
——————– ——-
absl-py 0.8.0
astor 0.8.0
chainer 6.3.0
cycler 0.10.0
decorator 4.4.0
filelock 3.0.12
gast 0.2.2
google-pasta 0.1.7
grpcio 1.23.0
h5py 2.9.0
imageio 2.5.0
joblib 0.13.2
Keras 2.2.5
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.0
kiwisolver 1.1.0
Markdown 3.1.1
matplotlib 3.1.1
networkx 2.3
numpy 1.17.1
pandas 0.25.1
Pillow 6.1.0
pip 19.2.3
protobuf 3.7.1
pyparsing 2.4.2
python-dateutil 2.8.0
pytz 2019.2
PyWavelets 1.0.3
PyYAML 5.1.2
scikit-image 0.15.0
scikit-learn 0.21.3
scipy 1.3.1
setuptools 28.8.0
six 1.12.0
tensorboard 1.14.0
tensorflow 1.14.0
tensorflow-estimator 1.14.0
termcolor 1.1.0
Theano 1.0.4
typing 3.6.6
typing-extensions 3.6.6
Werkzeug 0.15.5
wheel 0.33.6
wrapt 1.11.2

[vagrant@localhost python]$ python myapp.py
/home/vagrant/.pyenv/versions/3.6.4/lib/python3.6/site-packages/pandas/compat/__init__.py:84: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)
/home/vagrant/.pyenv/versions/3.6.4/lib/python3.6/site-packages/pandas/compat/__init__.py:84: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
warnings.warn(msg)

なにいいいいいいいいいいいいいいいいいいいいいい

linear regression

線形回帰モデル(Linear Regression)とは、回帰式を用いて説明変数から目的変数の値を予測する

y = b1x1 + b2x2 + b3x3 + … + bkxk + e(誤差)

scikit-learn は線形回帰を行う予想クラスとして
sklearn.linear_model.LinearRegressionが用意されている
{code}
sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False, copy_X=True, n_jobs=1)
{/code}

[vagrant@localhost python]$ sudo ln -s /usr/bin/python3.6 /usr/bin/python3
[vagrant@localhost python]$ sudo ln -s /usr/bin/pip3.6 /usr/bin/pip3
[vagrant@localhost python]$ python -V
Python 3.5.2

あれ、/usr/bin/python3ではなく、/usr/bin/pythonか。。

python3 線形回帰分析

import pandas as pd
import numpy as np

wine = pd.read_csv("winequality-red.csv", sep=";")
wine.head

from sklearn import linear_model
clf = linear_model.LinearRegression()

X = wine.log[:, ['density']].as_matrix()

Y = wine['alcohol'].as_matrix()

clf.fit(X, Y)

print(clf.coef_)
print(clf.intercept_)
print(clf.score(X, Y))

[vagrant@localhost python]$ python myapp.py
Traceback (most recent call last):
File “myapp.py”, line 1, in
import pandas as pd
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/__init__.py”, line 55, in
from pandas.core.api import (
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/api.py”, line 5, in
from pandas.core.arrays.integer import (
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/arrays/__init__.py”, line 1, in
from .array_ import array # noqa: F401
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/arrays/array_.py”, line 7, in
from pandas.core.dtypes.common import (
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/dtypes/common.py”, line 11, in
from pandas.core.dtypes.dtypes import (
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/dtypes/dtypes.py”, line 53, in
class Registry:
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages/pandas/core/dtypes/dtypes.py”, line 84, in Registry
self, dtype: Union[Type[ExtensionDtype], str]
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/typing.py”, line 552, in __getitem__
dict(self.__dict__), parameters, _root=True)
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/typing.py”, line 512, in __new__
for t2 in all_params – {t1} if not isinstance(t2, TypeVar)):
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/typing.py”, line 512, in
for t2 in all_params – {t1} if not isinstance(t2, TypeVar)):
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/typing.py”, line 1077, in __subclasscheck__
if super().__subclasscheck__(cls):
File “/home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/abc.py”, line 225, in __subclasscheck__
for scls in cls.__subclasses__():
TypeError: descriptor ‘__subclasses__’ of ‘type’ object needs an argument

python3.5.2が行けなかったか。。

sk-learnを使ってみよう

————— ——-
cycler 0.10.0
joblib 0.13.2
kiwisolver 1.1.0
matplotlib 3.0.3
numpy 1.17.1
pandas 0.25.1
pip 19.2.3
pyparsing 2.4.2
python-dateutil 2.8.0
pytz 2019.2
scikit-learn 0.21.3
scipy 1.3.1
setuptools 20.10.1
six 1.12.0

from sklearn import svm
xor_data = [
	[0,0,0],
	[0,1,0],
	[1,0,1],
	[1,1,0]
]
data = []
label = []
for row in xor_data:
	p = row[0]
	q = row[1]
	r = row[2]
	data.append([p,q])
	label.append(r)
clf = svm.SVC(gamma="auto")
clf.fit(data, label)
pre = clf.predict(data)
print("予想結果:", pre)
ok = 0; total = 0
for idx, answer in enumerate(label):
	p = pre[idx]
	if p == answer: ok += 1
	total += 1
print("正解率:", ok, "/", total, "=", ok/total)

[vagrant@localhost python]$ python myapp.py
予想結果: [0 0 0 0]
正解率: 3 / 4 = 0.75

OK、取り敢えず環境は整ってきた

scikit-learn

classification
Regression
clustering
dimensionality reduction
model selection
preprocessing

これを使って行きたい
[vagrant@localhost python]$ pip3.5 -V
pip 8.1.1 from /home/vagrant/.pyenv/versions/3.5.2/lib/python3.5/site-packages (python 3.5)
[vagrant@localhost python]$ pip install -U scikit-learn scipy matplotlib scikit-image
Collecting scikit-learn
Downloading https://files.pythonhosted.org/packages/1f/af/e3c3cd6f61093830059138624dbd26d938d6da1caeec5aeabe772b916069/scikit_learn-0.21.3-cp35-cp35m-manylinux1_x86_64.whl (6.6MB)
100% |████████████████████████████████| 6.6MB 230kB/s
Collecting scipy
Downloading https://files.pythonhosted.org/packages/7a/0e/3781e028d62a8422244582abd8f084e6314297026760587c85607f687bf3/scipy-1.3.1-cp35-cp35m-manylinux1_x86_64.whl (25.1MB)
100% |████████████████████████████████| 25.1MB 63kB/s
Collecting matplotlib
Downloading https://files.pythonhosted.org/packages/12/d1/7b12cd79c791348cb0c78ce6e7d16bd72992f13c9f1e8e43d2725a6d8adf/matplotlib-3.1.1.tar.gz (37.8MB)
100% |████████████████████████████████| 37.8MB 41kB/s
Complete output from command python setup.py egg_info:

Beginning with Matplotlib 3.1, Python 3.6 or above is required.

This may be due to an out of date pip.

Make sure you have pip >= 9.0.1.

—————————————-
Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-k4fsv7pl/matplotlib/
You are using pip version 8.1.1, however version 19.2.3 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
[vagrant@localhost python]$ pip install pandas
Collecting pandas
Downloading https://files.pythonhosted.org/packages/d9/05/38875a81040e679c196a854865dbafe4dfe5f92e8365ddfff21f2817d89d/pandas-0.25.1-cp35-cp35m-manylinux1_x86_64.whl (10.3MB)
100% |████████████████████████████████| 10.3MB 149kB/s
Collecting pytz>=2017.2 (from pandas)
Downloading https://files.pythonhosted.org/packages/87/76/46d697698a143e05f77bec5a526bf4e56a0be61d63425b68f4ba553b51f2/pytz-2019.2-py2.py3-none-any.whl (508kB)
100% |████████████████████████████████| 512kB 1.4MB/s
Collecting python-dateutil>=2.6.1 (from pandas)
Downloading https://files.pythonhosted.org/packages/41/17/c62faccbfbd163c7f57f3844689e3a78bae1f403648a6afb1d0866d87fbb/python_dateutil-2.8.0-py2.py3-none-any.whl (226kB)
100% |████████████████████████████████| 235kB 2.3MB/s
Collecting numpy>=1.13.3 (from pandas)
Downloading https://files.pythonhosted.org/packages/d4/64/7619774f0bd8ef364d46a5df8eb1bc78784cd787324b9624f6793e72f787/numpy-1.17.1-cp35-cp35m-manylinux1_x86_64.whl (20.1MB)
100% |████████████████████████████████| 20.2MB 79kB/s
Collecting six>=1.5 (from python-dateutil>=2.6.1->pandas)
Downloading https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Installing collected packages: pytz, six, python-dateutil, numpy, pandas
Successfully installed numpy-1.17.1 pandas-0.25.1 python-dateutil-2.8.0 pytz-2019.2 six-1.12.0
You are using pip version 8.1.1, however version 19.2.3 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.

[vagrant@localhost python]$ pip list
Package Version
————— ——-
cycler 0.10.0
joblib 0.13.2
kiwisolver 1.1.0
matplotlib 3.0.3
numpy 1.17.1
pandas 0.25.1
pip 19.2.3
pyparsing 2.4.2
python-dateutil 2.8.0
pytz 2019.2
scikit-learn 0.21.3
scipy 1.3.1
setuptools 20.10.1
six 1.12.0