[Numpy Summary] Section 3 Numpy creates an array

Hits: 0

Article directory

First, the creation of standard [arrays]

1.1 [numpy] .empty creates an empty array

Used to create an uninitialized array with a specified shape ( [shape] ), data type (dtype); the data in the array is random because it is not initialized;

numpy.empty(shape, dtype = float, order = 'C')

parameter describe
shape array shape
dtype data type, optional
order There are two options “C” and “F”, which represent, respectively, row-major and column-major, the order in which elements are stored in computer memory.
Generally do not need to pay attention

Example:

a = np.empty((4,3),dtype=int)
print(a)    # Each time the output is different because there is no initialization 
# [[-958363344 464 -958381568] 
# [ 464 -958387104 464] 
# [-958380912 464 -958380224] 
# [ 464 -958380224 464]]

1.2 numpy.zeros creates an array of zeros

Used to create an array of a specified shape (shape), and all initialized to 0
Example:

a = np.ones((4,3))
print(a)  
# [[1 1 1]
# [1 1 1]
# [1 1 1]
# [1 1 1]]

1.3 numpy.ones creates an array of 1

Used to create an array of the specified shape (shape), and all initialized to 1
Example:

a = np.zeros((4,3))
print(a)
# [[0 0 0]
#  [0 0 0]
#  [0 0 0]
#  [0 0 0]]

2. Create a general array

2.0 Use list to create an array numpy.array

The format is: numpy.array(object, dtype=None), where:

parameter describe
object An object of the created array, which can be a single value, list, cell, etc.
dtype data type, optional

Example:

array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(array)   # [0 1 2 3 4 5 6 7 8 9]
print(array.dtype)   # int32

2.1 Use list to create an array numpy.asarray

The format is: numpy.asarray(a, dtype = None, order = None), where:

parameter describe
a Input arguments of any form, can be: list, tuple of lists, tuple, tuple of tuples, list of tuples, multidimensional array
dtype data type, optional
order Optional, there are two options “C” and “F”, which represent, respectively, row-major and column-major, the order in which elements are stored in computer memory.

Example:

a = [1,3,1,5,4]
b = np.asarray(a)
print(type(a))   # <class 'list'>
print(type(b))   # <class 'numpy.ndarray'>

2.2 Using iterable objects to create arrays numpy.fromiter

The format is: numpy.fromiter(iterable, dtype, count=-1), where:

parameter describe
iterable iterable object
dtype The data type of the returned array
count The amount of data to read, the default is – 1, read all data

Example:

list=range(5)
it=iter(list)
x=np.fromiter(it, dtype=float)
print(x)

2.3 Create an array numpy.arange using a range of values

The format is: numpy.arange(start, stop, step, dtype), where:

parameter describe
start starting value, default is 0
stop Termination value (exclusive)
step step size, default is 1
dtype Returns the data type of the ndarray, or if not provided, the type of the input data is used.

Example:

a = np.arange(10)
print(a)   # output: [0 1 2 3 4 5 6 7 8 9]

b = np.arange(10,20,2)
print(b)   # output: [10 12 14 16 18]

2.4 Create arrays using numerical ranges numpy.linspace

The format is: np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None), where:

parameter describe
start the starting value of the sequence
stop The end value of the sequence, if endpoint is true, the value is included in the sequence
num Number of samples of equal stride to generate, defaults to 50
endpoint When the value is true, the stop value is included in the sequence, otherwise it is not included, the default is True.
court step If True, spacing will be displayed in the resulting array, otherwise it will not be displayed.
dtype data type of ndarray

Example:

a = np.linspace(10,20,5,endpoint=False)   # Starting at 10, ending at 20, a total of 5 numbers are generated, excluding 20 
print(a)    # Output: [10. 12. 14. 16. 18 .]
b = np.linspace(10,20,5,endpoint=True)
print(b)     # output: [10. 12.5 15. 17.5 20. ]

3. Create a random array

3.1 Create a random array of integers: np.random.randint

The format is:np.random.randint(0, 100, (3, 4))

Before using random, the random seed can np.random.seed(666)be set by , which is consistent with Python;

Example:

a = np.random.randint(0, 100, (3, 4))
print(a)
# The output is: 
# [[92 58 18 32] 
# [ 4 87 81 1] 
# [12 11 13 68]]

3.2 Create a random array of floating point type

As long as you divide the integer by the integer, for example, you need to create a floating-point array with a value range between 0 and 1 and a precision of 0.01, you can use the following method:

a = np.random.randint(0, 100, (3, 4))
b= a/100
print(b)
# The output is: 
# [[0.05 0.48 0.72 0.95] 
# [0.68 0.78 0.22 0.98] 
# [0.17 0.45 0.7 0.85]]

You may also like...

Leave a Reply

Your email address will not be published.