[PYTHON] Use Numpy

Introduction

Describes how to use Numpy.

Basic data (ndarray)

The basic data structure in Numpy is ndarray. Represents an N-dimensional array.

Describes how to create an ndarray.

Method using arange

>>> from numpy  import *

# range()Create a one-dimensional array with the same feeling as.
>>> a = arange(10);a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

#2D array with reshape(2 rows 5 columns)To
>>> a = arange(10).reshape(2,5);a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

#3D array(Two 3-by-4 matrices)
>>> a = arange(24).reshape(2,3,4);a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])

Method using array

#Create a one-dimensional array. Specify specific data.
>>> a = array([1,2,3]);a
array([1, 2, 3])

#Create a two-dimensional array.
>>> a = array([(1,2,3), (4,5,6)]);a
array([[1, 2, 3],
       [4, 5, 6]])

Method using zeros, ones, eye, empty

zeros creates an array with all zeros. ones creates an array with all 1 elements. eye creates an array with 1 diagonal element. empty creates an uninitialized array.

>>> a = zeros((2,3));a
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

>>> a = ones((2,3));a
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

>>> a = eye(3,3);a
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])

#The value of the element is undefined because it is uninitialized.
>>> a = empty((2,3));a
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])

Method using linspace

Create a one-dimensional array by specifying the start value, end value, and number of data. Useful when creating a graph of a function.

>>> x = linspace(0, 2*pi, 10);x
array([ 0.        ,  0.6981317 ,  1.3962634 ,  2.0943951 ,  2.7925268 ,
        3.4906585 ,  4.1887902 ,  4.88692191,  5.58505361,  6.28318531])

>>> sin(x)
array([  0.00000000e+00,   6.42787610e-01,   9.84807753e-01,
         8.66025404e-01,   3.42020143e-01,  -3.42020143e-01,
        -8.66025404e-01,  -9.84807753e-01,  -6.42787610e-01,
        -2.44929360e-16])

Method using random

Create an array with uniform random numbers and normal random numbers as elements.

# [0,1)Create a uniform random number for.
>>> a = random.random(10);a
array([ 0.55089503,  0.45425945,  0.57639104,  0.65731385,  0.06515141,
        0.75809653,  0.98538432,  0.14194245,  0.81874444,  0.77024755])

#Mean mu,Create a normal random number with standard deviation sigma.
>>> mu = 100; sigma=10
>>> a = random.normal(mu, sigma, 10).reshape(2,5);a
array([[  83.5821915 ,   82.34512541,  102.4633897 ,   72.53721859,
          96.21639407],
       [  91.54337302,   96.03740307,   82.04488924,  109.00803432,
         101.72208762]])

Calculation

Four arithmetic operations

>>> a = random.random(6).reshape(2,3);a
array([[ 0.52638479,  0.39144147,  0.75575996],
       [ 0.1557081 ,  0.11812827,  0.84377568]])
>>> b = random.random(6).reshape(2,3);b
array([[ 0.28797541,  0.60331924,  0.81078013],
       [ 0.83825199,  0.41031302,  0.64098045]])
>>> a + b
array([[ 0.8143602 ,  0.9947607 ,  1.56654009],
       [ 0.9939601 ,  0.52844128,  1.48475613]])
>>> a - b
array([[ 0.23840938, -0.21187777, -0.05502018],
       [-0.68254389, -0.29218475,  0.20279523]])
>>> a * b
array([[ 0.15158588,  0.23616417,  0.61275516],
       [ 0.13052263,  0.04846957,  0.54084371]])
>>> a / b
array([[ 1.82788103,  0.64881317,  0.93213921],
       [ 0.18575334,  0.28789793,  1.31638287]])

queue

>>> a = random.random(6).reshape(2,3);a
array([[ 0.26511097,  0.7092039 ,  0.78596957],
       [ 0.52916934,  0.00993958,  0.30160079]])
>>> b = random.random(6).reshape(3,2);b
array([[ 0.68708518,  0.77809895],
       [ 0.32463964,  0.35987488],
       [ 0.13998871,  0.65494372]])

#Matrix multiplication(dot)
>>> dot(a,b)
array([[ 0.52241638,  0.97627307],
       [ 0.4090319 ,  0.61285465]])

#Transpose matrix(T)
>>> a.T
array([[ 0.26511097,  0.52916934],
       [ 0.7092039 ,  0.00993958],
       [ 0.78596957,  0.30160079]])

#Inverse matrix(linalg.inv)
>>> linalg.inv(dot(a,b))
array([[ -7.74182569,  12.33267281],
       [  5.16705495,  -6.59937322]])

#Determinant(linalg.det)
>>> linalg.det(dot(a,b))
-0.079161515267537994

#eigenvalue(w), Eigenvector(v) linalg.eig
>>> w, v = linalg.eig(dot(a,b))
>>> w
array([-0.06590343,  1.20117446])
>>> v
array([[-0.85650205, -0.8210583 ],
       [ 0.51614363, -0.57084434]])

vector

>>> a = random.random(3);a
array([ 0.52541688,  0.12396039,  0.31534285])
>>> b = random.random(3);b
array([ 0.62372191,  0.00251833,  0.41029588])

#inner product
>>> inner(a,b)
0.45741006789576372

#Cross product(Cross product)
>>> cross(a,b)
array([ 0.0500663 , -0.01889014, -0.07599364])

#Cartesian product
>>> outer(a,b)
array([[  3.27714024e-01,   1.32317300e-03,   2.15576381e-01],
       [  7.73168097e-02,   3.12173138e-04,   5.08604357e-02],
       [  1.96686245e-01,   7.94137296e-04,   1.29383871e-01]])

Logical operation

Determines if the elements of the array meet the criteria.

>>> a = random.random(9).reshape(3,3);a
array([[ 0.07562832,  0.37995414,  0.80141031],
       [ 0.31946711,  0.54688344,  0.27916878],
       [ 0.64425661,  0.50601982,  0.55279281]])

#For each element a< 0.Make a judgment of 5.
>>> a < 0.5
array([[ True,  True, False],
       [ True, False,  True],
       [False, False, False]], dtype=bool)

#Element by element(0.2 < a) & (a < 0.4)Is judged.
>>> (0.2 < a) & (a < 0.4)
array([[False,  True, False],
       [ True, False,  True],
       [False, False, False]], dtype=bool)

# any():Is at least one true??
>>> a = [True,False]
>>> any(a)
True
>>> a = [False,False]
>>> any(a)
False

# all():Are all true?
>>> a = [True,False]
>>> all(a)
False
>>> a = [True,True]
>>> all(a)
True

Statistical calculation

>>> a = random.random(5);a
array([ 0.81006955,  0.54988884,  0.3000227 ,  0.68326733,  0.01710223])

#total
>>> a.sum()
2.3603506492918598
#average
>>> a.mean()
0.47207012985837193
#maximum
>>> a.max()
0.81006955003900494
#minimum
>>> a.min()
0.017102231221299058
#maximum-minimum
>>> a.ptp()
0.79296731881770588
#Standard deviation divisor is N
>>> a.std()
0.28337244927937266
#Standard deviation divisor is N-1
>>> a.std(ddof=1)
0.31682002976226714
#Distributed divisor is N
>>> a.var()
0.080299945006851087
#Array of maximum values Index
>>> a.argmax()
0
#Minimum value array Index
>>> a.argmin()
4
#Cumulative value
>>> a.cumsum()
array([ 0.81006955,  1.35995839,  1.65998109,  2.34324842,  2.36035065])

Performs statistical calculations along an axis for an N-dimensional array. It is an image to calculate by moving only in the specified axis direction. The axis is specified by the argument axis.

#3 rows 4 columns*Create an array of 2.
>>> a = random.random(24).reshape(2,3,4);a
array([[[ 0.70106649,  0.84600927,  0.96988224,  0.06242455],
        [ 0.90827331,  0.65320237,  0.41143149,  0.73883318],
        [ 0.03434408,  0.40834032,  0.16037719,  0.66273333]],

       [[ 0.33596355,  0.77672752,  0.26468854,  0.83723116],
        [ 0.9966073 ,  0.29452339,  0.26186954,  0.33732824],
        [ 0.76577634,  0.20663298,  0.33442575,  0.3477926 ]]])

#The sum is taken as an example, but other statistics can be calculated in the same way.
# axis=Sum along 0.
>>> a.sum(axis=0)
array([[ 1.03703004,  1.62273679,  1.23457077,  0.89965571],
       [ 1.90488061,  0.94772576,  0.67330104,  1.07616143],
       [ 0.80012042,  0.6149733 ,  0.49480294,  1.01052593]])

# axis=Sum along 1.
>>> a.sum(axis=1)
array([[ 1.64368387,  1.90755197,  1.54169092,  1.46399106],
       [ 2.09834719,  1.27788389,  0.86098384,  1.52235201]])

# axis=Sum along 2.
>>> a.sum(axis=2)
array([[ 2.57938255,  2.71174035,  1.26579492],
       [ 2.21461077,  1.89032848,  1.65462767]])

File

>>> a
array([[[ 0.69633633,  0.60467738,  0.46174455,  0.24528887],
        [ 0.03127559,  0.66646753,  0.93620592,  0.39435246],
        [ 0.59428005,  0.05191262,  0.94079899,  0.1217417 ]],

       [[ 0.6436789 ,  0.41509296,  0.38650686,  0.41877047],
        [ 0.87710361,  0.34266031,  0.58358085,  0.72265266],
        [ 0.57560748,  0.2277301 ,  0.12321634,  0.49292207]]])

#Save as a binary file.
>>> a.tofile("test.dat")

#Read from a binary file. Axis information such as rows and columns is gone.
>>> fromfile("test.dat")
array([ 0.69633633,  0.60467738,  0.46174455,  0.24528887,  0.03127559,
        0.66646753,  0.93620592,  0.39435246,  0.59428005,  0.05191262,
        0.94079899,  0.1217417 ,  0.6436789 ,  0.41509296,  0.38650686,
        0.41877047,  0.87710361,  0.34266031,  0.58358085,  0.72265266,
        0.57560748,  0.2277301 ,  0.12321634,  0.49292207])

#Save in npy format.
>>> save("test", a)

#Axis information also remains.
>>> load("test.npy")
array([[[ 0.69633633,  0.60467738,  0.46174455,  0.24528887],
        [ 0.03127559,  0.66646753,  0.93620592,  0.39435246],
        [ 0.59428005,  0.05191262,  0.94079899,  0.1217417 ]],

       [[ 0.6436789 ,  0.41509296,  0.38650686,  0.41877047],
        [ 0.87710361,  0.34266031,  0.58358085,  0.72265266],
        [ 0.57560748,  0.2277301 ,  0.12321634,  0.49292207]]])

#Save in npz format.
>>> savez("test", a=a)

#Browse the array with keywords.
>>> npz = load("test.npz")
>>> npz["a"]
array([[[ 0.69633633,  0.60467738,  0.46174455,  0.24528887],
        [ 0.03127559,  0.66646753,  0.93620592,  0.39435246],
        [ 0.59428005,  0.05191262,  0.94079899,  0.1217417 ]],

       [[ 0.6436789 ,  0.41509296,  0.38650686,  0.41877047],
        [ 0.87710361,  0.34266031,  0.58358085,  0.72265266],
        [ 0.57560748,  0.2277301 ,  0.12321634,  0.49292207]]])

#Compressed version of savez_There is also compressed. Same usage as savez.

#If you do not close it, the file will remain open.
>>> npz.close()

>>> x = random.random(6).reshape(2,3);x
array([[ 0.56246598,  0.19331716,  0.2826291 ],
       [ 0.81149981,  0.32680372,  0.90326888]])

#Savetxt is useful for 2D arrays. It's text so it's easy to read.
>>> savetxt("x.txt", x, delimiter="\t")
>>> loadtxt("x.txt")
array([[ 0.56246598,  0.19331716,  0.2826291 ],
       [ 0.81149981,  0.32680372,  0.90326888]])

#An error will occur if it is 3D.
>>> savetxt("a.txt", a) #An error will occur.

Recommended Posts

Use Numpy
Use OpenBLAS with numpy, scipy
numpy practice 1
Use Numpy, Scipy, scikit-learn on Heroku
numpy part 1
NumPy basics
Numpy Memorandum_Matrix
Use DeepLabCut
Use pycscope
Use collections.Counter
Use: Django-MySQL
Use Pygments.rb
NumPy axis
use pandas-ply
numpy part 2
Use GitPython
Use Miniconda
Use Numpy, Scipy, scikit-learn on Amazon Linux
Use multithreaded BLAS / LAPACK with numpy / scipy
My Numpy (Python)
Use Invariant TSC
where in numpy
[C] Use qsort ()
numpy unit test
NumPy array manipulation (3)
list and numpy
NumPy universal functions
numpy memorandum 1 / np.pad
#Python basics (#Numpy 1/2)
Use JIRA API
Use weak references
Use django-debug-toolbar non-locally
numpy index search
[Numpy] Shuffle ndarray
Use combinatorial optimization
Python #Numpy basics
numpy non-basic techniques
About Numpy broadcast
[PyTorch] Sample ① ~ NUMPY ~
python: Use your own class for numpy ndarray
Install Numpy + atlas
use go module
[Python] Numpy memo