Numpy中的一些简单方法

import numpy as np

# 创建一个数组,类型为<class 'numpy.ndarray'>, array.reshape()

arr_1D = np.arange(12) # 生成一个一维的 ndarray
print("一维:","\n",arr_1D)
arr_2D = arr_1D.reshape((4, 3))  # reshape后, 转换成一个二维的 ndarray
print("二维:","\n",arr_2D)
arr_3D = arr_1D.reshape(2,3,2)   # reshape后, 转换成一个三维的 ndarray,这个(2,3,2)的含义还没搞懂!!
print("三维:","\n",arr_3D)

# 转置transpose, array.T
arr_transpose = arr_2D.T
print("转置后:", "\n", arr_transpose)

# 矩阵点积, np.dot()
arr_dot_product = np.dot(arr_2D, arr_transpose)
print("矩阵的点积为:", "\n", arr_dot_product)

# 查询矩阵信息
print(f"Shape of arr_3D: {arr_3D.shape}")
print(f"Size of arr_3D: {arr_3D.size}")
print(f"Type of arr_3D: {arr_3D.dtype}")

# 创建一个内部等距的矩阵, np.linspace()
arr3 = np.linspace(2,10,6) #间隔即为:(10-2)/5
print(arr3)
arr4 = arr3.reshape(2,-1) # -1为占位,numpy可以推测后面的值
print(arr4)

arr_eye1 = np.eye(3)
print("eye(3):" ,"\n")
print(arr_eye1,"\n"*2)

arr_identity1 = np.identity(3)
print("identity(3):" ,"\n")
print(arr_identity1,"\n"*2)

arr_eye2 = np.eye(5)
print("eye(5):" ,"\n")
print(arr_eye2,"\n"*2)

arr_identity2 = np.identity(5)
print("identity(5):" ,"\n")
print(arr_identity2,"\n"*2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值