python learning notes (2): numpy Foundation

  • Counter function can count the number of data in the list
  • Most common (10) can extract the top ten bits
from collections import Counter
a = ['q','q','w','w','w']
count = Counter(a)
count.most_common(1)
[('w', 3)]
count
Counter({'q': 2, 'w': 3})
  • The series object in panda has a value "counts method to count

  • The. fillna() function can replace the true value NA

import numpy as np
from numpy.random import randn
data = {i : randn() for i in range(7)}
data
{0: -0.2657989059225722,
 1: -1.2517286143172295,
 2: -0.6360811023039581,
 3: 1.2009891917346602,
 4: 1.7528414640242418,
 5: -0.24155970563487628,
 6: -0.7637924413712933}
  • The last two results are saved in and
9*3
27
_
27
%pwd
# Get current working directory
'D:\\Code\\Python\\code'
  • Magic command
  • %Time the execution time of a statement
  • %The average time it takes to execute multiple times

numpy Foundation

import numpy as np
data1 = [6, 7.5, 8],[2, 0 ,1]
arr1 = np.array(data1)
arr1
array([[6. , 7.5, 8. ],
       [2. , 0. , 1. ]])
arr1.ndim
2
arr1.shape
(2, 3)
arr1.dtype
dtype('float64')
np.zeros((2, 3, 4))
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]])
np.ones((3))
array([1., 1., 1.])
np.arange(9)
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.eye(3,)
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
  • The changes after array slicing will be reflected on the original array
arr = np.arange(9)
arr2 = arr[5:8]
arr2[:] = 4
arr
array([0, 1, 2, 3, 4, 4, 4, 4, 8])
arr = np.arange(9)
arr2 = arr[5:8].copy()
arr2[:] = 4
arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
  • Three dimensional array is layer, row and column first
data = np.random.randn(4,3)
data
array([[ 0.7144327 ,  0.87144603,  1.10651404],
       [-0.19509352, -0.01102958,  1.8051039 ],
       [ 0.03106339,  0.83767495,  0.20094192],
       [ 0.96032146, -0.40303045,  1.4522938 ]])
data[[1,2,0],[1,2,0]]
# The extracted data is (11) (22) (00)
array([-0.01102958,  0.20094192,  0.7144327 ])
  • Discontinuous data extraction
data[[0,2]][:,[0,2]]
# Jump to method one
array([[0.7144327 , 1.10651404],
       [0.03106339, 0.20094192]])
data[np.ix_([0,2],[0,2])]
# Jump to method two
array([[0.7144327 , 1.10651404],
       [0.03106339, 0.20094192]])
data.T
array([[ 0.7144327 , -0.19509352,  0.03106339,  0.96032146],
       [ 0.87144603, -0.01102958,  0.83767495, -0.40303045],
       [ 1.10651404,  1.8051039 ,  0.20094192,  1.4522938 ]])
np.sqrt(data)
F:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in sqrt
  """Entry point for launching an IPython kernel.





array([[0.84524121, 0.93351274, 1.05190971],
       [       nan,        nan, 1.34354155],
       [0.17624808, 0.91524584, 0.44826546],
       [0.97995993,        nan, 1.20511153]])
np.exp(data)
array([[2.04302734, 2.39036489, 3.02379915],
       [0.82275771, 0.98903102, 6.0806032 ],
       [1.03155089, 2.31098757, 1.22255377],
       [2.61253617, 0.66829175, 4.27290447]])
np.rint(data)
# Rounding
array([[ 1.,  1.,  1.],
       [-0., -0.,  2.],
       [ 0.,  1.,  0.],
       [ 1., -0.,  1.]])
np.modf(data)
# Divide data into decimal and integer parts
(array([[ 0.7144327 ,  0.87144603,  0.10651404],
        [-0.19509352, -0.01102958,  0.8051039 ],
        [ 0.03106339,  0.83767495,  0.20094192],
        [ 0.96032146, -0.40303045,  0.4522938 ]]), array([[ 0.,  0.,  1.],
        [-0., -0.,  1.],
        [ 0.,  0.,  0.],
        [ 0., -0.,  1.]]))
np.isnan(data)
array([[False, False, False],
       [False, False, False],
       [False, False, False],
       [False, False, False]])
np.where(data > 0,9,data)
array([[ 9.        ,  9.        ,  9.        ],
       [-0.19509352, -0.01102958,  9.        ],
       [ 9.        ,  9.        ,  9.        ],
       [ 9.        , -0.40303045,  9.        ]])
  • In axis, 0 represents vertical summation and 1 represents horizontal summation
np.mean(data,axis=1)
array([0.89746426, 0.5329936 , 0.35656009, 0.6698616 ])
np.in1d(data, [1,2,3])
# Check whether each element in data is within 1, 2 and 3
array([False, False, False, False, False, False, False, False, False,
       False, False, False])
from numpy.linalg import inv, qr
from numpy.random import randn
x = randn(5,5)
mat = x.dot(inv(x))
# Inverse
mat = np.rint(mat)
mat
array([[ 1.,  0., -0.,  0.,  0.],
       [ 0.,  1., -0.,  0.,  0.],
       [ 0., -0.,  1.,  0.,  0.],
       [-0.,  0., -0.,  1., -0.],
       [-0.,  0.,  0., -0.,  1.]])
np.diag(mat)
# Return diagonal element
array([1., 1., 1., 1., 1.])
np.random.permutation(mat)
# Random arrangement of return sequences
array([[ 0., -0.,  1.,  0.,  0.],
       [-0.,  0., -0.,  1., -0.],
       [ 1.,  0., -0.,  0.,  0.],
       [-0.,  0.,  0., -0.,  1.],
       [ 0.,  1., -0.,  0.,  0.]])
np.random.randint(0,2,12)
array([1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1])

Tags: Python Anaconda IPython

Posted on Tue, 31 Mar 2020 16:48:30 -0700 by ChrisF79