pythonで2次方程式を解く

2次式は最高次数が2次の式
y = ax^2 + bx + c とすると、xとyの関係を表すグラフが放物線の軌道を描く

# solving a quadratic equation

def solv_quadratic_equation(a, b, c):
	""" 2次方程式を解く """
	D = (b**2 - 4*a*c) ** (1/2)
	x_1 = (-b + D) / (2 * a)
	x_2 = (-b - D) / (2 * a)

	return x_1, x_2

if __name__ == '__main__':
	a = input('input a: ')
	b = input('input b: ')
	c = input('input c: ')

	print(solv_quadratic_equation.__doc__)
	x1, x2 = solv_quadratic_equation(float(a), float(b), float(c))

	print('x1:{}'.format(x1))
	print('x2:{}'.format(x2))

[vagrant@localhost python]$ python myapp.py
input a: 1
input b: 2
input c: 1
2次方程式を解く
x1:-1.0
x2:-1.0
[vagrant@localhost python]$ python myapp.py
input a: 1
input b: 2
input c: 3
2次方程式を解く
x1:(-0.9999999999999999+1.414213562373

すげーーーーーーーーーーーーーーー&ドウシヨーーーーーーーーーーー

連立一次方程式


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の基本

四則計算

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、取り敢えず環境は整ってきた

python astモジュール

The ast module makes it easy to handle Python abstract syntax trees in Python applications. The abstract syntax itself can change with every release of Python. Using this module will help to learn the current grammer programmatically.

To create an abstract syntax tree, pass ast. PyCF_ONLY_AST as a flag for the built-in function compile() or use the helper function parse() provided by this module. The result is a tree of objects of classes that inherit from ast.AST. Abstract syntax trees can be compiled into Python code objects using the built-in function compile().

[vagrant@localhost test]$ python --version
Python 3.5.2
[vagrant@localhost test]$ cat << 'EOF' > helloworld.py
> !#/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> def main():
>   print('hello, world!')
>
> if __name__ == '__main__':
>   main()
> EOF

[vagrant@localhost test]$ python
Python 3.5.2 (default, Jul 28 2018, 11:25:01)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> FILENAME = ‘helloworld.py’
>>> with open(FILENAME, ‘r’) as f:
… source = f.read()
File ““, line 2
source = f.read()
^
IndentationError: expected an indented block

あれ、うまくいかんな。。