Operating with Arrays using NumPy
Introduction
Now we are going to discover a library, which will be useful for more advanced usage of arrays, by providing a special data structure, called ndarray
- NumPy.
To import this library standard code is import numpy as np
.
Creating arrays
Let's assume that we have a standard list variable lst
. If we are going to create a one dimensional NumPy array called arr
, which will correspond to this variable, we should use the next code:
1234567import numpy as np # Creating list of integers called `lst` lst = [1, 2, 3] # Creating NumPy array `arr` from list `lst` arr = np.array(lst) print(arr) print(type(arr))
Now, if we want to create a new two dimensional array called arr
, we have to pass as argument to np.array
list of arrays/lists of the same length. For example:
1234import numpy as np # Creating array, in which the first row is [0, 1] and the second is [2, 3] arr = np.array([[0, 1], [2, 3]]) print(arr)
We also should notice such a fact, that in NumPy all elements/lists of elements should be of the same type (for example, all elements should be int
, or all elements should be float
, or all elements should be list
of int
, etc.)
Aggregation methods
Unlike ordinary lists, NumPy arrays also provide a set of aggregation methods. For example, the code below shows how to get minimum value from one-dimensional array a:
123456import numpy as np # Initializing array `a` a = np.array([0, 1, 2, 3, -1]) # Getting minimum value from `a` min_ = a.min() print(min_)
In case when we work with a multidimensional array, we should directly specify the axis, along which corresponding aggregation function will be taken. It could be done by setting the necessary value to the parameter axis
of the corresponding method.
For example, in case of two-dimensional array a, taking mean value for each column could be performed using next code:
123456import numpy as np # Initializing array `a` a = np.array([[1, 2, 3, 0], [4, 5, 6, 9]]) # Getting mean value for each column of `a` mean_ = a.mean(axis = 0) print(mean_)
Additional concepts
In order to see explanations and examples of some more advanced concepts - watch the video below:
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 7.14
Operating with Arrays using NumPy
Scorri per mostrare il menu
Introduction
Now we are going to discover a library, which will be useful for more advanced usage of arrays, by providing a special data structure, called ndarray
- NumPy.
To import this library standard code is import numpy as np
.
Creating arrays
Let's assume that we have a standard list variable lst
. If we are going to create a one dimensional NumPy array called arr
, which will correspond to this variable, we should use the next code:
1234567import numpy as np # Creating list of integers called `lst` lst = [1, 2, 3] # Creating NumPy array `arr` from list `lst` arr = np.array(lst) print(arr) print(type(arr))
Now, if we want to create a new two dimensional array called arr
, we have to pass as argument to np.array
list of arrays/lists of the same length. For example:
1234import numpy as np # Creating array, in which the first row is [0, 1] and the second is [2, 3] arr = np.array([[0, 1], [2, 3]]) print(arr)
We also should notice such a fact, that in NumPy all elements/lists of elements should be of the same type (for example, all elements should be int
, or all elements should be float
, or all elements should be list
of int
, etc.)
Aggregation methods
Unlike ordinary lists, NumPy arrays also provide a set of aggregation methods. For example, the code below shows how to get minimum value from one-dimensional array a:
123456import numpy as np # Initializing array `a` a = np.array([0, 1, 2, 3, -1]) # Getting minimum value from `a` min_ = a.min() print(min_)
In case when we work with a multidimensional array, we should directly specify the axis, along which corresponding aggregation function will be taken. It could be done by setting the necessary value to the parameter axis
of the corresponding method.
For example, in case of two-dimensional array a, taking mean value for each column could be performed using next code:
123456import numpy as np # Initializing array `a` a = np.array([[1, 2, 3, 0], [4, 5, 6, 9]]) # Getting mean value for each column of `a` mean_ = a.mean(axis = 0) print(mean_)
Additional concepts
In order to see explanations and examples of some more advanced concepts - watch the video below:
Grazie per i tuoi commenti!