import numpy as np
# 1.创建一个长度为10的ndarry对象,然后让第三个元素等于1
arr1 = np.zeros(10)
arr1[2] = 1
print(arr1)
# 2.创建10到49的ndarry对象
arr2 = np.arange(10, 50)
print(arr2)
# 3.反转arr2
arr3 = arr2[::-1]
print(arr3)
# 4.使用np.random.random
arr4 = np.random.random((10, 10))
print(arr4)
print(arr4.min())
# 5.(10,10) ndarry 内部全为0 外部全为1
arr5 = np.zeros(100).reshape(10, 10)
# arr5[0] = arr5[-1] = arr5[:, 0] = arr5[:, -1] = 1
arr5[[0, -1]] = 1
arr5[:, [0, -1]] = 1
print(arr5)
# 6.每一行都是0,4的5*5矩阵
arr6 = np.arange(5).reshape(1, 5)
print(arr6.repeat(5, axis=0))
# 7.0,1之间长度为12等差数列
arr7 = np.linspace(0, 1, num=12)
print(arr7)
# 8.长度为10随机数组并排序
arr8 = np.random.randint(1, 100, size=10)
print(arr8)
arr8.sort()
print(arr8)
# 9.创建符合类型
student = np.dtype(
[('name', '<U20'), ('id', 'i4'), ('age', 'i1')]
)
arr9 = np.array([('tom', 1, 18), ('lily', 2, 22)], dtype=student)
print(arr9)
NumPy作业(1)
1140 views
目录: