%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def step_function(x):
# if x > 0:
# return 1
# else:
# return 0
y = x > 0
return y.astype(np.int)
x = np.array([-1.0, 1.0, 2.0])
y = x > 0
y
array([False, True, True], dtype=bool)
y.astype(np.int)
array([0, 1, 1])
x = np.arange(-5.0, 5.0, 0.1)
y = step_function(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
(-0.1, 1.1)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
x = np.array([-1.0, 1.0, 2.0])
sigmoid(x)
array([ 0.26894142, 0.73105858, 0.88079708])
t = np.array([1.0, 2.0, 3.0])
1.0 + t
array([ 2., 3., 4.])
1.0 / t
array([ 1. , 0.5 , 0.33333333])
x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
(-0.1, 1.1)
x = np.arange(-5.0, 5.0, 0.1)
y1 = step_function(x)
y2 = sigmoid(x)
plt.plot(x, y1, "--")
1plt.plot(x, y2)
plt.ylim(-0.1, 1.1)
(-0.1, 1.1)
def relu(x):
return np.maximum(0, x)
x = np.arange(-5.0, 5.0, 0.1)
# y1 = step_function(x)
# y2 = sigmoid(x)
# plt.plot(x, y1, "--")
# plt.plot(x, y2)
y = relu(x)
plt.plot(x, y)
plt.ylim(-1, 6)
(-1, 6)
A = np.array([1, 2, 3, 4])
A
array([1, 2, 3, 4])
np.ndim(A)
1
A.shape
(4,)
A.shape[0]
4
B = np.array([[1, 2], [3, 4], [5, 6]])
B
array([[1, 2], [3, 4], [5, 6]])
np.ndim(B)
2
B.shape
(3, 2)
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# np.dot(A, B)
A.dot(B)
array([[19, 22], [43, 50]])
A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[1, 2], [3, 4], [5, 6]])
# np.dot(A, B)
A.dot(B)
array([[22, 28], [49, 64]])
C = np.array([[1, 2], [3, 4]])
A.dot(C)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-28-d891270025f5> in <module>() 1 C = np.array([[1, 2], [3, 4]]) ----> 2 A.dot(C) ValueError: shapes (2,3) and (2,2) not aligned: 3 (dim 1) != 2 (dim 0)
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([7, 8])
A.dot(B)
array([23, 53, 83])
A
array([[1, 2], [3, 4], [5, 6]])
A.T
array([[1, 3, 5], [2, 4, 6]])