This is the 24th day of my participation in the August More Text Challenge
1. Type conversion
Different types of values can perform different operations, so cast the raw data. In numpy arrays, the conversion method is astype(), which specifies the target type to be converted in parentheses after astype.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
Copy the code
result:
[1 2 3] [4 5 6] [7 8 9]]Copy the code
print(arr.dtype)
Copy the code
result:
int32
Copy the code
Convert an ARR array from int to float
print(arr.astype(np.float64))
Copy the code
result:
[[1. 2. 3.] [4. 5. 6.] [7.Copy the code
The astype() methods here and in the Pandas series are essentially the same, although they belong to two different libraries.
2. Handle the missing value
Missing values are represented by Np.nan in numpy
Missing value processing is divided into 2 steps. The first step is to judge whether there are missing values and find out the missing values. The second step is to fill in the missing values
An array of missing values is given before processing the missing values
import numpy as np
arr = np.array([1, 2, np.nan, 4])
print(arr)
Copy the code
result:
[ 1. 2. nan 4.]
Copy the code
2.1 Searching for missing Values
The method used to find missing values is isnan(). If a location returns True, it is a missing value.
print(np.isnan(arr))
Copy the code
result:
[False False True False]
Copy the code
2.2 Handling missing Values
arr[np.isnan(arr)] = 0
print(arr)
Copy the code
result:
/ 1. 2. 0. 4.Copy the code
3. Repeat value processing
Start by creating an array of duplicate values
import numpy as np
arr = np.array([1, 2, 3, 2, 1])
print(arr)
Copy the code
result:
[1 2 3 2 1]
Copy the code
print(np.unique(arr))
Copy the code
result:
[1, 2, 3]Copy the code