一:常用矩阵的方法和属性

1.向量转矩阵

print(numpy.arange(15))
a = numpy.arange(15).reshape(3,5)
print(a)

打印结果:

[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]

[10 11 12 13 14]]

Tips:reshpe(3, -1)也可以,-1表示默认。多维如reshape(2, 3,-1)。


2.矩阵的维度

print(a.ndim)

打印结果:2


3.矩阵中数据的类型

print(a.dtype.name)

打印结果:int32


4.矩阵的长度

print(a.size)

打印结果:15


二:矩阵的初始化

1.初始化为0

numpy.zeros((行数, 列数))

注意:此处有两个括号

init1 = numpy.zeros((3, 4))print(init1)

打印结果:

[[ 0. 0. 0. 0.]

[ 0. 0. 0. 0.]

[ 0. 0. 0. 0.]]

注意:此处默认初始化为float


2.初始化为1

init2 = numpy.ones((2,3,4),dtype="int32")print(init2)

打印结果:

[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]

[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]

注意:dtype可以修改初始化后的类型


3.初始化一个等差数列

init3 = numpy.arange(10, 30, 5)print(init3)
打印结果:
[10 15 20 25]

4.初始化一个随机矩阵

init4 = numpy.random.random((2,3))print(init4)

打印结果:

[[ 0.12118478 0.28265194 0.7184926 ]
[ 0.29421611 0.20473122 0.49643855]]

4.在某个区间中等距取值

numpy.linspace(起始值,结束值,间隔)

from numpy import piinit5 = numpy.linspace(0, 2*pi, 10)print(init5)

打印结果:

[ 0. 0.6981317 1.3962634 2.0943951 2.7925268 3.4906585
4.1887902 4.88692191 5.58505361 6.28318531]


三:矩阵的计算

1.三角函数

a = numpy.sin(numpy.linspace(0, 2*pi, 5))print(a)
打印结果:
[ 0.00000000e+00 1.00000000e+00 1.22464680e-16 -1.00000000e+00
-2.44929360e-16]

不明此处sin(pi)的值为什么不是0


2.简单计算

a = numpy.array([10, 20, 30, 40])b = numpy.arange(4)print(a - b)print(a + b)print(a * b)print(b / a)print(b ** 2) #乘方print(a < 35)

打印结果:

[10 19 28 37]
[10 21 32 43]
[ 0 20 60 120]
[ 0. 0.05 0.06666667 0.075 ]
[0 1 4 9]

[ True True True False]


3.矩阵乘法

A = numpy.array([    [1, 1],    [0, 1]])B = numpy.array([    [2, 0],    [3, 4]])print(A)print("#####")print(B)print("#####")print(A*B) #对应位置相乘print("#####")print(A.dot(B))  #矩阵乘法print("#####")print(numpy.dot(A,B)) #矩阵乘法print("#####")

打印结果:

[[1 1]
[0 1]]
#####
[[2 0]
[3 4]]
#####
[[2 0]
[0 4]]
#####
[[5 4]
[3 4]]
#####
[[5 4]
[3 4]]
#####


4.平方和开方

a = numpy.arange(3)print(numpy.exp(a))print(numpy.sqrt(a))

打印结果:

[ 1. 2.71828183 7.3890561 ]
[ 0. 1. 1.41421356]

5.矩阵和向量的转换

a = numpy.floor(10 * numpy.random.random((3, 4))) #向下取整print(a)print("######")print(a.ravel())  #多维矩阵转向量a.shape = (6, 2)print(a)print("######")print(a.T)  #转置矩阵

打印结果:

[[ 6. 5. 2. 7.]
[ 4. 4. 8. 9.]
[ 6. 9. 7. 0.]]
######
[ 6. 5. 2. 7. 4. 4. 8. 9. 6. 9. 7. 0.]
[[ 6. 5.]
[ 2. 7.]
[ 4. 4.]
[ 8. 9.]
[ 6. 9.]
[ 7. 0.]]
######
[[ 6. 2. 4. 8. 6. 7.]
[ 5. 7. 4. 9. 9. 0.]]

6.矩阵的拼接

a = numpy.floor(10 * numpy.random.random((2, 2)))b = numpy.floor(10 * numpy.random.random((2, 2)))print(a)print("######")print(b)print("######")print(numpy.hstack((a, b)))print("######")print(numpy.vstack((a, b)))

打印结果:

[[ 3. 0.]
[ 2. 7.]]
######
[[ 8. 4.]
[ 7. 4.]]
######
[[ 3. 0. 8. 4.]
[ 2. 7. 7. 4.]]
######
[[ 3. 0.]
[ 2. 7.]
[ 8. 4.]

[ 7. 4.]]


7.矩阵的切割

numpy.hsplit(要切割的矩阵,切割成的份数)

a = numpy.floor(10 * numpy.random.random((2, 12)))print(a)print("#####")print(numpy.hsplit(a, 3))print("#####")print(numpy.hsplit(a, (3, 4))) #(3, 4)该元组表示在第三个和第四个空隙切割a = numpy.floor(10 * numpy.random.random((12, 2)))print("#####")print(a)print("#####")print(numpy.vsplit(a, 3))

打印结果:

[[ 9. 7. 6. 1. 4. 2. 4. 9. 1. 2. 2. 0.]
[ 6. 8. 5. 0. 7. 8. 9. 3. 2. 6. 5. 2.]]
#####
[array([[ 9., 7., 6., 1.],
[ 6., 8., 5., 0.]]), array([[ 4., 2., 4., 9.],
[ 7., 8., 9., 3.]]), array([[ 1., 2., 2., 0.],
[ 2., 6., 5., 2.]])]
#####
[array([[ 9., 7., 6.],
[ 6., 8., 5.]]), array([[ 1.],
[ 0.]]), array([[ 4., 2., 4., 9., 1., 2., 2., 0.],
[ 7., 8., 9., 3., 2., 6., 5., 2.]])]
#####
[[ 2. 8.]
[ 7. 7.]
[ 3. 3.]
[ 5. 2.]
[ 5. 6.]
[ 4. 4.]
[ 5. 4.]
[ 3. 1.]
[ 2. 2.]
[ 4. 6.]
[ 8. 1.]
[ 1. 7.]]
#####
[array([[ 2., 8.],
[ 7., 7.],
[ 3., 3.],
[ 5., 2.]]), array([[ 5., 6.],
[ 4., 4.],
[ 5., 4.],
[ 3., 1.]]), array([[ 2., 2.],
[ 4., 6.],
[ 8., 1.],
[ 1., 7.]])]




更多相关文章

  1. 数组与矩阵---需要排序的最短子数组长度
  2. python 按位置关系输出矩阵元素
  3. 显示一个矩阵,将一个共同因素放在同情中
  4. 矩阵类的python实现
  5. leet240. 搜索二维矩阵 II
  6. 彻底理解初始化参数SERVICE_NAMES和客户端TNS中的SERVICE_NAME
  7. identity_insert和表初始化脚本
  8. 【Linux】Mysql初始化root密码和允许远程访问
  9. Java类的初始化顺序

随机推荐

  1. Android SDK开发 -- TitleBar重构 - 使用
  2. Android 复习笔记之图解Intent和IntentFi
  3. HTC 為 Android(安卓)4.0 更新再發聲明
  4. [已解决]mac android studio安装报错java
  5. Android Studio设置apk文件名
  6. android 取消Title 头部标题栏
  7. Android uiautomator dump 命令介绍
  8. oauth点击授权之后,不Callback到指定url
  9. android 车机电话的通讯录联系人搜索实现
  10. Android Phone进程启动过程