Numpy 4. Array creation

catalogue

Array creation

1. Create ndarray based on existing data

(a) It is created through the array() function.

(b) It is created through the asarray() function

(c) It is created through the fromfunction() function

2. Fill according to ones and zeros

(a) Zero array

(b) 1 array

(c) Empty array

(d) Unit array

(e) Diagonal array

(f) Constant array

3. Create an ndarray using a numeric range

4. Creation of structure array

(a) Using dictionaries to define structures

(b) The structure is defined using a list containing multiple tuples

Array properties

Array creation

Import numpy.

import numpy as np

The most important data structure provided by numpy is ndarray, which is an extension of list in python.

1. Create ndarray based on existing data

(a) It is created through the array() function.

def array(p_object, dtype=None, copy=True, order='K', subok=False, ndmin=0): 

[example]

import numpy as np

# Create a one-dimensional array
a = np.array([0, 1, 2, 3, 4])
b = np.array((0, 1, 2, 3, 4))
print(a, type(a))
# [0 1 2 3 4] <class 'numpy.ndarray'>
print(b, type(b))
# [0 1 2 3 4] <class 'numpy.ndarray'>

# Create a 2D array
c = np.array([[11, 12, 13, 14, 15],
              [16, 17, 18, 19, 20],
              [21, 22, 23, 24, 25],
              [26, 27, 28, 29, 30],
              [31, 32, 33, 34, 35]])
print(c, type(c))
# [[11 12 13 14 15]
#  [16 17 18 19 20]
#  [21 22 23 24 25]
#  [26 27 28 29 30]
#  [31 32 33 34 35]] <class 'numpy.ndarray'>

# Create 3D array
d = np.array([[(1.5, 2, 3), (4, 5, 6)],
              [(3, 2, 1), (4, 5, 6)]])
print(d, type(d))
# [[[1.5 2.  3. ]
#   [4.  5.  6. ]]
#
#  [[3.  2.  1. ]
#   [4.  5.  6. ]]] <class 'numpy.ndarray'>

(b) It is created through the asarray() function

Both array() and asarray() can convert structural data into ndarray, but the main difference between array() and asarray() is that when the data source is ndarray , array() will still copy a copy and occupy new memory, but , asarray() will not change dtype.

def asarray(a, dtype=None, order=None):
    return array(a, dtype, copy=False, order=order)

[example] both array() and asarray() can convert structural data into ndarray

import numpy as np

x = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
y = np.array(x)
z = np.asarray(x)
x[1][2] = 2
print(x,type(x))
# [[1, 1, 1], [1, 1, 2], [1, 1, 1]] <class 'list'>

print(y,type(y))
# [[1 1 1]
#  [1 1 1]
#  [1 1 1]] <class 'numpy.ndarray'>

print(z,type(z))
# [[1 1 1]
#  [1 1 1]
#  [1 1 1]] <class 'numpy.ndarray'>

[example] the difference between array() and asarray(). (the main difference between array () and asarray () is that when the data source is ndarray (), array () will still copy a copy and occupy new memory, but asarray () will not change dtype.)

import numpy as np

x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
y = np.array(x)
z = np.asarray(x)
w = np.asarray(x, dtype=np.int)
x[1][2] = 2
print(x,type(x),x.dtype)
# [[1 1 1]
#  [1 1 2]
#  [1 1 1]] <class 'numpy.ndarray'> int32

print(y,type(y),y.dtype)
# [[1 1 1]
#  [1 1 1]
#  [1 1 1]] <class 'numpy.ndarray'> int32

print(z,type(z),z.dtype)
# [[1 1 1]
#  [1 1 2]
#  [1 1 1]] <class 'numpy.ndarray'> int32

print(w,type(w),w.dtype)
# [[1 1 1]
#  [1 1 2]
#  [1 1 1]] <class 'numpy.ndarray'> int32

[example] when changing to a larger dtype, its size must be the divisor of the total size (in bytes) of the last axis of the array

import numpy as np

x = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
print(x, x.dtype)
# [[1 1 1]
#  [1 1 1]
#  [1 1 1]] int32
x.dtype = np.float

# ValueError: When changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.

(c) It is created through the fromfunction() function

When drawing a function, you may use fromfunction(), which can create an array from the function.

def fromfunction(function, shape, **kwargs):

[example] construct an array by executing a function on each coordinate.

import numpy as np

def f(x, y):
    return 10 * x + y

x = np.fromfunction(f, (5, 4), dtype=int)
print(x)
# [[ 0  1  2  3]
#  [10 11 12 13]
#  [20 21 22 23]
#  [30 31 32 33]
#  [40 41 42 43]]

x = np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
print(x)
# [[ True False False]
#  [False  True False]
#  [False False  True]]

x = np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int)
print(x)
# [[0 1 2]
#  [1 2 3]
#  [2 3 4]]

2. Fill according to ones and zeros

One of the things we often do in machine learning tasks is to initialize parameters. We need to create a fixed size matrix with constant or random values.

(a) Zero array

  • zeros() function: returns an array of zeros for a given shape and type.
  • zeros_like() function: returns an array of zeros with the same shape and type as the given array.
def zeros(shape, dtype=None, order='C'):
def zeros_like(a, dtype=None, order='K', subok=True, shape=None):

[example]

import numpy as np

x = np.zeros(5)
print(x)  # [0. 0. 0. 0. 0.]
x = np.zeros([2, 3])
print(x)
# [[0. 0. 0.]
#  [0. 0. 0.]]

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.zeros_like(x)
print(y)
# [[0 0 0]
#  [0 0 0]]

(b) 1 array

  • The ones() function returns an array of 1 for the given shape and type.
  • ones_like() function: returns an array of 1 the same shape and type as the given array.
def ones(shape, dtype=None, order='C'):
def ones_like(a, dtype=None, order='K', subok=True, shape=None):

[example]

import numpy as np

x = np.ones(5)
print(x)  # [1. 1. 1. 1. 1.]
x = np.ones([2, 3])
print(x)
# [[1. 1. 1.]
#  [1. 1. 1.]]

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.ones_like(x)
print(y)
# [[1 1 1]
#  [1 1 1]]

(c) Empty array

  • empty() function: returns an empty array whose elements are random numbers.
  • empty_like function: returns a new array with the same shape and type as the given array.
def empty(shape, dtype=None, order='C'): 
def empty_like(prototype, dtype=None, order='K', subok=True, shape=None):

[example]

import numpy as np

x = np.empty(5)
print(x)
# [1.95821574e-306 1.60219035e-306 1.37961506e-306 
#  9.34609790e-307 1.24610383e-306]

x = np.empty((3, 2))
print(x)
# [[1.60220393e-306 9.34587382e-307]
#  [8.45599367e-307 7.56598449e-307]
#  [1.33509389e-306 3.59412896e-317]]

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.empty_like(x)
print(y)
# [[  7209029   6422625   6619244]
#  [      100 707539280       504]]

(d) Unit array

  • eye() function: returns an array of units with 1 on the diagonal and zero elsewhere.
  • identity() function: returns an array of units of one side.
def eye(N, M=None, k=0, dtype=float, order='C'):
def identity(n, dtype=None):

[example]

import numpy as np

x = np.eye(4)
print(x)
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]

x = np.eye(2, 3)
print(x)
# [[1. 0. 0.]
#  [0. 1. 0.]]

x = np.identity(4)
print(x)
# [[1. 0. 0. 0.]
#  [0. 1. 0. 0.]
#  [0. 0. 1. 0.]
#  [0. 0. 0. 1.]]

(e) Diagonal array

  • diag() function: extract diagonal lines or construct diagonal arrays.
def diag(v, k=0):

[example]

import numpy as np

x = np.arange(9).reshape((3, 3))
print(x)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]
print(np.diag(x))  # [0 4 8]
print(np.diag(x, k=1))  # [1 5]
print(np.diag(x, k=-1))  # [3 7]

v = [1, 3, 5, 7]
x = np.diag(v)
print(x)
# [[1 0 0 0]
#  [0 3 0 0]
#  [0 0 5 0]
#  [0 0 0 7]]

(f) Constant array

  • full() function: returns an array of constants.
  • full_like() function: returns an array of constants with the same shape and type as the given array.
def full(shape, fill_value, dtype=None, order='C'):
def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None):

[example]

import numpy as np

x = np.full((2,), 7)
print(x)
# [7 7]

x = np.full(2, 7)
print(x)
# [7 7]

x = np.full((2, 7), 7)
print(x)
# [[7 7 7 7 7 7 7]
#  [7 7 7 7 7 7 7]]

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.full_like(x, 7)
print(y)
# [[7 7 7]
#  [7 7 7]]

3. Create an ndarray using a numeric range

  • arange() function: returns the value of uniform interval within a given interval.
  • linspace() function: returns equally spaced numbers within a specified interval.
  • logspace() function: returns the number of logarithmic scales evenly distributed.
  • numpy.random.rand() returns an array of random numbers in [0,1].
def arange([start,] stop[, step,], dtype=None): 
def linspace(start, stop, num=50, endpoint=True, retstep=False, 
             dtype=None, axis=0):
def logspace(start, stop, num=50, endpoint=True, base=10.0, 
             dtype=None, axis=0):
def rand(d0, d1, ..., dn): 

[example]

import numpy as np

x = np.arange(5)
print(x)  # [0 1 2 3 4]

x = np.arange(3, 7, 2)
print(x)  # [3 5]

x = np.linspace(start=0, stop=2, num=9)
print(x)  
# [0.   0.25 0.5  0.75 1.   1.25 1.5  1.75 2.  ]

x = np.logspace(0, 1, 5)
print(np.around(x, 2))
# [ 1.    1.78  3.16  5.62 10.  ]            
                                    #np.around returns the value after rounding, and the precision can be specified.
                                   # around(a, decimals=0, out=None)
                                   # a input array
                                   # decimals the number of decimal places to round. The default value is 0. If negative, the integer is rounded to the left of the decimal point


x = np.linspace(start=0, stop=1, num=5)
x = [10 ** i for i in x]
print(np.around(x, 2))
# [ 1.    1.78  3.16  5.62 10.  ]

x = np.random.random(5)
print(x)
# [0.41768753 0.16315577 0.80167915 0.99690199 0.11812291]

x = np.random.random([2, 3])
print(x)
# [[0.41151858 0.93785153 0.57031309]
#  [0.13482333 0.20583516 0.45429181]]

4. Creation of structure array

Structure array, first define the structure, and then use NP Array() to create an array whose parameter dtype is the defined structure.

(a) Using dictionaries to define structures

[example]

import numpy as np

personType = np.dtype({
    'names': ['name', 'age', 'weight'],
    'formats': ['U30', 'i8', 'f8']})

a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],
             dtype=personType)
print(a, type(a))
# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]
# <class 'numpy.ndarray'>

(b) The structure is defined using a list containing multiple tuples

[example]

import numpy as np

personType = np.dtype([('name', 'U30'), ('age', 'i8'), ('weight', 'f8')])
a = np.array([('Liming', 24, 63.9), ('Mike', 15, 67.), ('Jan', 34, 45.8)],
             dtype=personType)
print(a, type(a))
# [('Liming', 24, 63.9) ('Mike', 15, 67. ) ('Jan', 34, 45.8)]
# <class 'numpy.ndarray'>

# The value of structure array is similar to that of general array, and the elements can be obtained through subscript:
print(a[0])
# ('Liming', 24, 63.9)

print(a[-2:])
# [('Mike', 15, 67. ) ('Jan', 34, 45.8)]

# We can use the field name as a subscript to get the corresponding value
print(a['name'])
# ['Liming' 'Mike' 'Jan']
print(a['age'])
# [24 15 34]
print(a['weight'])
# [63.9 67.  45.8]

Array properties

When using numpy, you will want to know some information about the array. Fortunately, this package contains many convenient methods to give you the information you want.

  • numpy.ndarray.ndim is used to return the dimension (number of axes) of the array, also known as rank. The rank of one-dimensional array is 1, the rank of two-dimensional array is 2, and so on.
  • numpy.ndarray.shape represents the dimension of the array and returns a tuple. The length of this tuple is the number of dimensions, that is, the {ndim} attribute (rank).
  • numpy. ndarray. The total amount of all elements in the size array is equivalent to the product of all elements in the shape of the array. For example, the total amount of elements in the matrix is the product of rows and columns.
  • numpy. ndarray. The element type of the dtype {ndarray} object.
  • numpy.ndarray.itemsize returns the size of each element in the array in bytes.
class ndarray(object):
    shape = property(lambda self: object(), lambda self, v: None, lambda self: None)
    dtype = property(lambda self: object(), lambda self, v: None, lambda self: None)
    size = property(lambda self: object(), lambda self, v: None, lambda self: None)
    ndim = property(lambda self: object(), lambda self, v: None, lambda self: None)
    itemsize = property(lambda self: object(), lambda self, v: None, lambda self: None)

[example]

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a.shape)  # (5,)
print(a.dtype)  # int32
print(a.size)  # 5
print(a.ndim)  # 1
print(a.itemsize)  # 4

b = np.array([[1, 2, 3], [4, 5, 6.0]])
print(b.shape)  # (2, 3)
print(b.dtype)  # float64
print(b.size)  # 6
print(b.ndim)  # 2
print(b.itemsize)  # 8

In ndarray, all elements must be of the same type, otherwise they will be automatically converted downward, int - > float - > str.

[example]

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)  # [1 2 3 4 5]
b = np.array([1, 2, 3, 4, '5'])
print(b)  # ['1' '2' '3' '4' '5']
c = np.array([1, 2, 3, 4, 5.0])
print(c)  # [1. 2. 3. 4. 5.]

Keywords: Python Data Analysis

Added by bobbuilder on Mon, 17 Jan 2022 01:05:53 +0200