!python --version
Python 3.6.5
#もう何年もやってるので省略
import numpy as np
np.__version__
'1.14.3'
x = np.array([1.0,2.0,3.0])
y = np.array([2.0,4.0,6.0])
x + y
array([3., 6., 9.])
x - y
array([-1., -2., -3.])
x * y
array([ 2., 8., 18.])
x / y
array([0.5, 0.5, 0.5])
x / 2.0
array([0.5, 1. , 1.5])
A = np.array([[1,2],[3,4]])
print(A)
[[1 2] [3 4]]
A.shape
(2, 2)
A.dtype
dtype('int32')
B = np.array([[3,0],[0,6]])
A + B
array([[ 4, 2], [ 3, 10]])
A * B
array([[ 3, 0], [ 0, 24]])
A*10
array([[10, 20], [30, 40]])
X = np.array([[51,55],[14,19],[0,4]])
X
array([[51, 55], [14, 19], [ 0, 4]])
X[0] #0行目
array([51, 55])
X[0][1] #(0,1) =>(0行1列)
55
for row in X:
print(row)
[51 55] [14 19] [0 4]
AX = X.flatten() #行列を配列に
AX
array([51, 55, 14, 19, 0, 4])
AX[AX>15]#配列から条件の値を抽出
array([51, 55, 19])
X>15
array([[ True, True], [False, True], [False, False]])
X[X>15]#行列でもできる。
array([51, 55, 19])
ニューラルネットワークにおいては行列は 行数=入力数 列数=重み数
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,6,0.1) #0to6 by 0.1 => sin(0to6)
y = np.sin(x)
plt.plot(x,y)
plt.show()
plt.close()
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,6,0.1) #0to6 by 0.1 => sin(0to6)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x,y1,label="sin(x)")
plt.plot(x,y2,label="cos(x)", linestyle="--")
plt.xlabel("x")
plt.ylabel("y")
plt.title("sin(x), cos(x)")
plt.legend()#show label box
plt.show()
plt.close()
%matplotlib inline
#plt.show()をinlineにて表示する
#Esc+Lで行番号を表示する
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,6,0.1) #0to6 by 0.1 => sin(0to6)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x,y1,label="sin(x)")
plt.plot(x,y2,label="cos(x)", linestyle="--")
plt.axhline(color='black',linestyle="--")
plt.xlabel("x")
plt.ylabel("y")
plt.title("sin(x), cos(x), y=0")
plt.legend()#show label box
plt.show()
plt.close()
from graphviz import Digraph
dot = Digraph(comment="単純パーセプトロン")
dot.attr(rankdir="LR")
dot.attr(splines="line")
dot.attr(fixedsize="true")
with dot.subgraph(name="cluster_x") as x:
x.attr(label="入力層")
x.attr(color="white")
x.node("x1","x1:入力1")
x.node("x2","x2:入力2")
with dot.subgraph(name="cluster_y") as y:
y.attr(label="出力層")
y.attr(color="white")
y.node("y","y:出力1")
dot.edge("x1", "y", label="w1:重み1")
dot.edge("x2", "y", label="w2:重み2")
#print(dot)
dot