In [1]:
import numpy as np

x = [1, 2, 3, 4, 5, 6 ]
v = np.array(x)
v
Out[1]:
array([1, 2, 3, 4, 5, 6])
In [2]:
M = np.array([[1,2], [3,4]])
M
Out[2]:
array([[1, 2],
       [3, 4]])
In [3]:
type(v), type(M)
Out[3]:
(numpy.ndarray, numpy.ndarray)
In [4]:
v.shape
Out[4]:
(6,)
In [5]:
M.shape
Out[5]:
(2, 2)
In [6]:
M.size
Out[6]:
4
In [7]:
M.dtype
Out[7]:
dtype('int64')
In [8]:
np.random.rand(5,3)
Out[8]:
array([[ 0.18883563,  0.75048581,  0.26255198],
       [ 0.08558843,  0.60417858,  0.23096214],
       [ 0.48921946,  0.29703874,  0.59080132],
       [ 0.06991706,  0.59484146,  0.92259311],
       [ 0.53895609,  0.08804871,  0.42498876]])
In [9]:
np.zeros([5,3])
Out[9]:
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
In [10]:
M = np.array([[1,2,3], [4,5,6]])
N = np.array([[1,1,1], [2,2,2]])
print(N)
print(M)
[[1 1 1]
 [2 2 2]]
[[1 2 3]
 [4 5 6]]
In [11]:
M + N
Out[11]:
array([[2, 3, 4],
       [6, 7, 8]])
In [12]:
5*M
Out[12]:
array([[ 5, 10, 15],
       [20, 25, 30]])
In [13]:
M*N
Out[13]:
array([[ 1,  2,  3],
       [ 8, 10, 12]])
In [14]:
# Mno┼╝enie macierzy
try:
    M.dot(N)
except Exception as e:
    print('Exception:',e)
Exception: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)
In [15]:
# Transpozycja
M.dot(N.T)
Out[15]:
array([[ 6, 12],
       [15, 30]])
In [16]:
v=np.linspace(0,10,21)
v
Out[16]:
array([  0. ,   0.5,   1. ,   1.5,   2. ,   2.5,   3. ,   3.5,   4. ,
         4.5,   5. ,   5.5,   6. ,   6.5,   7. ,   7.5,   8. ,   8.5,
         9. ,   9.5,  10. ])
In [17]:
np.sin(v)
Out[17]:
array([ 0.        ,  0.47942554,  0.84147098,  0.99749499,  0.90929743,
        0.59847214,  0.14112001, -0.35078323, -0.7568025 , -0.97753012,
       -0.95892427, -0.70554033, -0.2794155 ,  0.21511999,  0.6569866 ,
        0.93799998,  0.98935825,  0.79848711,  0.41211849, -0.07515112,
       -0.54402111])
In [18]:
%matplotlib inline
from matplotlib import pyplot as plt
plt.plot(v, np.sin(v))
Out[18]:
[<matplotlib.lines.Line2D at 0x1052ef518>]
In [19]:
M = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]])
M
Out[19]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
In [20]:
M[:,2]
Out[20]:
array([ 3,  7, 11, 15])
In [21]:
M[2,1:3]
Out[21]:
array([10, 11])
In [22]:
M[1:3,2:4]
Out[22]:
array([[ 7,  8],
       [11, 12]])
In [23]:
M[::2,:]
Out[23]:
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])
In [24]:
M
Out[24]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])
In [25]:
M[::2,::2]=0
M
Out[25]:
array([[ 0,  2,  0,  4],
       [ 5,  6,  7,  8],
       [ 0, 10,  0, 12],
       [13, 14, 15, 16]])