numpy 练习题
此文章仅为本人学习笔记,侵权删
题目来自于 https://github.com/nndl/nndl.github.io
numpy 的array操作
1.导入numpy库
import numpy as np
2.建立一个一维数组 a 初始化为[4,5,6], (1)输出a 的类型(type)(2)输出a的各维度的大小(shape)(3)输出 a的第一个元素(值为4)
a = np.array([4, 5, 6])
print(a.dtype)
print(a.shape)
print(a[0])
int64
(3,)
4
3.建立一个二维数组 b,初始化为 [ [4, 5, 6],[1, 2, 3]] (1)输出各维度的大小(shape)(2)输出 b(0,0),b(0,1),b(1,1) 这三个元素(对应值分别为4,5,2)
b = np.array( [[4, 5, 6],
[1, 2, 3]])
print(b.shape)
print(b[0][0], b[0][1], b[1][1])
(2, 3)
4 5 2
4. (1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型(提示: dtype = int)(2)建立一个全1矩阵b,大小为4x5; (3)建立一个单位矩阵c ,大小为4x4; (4)生成一个随机数矩阵d,大小为 3x2.
a = np.zeros((3, 3), dtype = int)
b = np.ones((4, 5))
c = np.identity((4))
d = np.random.randint(1, 10, size=(3, 2))
5. 建立一个数组 a,(值为[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ) ,(1)打印a; (2)输出 下标为(2,3),(0,0) 这两个数组元素的值
a = np.array([[1