Basic numpy learning and array and vector computing in python, pythonnumpy
Preface
In python, sometimes we use arrays to operate data, which can greatly improve data processing efficiency. Similar to R's vectorized operations, data operations tend to be simplified, in python, The numpy module can be used for Array and vector computing.
The following is a simple example.
Import numpy as np data = np. array ([2, 5, 6, 8, 3]) # construct a simple array print (data)
Result:
[2 5 6 8 3]
Data1 = np. array ([2, 5, 6, 8, 3], np. arange (5)]) # construct a two-dimensional array print (data1)
Result:
[[2 5 6 8 3][0 1 2 3 4]]
We can also use the shape and dtype methods to view the dimension and data format of the array.
print(data.shape)print(data.dtype)print(data1.shape)print(data1.dtype)
Result:
(5,)int32(2, 5)int32
We can see that data is a one-dimensional array with five elements in each group and the data type is 32-bit int.
Data1 is a two-dimensional array. Each group has five elements and the data type is 32-bit int.
There is a better way to distinguish between the layers and positions in the brackets in the printed results. We can see the dimension of the array. The brackets in one layer represent a dimension.
Other array attribute methods include:
array.ndimThe dimension of the array. The result of the one-dimensional array is 1, and the result of the two-dimensional array is 2.
array.sizeNumber of elements in the array
array.itemsizByte size of each element in the array
Next, let's take a look at the data types in the array:
Basic Data Types in NumPy
| Name |
Description |
| Bool |
Boolean Type stored in one byte (True or False) |
| Inti |
The integer (generally int32 or int64) determined by the platform) |
| Int8 |
Size of one byte,-128 to 127 |
| Int16 |
Integer,-32768 to 32767 |
| Int32 |
Integer,-2 ** 31 to 2 ** 32-1 |
| Int64 |
Integer,-2 ** 63 to 2 ** 63-1 |
| Uint8 |
Unsigned integer, 0 to 255 |
| Uint16 |
Unsigned integer, 0 to 65535 |
| Uint32 |
Unsigned integer, 0 to 2 ** 32-1 |
| Uint64 |
Unsigned integer, 0 to 2 ** 64-1 |
| Float16 |
Semi-precision floating point: 16 bits, plus/minus 1 bits, exponent 5 bits, accuracy 10 bits |
| Float32 |
Single-precision floating point: 32-bit, positive and negative 1-bit, exponent 8-bit, accuracy 23-bit |
| Float64 or float |
Double-precision floating point number: 64-bit, plus/minus 1-bit, exponential 11-bit, accuracy 52-bit |
| Complex64 |
The plural. Two 32-bit floating point numbers are used to represent the real and virtual parts. |
| Complex128 or complex |
The plural. Two 64-bit floating point numbers are used to represent the real and virtual parts respectively. |
Basic Array Operations
Arrays can also be used to perform common addition, subtraction, multiplication, division operations.
arr=np.array(np.arange(10))arr1=np.array(np.arange(1,11))print(arr*2)
Result:
[ 0 2 4 6 8 10 12 14 16 18]
print(arr+arr1)
Result:
[ 1 3 5 7 9 11 13 15 17 19]
Note:, Add two arrays with the same length
Next, let's take a look at the array index.
arr=np.arange(10)
Index directly with subscript
print(arr[5])
Result:
5
Slice Index
print(arr[5:8])
Result:
[5 6 7]
You can use indexes to change data.
arr[5]=120print(arr)
Result:
[ 0 1 2 3 4 120 6 7 8 9]
We can see that the number of subscript 5 has changed to 120.
In addition, Arrays can also perform Boolean operations.
arr=np.arange(5)name=np.array(['a','b','b','c','a'])print(name=='a')
Result:
[ True False False False True]
That is, all data that meets the condition is output as True.
Next, we can use the Boolean value after the name array sets the condition to perform related operations on the arr array.
print(arr[name=='a'])
Result:
[0 4]
That is, the elements in arr corresponding to the location of a in name are printed out.
Multi-condition operation
result=(name='a')|(name='c')print(result)print(name[result])
Result:
[ True False False True True]['a' 'c' 'a']
Next, let's take a look at the ufunc method.
Functions used to operate a single array are as follows:
Method used to operate two or more Arrays
Use of Related Function Methods
np.meshgridUsed to generate a multidimensional matrix
a,b=np.meshgrid(np.arange(1,5),np.arange(2,4))print(a)print(b)
Result:
[[1 2 3 4][1 2 3 4]][[2 2 2 2][3 3 3 3]]
Arrays are formed based on arrays with the least Data
np.whereReturns the vectorized version of the ternary expression x if condition else y.
arr1=np.arange(5)arr2=np.arange(20,25)condition=np.array([1,0,1,0,0])result=np.where(condition,arr1,arr2)print(arr1)print(arr2)print(result)
Result:
[0 1 2 3 4][20 21 22 23 24][ 0 21 2 23 24]
As you can see, in the result, the content of the array arr1 is displayed with condition 1, and the content of arr2 is displayed with condition 0.
Mathematical Statistics
In the array, we can also use mathematical statistics to count, such as sum mean std.
arr=np.random.randint(1,20,10)print(arr)print(np.mean(arr))print(np.sum(arr))print(np.std(arr))
Result:
[19 14 8 13 13 10 10 9 19 7]12.21224.01995024845
The specific method content is shown in:
Related Statistical Methods for Boolean Arrays
arr=np.arange(-20,10)result=(arr>5).sum()print(arr)print(result)
Result:
-20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3-2 -1 0 1 2 3 4 5 6 7 8 9]4
You can determine the data and then sum the number.
Other array methods include
Data Reading and storage
Common linear functions
arr=np.array([np.random.randint(1,10,5),np.random.randint(10,20,5)])print(arr)print(np.dot(arr,2))
Result:
[[ 4 6 5 1 6][14 16 11 10 18]][[ 8 12 10 2 12][28 32 22 20 36]]
The dot method can be used to multiply a matrix.
Other methods are as follows:
Finally, let's take a look at the random number generation method in numpy.
In many examples above, we have used random number generation,
arr=np.random.random(10)print(arr)
Result:
[ 0.90051063 0.72818635 0.00411373 0.13154345 0.45513344 0.97007760.42150977 0.27728599 0.50888291 0.62288808]
Other random number generation methods
Summary
Well, the above is all the content of this article. After learning about the numpy operation method above, the basic data operation problems should not be very large. I hope the content of this article will help you in your study or work.