import numpy as np
t1 = np.arange(12).reshape(3, 4).astype("float")
t1[1, 2:] = np.nan
print(t1)
def fill_nan(t1):
for i in range(t1.shape[1]):
temp_col = t1[:, i]
nan_num = np.count_nonzero(temp_col != temp_col)
if nan_num > 0:
temp_not_nan_col = temp_col[temp_col == temp_col]
temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
return t1
print(fill_nan(t1))
t2 = np.arange(12).reshape(3, 4).astype("float")
t3 = np.vstack((t1, t2))
print(t3)
t4 = np.hstack((t1, t2))
print(t4)
t1[[1, 2], :] = t1[[2, 1], :]
print(t1)
t1[:, [1, 2]] = t1[:, [2, 1]]
print(t1)
t5 = np.ones((2, 3))
print(t5)
t6 = np.zeros((3, 4))
print(t6)
t7 = np.eye(4)
print(t7)
t8=np.random.rand(3,4)
print(t8)
np.random.seed(3)
a = np.random.rand(3)
print(a)
np.random.seed(3)
b = np.random.rand(3)
print(b)
|