My Coding > Programming language > Python > Python libraries and packages > Python NumPy

Python NumPy (Page: 1)

Go to Page:

  1. NumPy creating;
  2. NumPy array reshaping;
  3. Images with NumPy;
  4. NumPy copy;
  5. NumPy mask;
  6. Geometry;

NumPy initializing

NumPy is the fundamental package for array computing with Python.

In this section I will describe basic manipulations with NumPy array

Initialization of one-dimensional NumPy array

Initialize NumPy array with list

It is possible to convert list into NumPy array


tmp = [0,1,3,5,7,9] # temporary list
Na = np.array(tmp)  # list tmp converted to NP array Na
                    # [0 1 3 5 7 9]

Initialize NumPy array with arranged values

arrange() is almost identical to range(), but can work with float and integer values and it is designed t fill arrays with the range of values


a_a = np.arange(1,2,0.1)
print(a_a)   # [1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]

Initialization of two-dimensional NumPy array

It is possible to convert list of lists into NumPy array. Furthermore, you can initialize any dimensional array, but it is important to be careful about list sizes.


tmp = [[1,2,3],[4,5,6]] # list of lists
a_2D = np.array(tmp) # 2D array with preset values
print(a_2D)          # [[1 2 3] [4 5 6]] 

We can create pre-filled array

Zero filled NumPy array

zeros() - function to create array filled with zeroes


a_0 = np.zeros((2,3)) # 2 rows, 3 columns, filled with float(0).
print(a_0)            # [[0. 0. 0.] [0. 0. 0.]]

To fill this array with int(0) values, it is necessary to specify this type


a_0 = np.zeros((2,3), dtype = np.int32) # filled with int(0).
print(a_0)                              # [[0 0 0] [0 0 0]]

Ones filled NumPy array

ones() - function to create array filled with float(1). Use dtype = np.int32 to fill with integer values


a_1 = np.ones((4,2)) # 4 rows, 2 collumns, filled with 1.
print(a_1)           # [[1. 1.] [1. 1.] [1. 1.] [1. 1.]]

Randomply filled NumPy array

rand() - function to create array filled with random values on range [0., 1.). For example, wee need to create an array 5 rows with 3 column each, filled with random values in range from -3 to 10


a, b = -3, 10
ran = np.random.rand(5, 3) * (b - a) + a
print(ran)
#[[ 7.13105431 -2.9442257  -0.70409542]
# [ 7.4826382  -0.23493916  1.67161632]
# [ 8.96112949  8.94834147  6.57329131]
# [ 5.92371583  2.35650222  4.84267641]
# [ 5.44436381  6.43486693 -0.70091261]]

Non-initialized NumPy array

In fact, this array will be filled not with non-initialized value, but it will be filled with some random values. The function empty() works much faster than previous, but values can be any


a_e = np.empty((3,2)) # values are not set - initialized with some random values (much faster)
print(a_e)            # [[0.00000000e+000 6.93913844e-310]
                      #  [2.22809558e-312 2.14321575e-312]
                      #  [2.46151512e-312 2.41907520e-312]]

Identity matrix

eye() - interesting, why eye? Create identity matrix with specified size


a_I = np.eye(3) # Identity matrix size 3
print(a_I)      # [[1. 0. 0.]
                #  [0. 1. 0.]
                #  [0. 0. 1.]]

Creating structural array

Structural array in NumPy is kind of analogue of one level dictionary. It is proper array, but it is possible to refer to some data by names of these rows or columns, rather than by its numbers.


import numpy as np

e_name = ['John', 'Vasil', 'Marta', 'Smith']
e_ids  = [1, 2, 3, 4]
e_info = [34.56, 36.76, 11.65, 56.32]
# initialize NumPy array with zeros()

e_data = np.zeros(4, dtype = {'names' : ('Name', 'IDS', 'Info'),
                               'formats':('U16', 'i4', 'f8')}) 
                                # format - unicode(16), integer(4), float(8)
# initialize this array with data
e_data['Name'] = e_name
e_data['IDS']  = e_ids
e_data['Info'] = e_info
print(e_data) # [('John', 1, 34.56) ('Vasil', 2, 36.76) ('Marta', 3, 11.65) ('Smith', 4, 56.32)]

# It is possible to extract some data by name
print(e_data['Name'])                      # ['John' 'Vasil' 'Marta' 'Smith']

print(e_data[2])                           # ('Marta', 3, 11.65)

# it is possible to refer to data by name and by numbers
print(e_data[-1][0])                       # Smith
print(e_data[-1]['Name'])                  # Smith

# It is possble to use conditions
print(e_data[e_data['Info'] > 35]['Name']) # ['Vasil' 'Smith']

Go to Page: 1; 2; 3; 4; 5; 6;


Published: 2021-10-04 11:48:19
Updated: 2021-11-14 08:41:55

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.