1.Numpy Numpy: Useful for working with multidimensional arrays in Python extension modules
I always have less memory than people, is that? How about this? In the state Every time I fall into it, I search with the same search word, so I will make a note of it on this occasion.
The environment uses the environment created in the previous article. → Preparing for python development on Windows 10
2.list to Numpy
How to convert from a python list to a Numpy array
import numpy as np
a = np.array([0, 1, 2, 3, 4, 5]) #Create an array of NumPy from a Python list
print(a)
Execution result
[0 1 2 3 4 5]
<class 'numpy.ndarray'>
print(type(a))
Execution result
<class 'numpy.ndarray'>
import numpy as np
b = np.array([[0, 1, 2], [3, 4, 5]]) #Create a two-dimensional array of NumPy from a double list
print(b)
Execution result
[[0 1 2]
[3 4 5]]
print(b.shape) #Get the shape as a tuple with shape (number of rows, number of columns)
Execution result
(2, 3)
import numpy as np
a = np.array([[0, 1, 2], [3, 4, 5]]) #A two-dimensional array
print(a)
#[[0 1 2]
# [3 4 5]]
print(a + 10) #Add 10 to each element
# [[10 11 12]
# [13 14 15]]
print(a * 10) #Multiply each element by 10
#[[ 0 10 20]
# [30 40 50]]
Arithmetic between arrays
b = np.array([[0, 1, 2], [3, 4, 5]]) #A two-dimensional array
c = np.array([[2, 0, 1], [5, 3, 4]]) #A two-dimensional array
print(b)
print("--------------")
print(c)
print("--------------")
print(b + c)
print("--------------")
print(b * c)
Execution result
[[0 1 2]
[3 4 5]]
--------------
[[2 0 1]
[5 3 4]]
--------------
[[2 1 3]
[8 7 9]]
--------------
[[ 0 0 2]
[15 12 20]]
Recommended Posts