主要内容
Talk is cheap show me the code. —— Linus Torvalds
'''
本代码用于展示Ndarray的5种重要属性
20230925 by weidada
'''
import numpy as np
# 创建一个Ndarray对象
nd = np.array([[1, 2], [3, 4]])
print('nd的类型为:', type(nd))
print('1. nd的维度:',nd.ndim)
print('2. nd的行列数:',nd.shape)
print('3. nd的元素个数:',nd.size)
print('4. nd的元素大小:',nd.itemsize)
print('5. nd的元素类型:',nd.dtype)
'''
代码输出:
nd的类型为:
1. nd的维度: 2
2. nd的行列数: (2, 2)
3. nd的元素个数: 4
4. nd的元素大小: 4
5. nd的元素类型: int32
'''
import numpy as np
nd1 = np.array([1, 2, 3, 4], dtype=np.float64)
print('nd1:', nd1)
print('nd1元素类型:', nd1.dtype)
# zeros/ones/empty
nd2 = np.zeros(shape=(2, 3), dtype=np.float64)
print('nd2:', nd2)
nd3 = np.ones(shape=(3, 4), dtype=np.int32)
print('nd3:', nd3)
# 创建一个三行四列的数组,并且不对内存中的数据做任何初始化操作
nd4 = np.empty(shape=(3, 4))
print('nd4:', nd4)
# mat/matrix
nd5 = np.mat('1,2;3,4')
print('nd5:', nd5)
nd5_2 = np.mat('1 2;3 4')
print('nd5_2:', nd5_2)
nd6 = np.matrix([[1, 2], [3, 4]], dtype=np.float64)
print('nd6:', nd6)
# random
# 创建一个三行四列的随机数组,数组的元素位于[0,1)区间中
nd7 = np.random.rand(3, 4)
print('nd7:', nd7)
# 从[1,10)区间中取出随机整数创建一个两行散列的数组
nd7_2 = np.random.randint(1, 10, size=(2, 3))
print('nd7_2:', nd7_2)
# 从[-3,3)区间中取出实数,创建一个三行四列的数组
nd7_3 = np.random.uniform(-3, 3, size=(3, 4))
print('nd7_3:', nd7_3)
'''
代码输出:
nd1: [1. 2. 3. 4.]
nd1元素类型: float64
nd2: [[0. 0. 0.]
[0. 0. 0.]]
nd3: [[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
nd4: [[0. 0. 1. 1.]
[1. 1. 1. 1.]
[1. 0. 0. 1.]]
nd5: [[1 2]
[3 4]]
nd5_2: [[1 2]
[3 4]]
nd6: [[1. 2.]
[3. 4.]]
nd7: [[0.40850696 0.13023629 0.65993262 0.44094543]
[0.39357602 0.49633451 0.65386546 0.08969138]
[0.21696212 0.75754505 0.6421084 0.86626301]]
nd7_2: [[4 6 1]
[3 6 7]]
nd7_3: [[ 1.40482872 -2.84858258 -2.95598728 -0.31476042]
[ 2.82887046 2.71789577 -0.2911986 1.55282051]
[ 1.01122184 -1.10465388 -2.52051117 2.43563793]]
'''
import numpy as np
# 使用astype转换数组元素的类型
nd = np.array([1, 2, 3, 4, 5])
print('nd的元素类型:', nd.dtype)
nd1 = nd.astype(np.float64)
print('nd1的元素类型:', nd1.dtype)
# 使用logspace创建等比数列
# [2^0,2^3]区间中取出4个元素,组成等比数列
logd = np.logspace(0, 3, 4, base=2, dtype=np.int32)
print(logd)
# [0,10]区间取出11个元素组成等差数列
lind = np.linspace(0, 10, endpoint=True, num=11)
print(lind)
'''
nd的元素类型: int32
nd1的元素类型: float64
[1 2 4 8]
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
'''
import numpy as np
data = np.random.uniform(-3, 3, size=(2, 3))
print(data)
print("向上取整:", np.ceil(data))
print("向下取整:", np.floor(data))
print("四舍五入:", np.rint(data))
print("是否为空:", np.isnan(data))
print("对位乘法:", np.multiply(data, data))
print("对位除法:", np.divide(data, data))
print("绝对值:", np.abs(data))
print("三目运算:", np.where(data > 0, 1, -1))
'''
代码输出:(注意,每次输出结果并不相同,因为有random)
[[-1.3457752 1.57889323 1.62845167]
[ 2.52058874 0.24765177 -1.19728704]]
向上取整: [[-1. 2. 2.]
[ 3. 1. -1.]]
向下取整: [[-2. 1. 1.]
[ 2. 0. -2.]]
四舍五入: [[-1. 2. 2.]
[ 3. 0. -1.]]
是否为空: [[False False False]
[False False False]]
对位乘法: [[1.8111109 2.49290383 2.65185485]
[6.35336761 0.0613314 1.43349626]]
对位除法: [[1. 1. 1.]
[1. 1. 1.]]
绝对值: [[1.3457752 1.57889323 1.62845167]
[2.52058874 0.24765177 1.19728704]]
三目运算: [[-1 1 1]
[ 1 1 -1]]
'''
import numpy as np
x = np.random.randint(1, 10, size=(2, 3))
y = np.random.randint(1, 10, size=(3, 2))
print('x:\n', x)
print('y:\n', y)
# 对位相加
print('x + x:\n', x + x)
# 对位相减
print('x - x\n', x - x)
# 所有元素+/-1
print('x + 1:\n', x + 1)
print('x - 1:\n', x - 1)
# 矩阵乘法
print('x dot y:\n', x.dot(y))
# 数组元素对位相乘
print('x multiply x:\n', np.multiply(x, x))
'''
代码输出:
x:
[[9 7 3]
[4 4 9]]
y:
[[3 6]
[7 5]
[1 8]]
x + x:
[[18 14 6]
[ 8 8 18]]
x - x
[[0 0 0]
[0 0 0]]
x + 1:
[[10 8 4]
[ 5 5 10]]
x - 1:
[[8 6 2]
[3 3 8]]
x dot y:
[[ 79 113]
[ 49 116]]
x multiply x:
[[81 49 9]
[16 16 81]]
'''