ch1 numpy_array的操作和运算

本文详细介绍了numpy库中array的操作,包括创建不同类型的数组、访问元素、切片以及矩阵运算,如加减乘除、开方、求和、求平均数等。还涉及到矩阵转置、指数运算和绘图等高级功能。
摘要由CSDN通过智能技术生成

chap1_warmup

1. numpy的 array操作

  1. 导入numpy库
import numpy as np

# 应注意文件名不能成为numpy.py,否则会AttributeError: module 'numpy' has no attribute 'array'
  1. 建立一个一维数组 a 初始化为[4,5,6], (1)输出a 的类型(type)(2)输出a的各维度的大小(shape)(3)输出 a的第一个元素(值为4)
a = np.array([4, 5, 6])
size_a = np.shape(a)
print('type:', type(a), '\nsize_a:', size_a, '\nfirst a:', a[0])

  1. 建立一个二维数组 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]])

size_b = np.shape(b)
print('\nsize_b:', size_b, '\n(0,1):', b[0][0],'\n(0,1):', b[0][1],'\n(0,1):', b[1][1])

  1. (1)建立一个全0矩阵 a, 大小为 3x3; 类型为整型(提示: dtype = int)(2)建立一个全1矩阵b,大小为4x5; (3)建立一个单位矩阵c ,大小为4x4; (4)生成一个随机数矩阵d,大小为 3x2.
a = np.zeros((3, 3), dtype='i4')	# i4 表示int32
b = np.ones((4, 5), dtype='i8')		# i8 表示int64
c = np.eye(4, dtype='i4')
rd = np.random.RandomState(888)		# 产生随机数种子
d = rd.randint(-2, 4, (3, 2))		# 再[-2, 4]之间产生3*2的随机矩阵
print(a, '\n', b, '\n', c, '\n', d)
  1. 建立一个数组 a,(值为[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] ) ,(1)打印a; (2)输出 下标为(2,3),(0,0) 这两个数组元素的值
a = np.array([[1
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值