Github源码In [1]:
import numpy as np
B = np.arange(3)
print(B)
print(np.exp(B)) #返回e的幂次方,e是一个常数为2.71828
print(np.sqrt(B))# 开根号
[0 1 2]
[1. 2.71828183 7.3890561 ]
[0. 1. 1.41421356]
In [2]:
# floor向下取整 3.5 向下取整为 3
a = np.floor(10*np.random.random((3,4)))
print(a)
print(a.shape)
# 把矩阵拉平,从矩阵拉成了向量 返回新的 不改变原来的数据
print(a.ravel())
print(7777777777777777777777)
print(a)
print(a.shape)
a.shape = (6, 2)
print(a)
print(a.T) # 转置
print(a.resize((2,6)))
print(a)
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
#a.reshape(3,-1) # 3行 -1自动计算
[[5. 2. 7. 0.]
[3. 6. 4. 7.]
[4. 0. 7. 7.]]
(3, 4)
[5. 2. 7. 0. 3. 6. 4. 7. 4. 0. 7. 7.]
7777777777777777777777
[[5. 2. 7. 0.]
[3. 6. 4. 7.]
[4. 0. 7. 7.]]
(3, 4)
[[5. 2.]
[7. 0.]
[3. 6.]
[4. 7.]
[4. 0.]
[7. 7.]]
[[5. 7. 3. 4. 4. 7.]
[2. 0. 6. 7. 0. 7.]]
None
[[5. 2. 7. 0. 3. 6.]
[4. 7. 4. 0. 7. 7.]]
In [4]:
# hstack 矩阵横拼接,hstack 矩阵纵拼接
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print('---')
print(b)
print('---')
print(np.hstack((a,b)) )#横拼接
print('---')
print(np.vstack((a,b)) ) #竖拼接
[[4. 2.]
[7. 1.]]
---
[[8. 4.]
[2. 1.]]
---
[[4. 2. 8. 4.]
[7. 1. 2. 1.]]
---
[[4. 2.]
[7. 1.]
[8. 4.]
[2. 1.]]
In [5]:
a = np.floor(10*np.random.random((2,12)))
print(a)
print('---')
print(np.hsplit(a,3)) #横着切分 3分
print('---')
# 传元组 就相当于指定了一个位置 在(3,4)位置切一刀
print(np.hsplit(a,(3,4)) )
a = np.floor(10*np.random.random((12,2)))
print(a)
np.vsplit(a,3) #纵切分
[[1. 7. 6. 4. 7. 7. 5. 6. 8. 0. 1. 6.]
[7. 2. 2. 8. 6. 4. 6. 9. 5. 5. 4. 2.]]
---
[array([[1., 7., 6., 4.],
[7., 2., 2., 8.]]), array([[7., 7., 5., 6.],
[6., 4., 6., 9.]]), array([[8., 0., 1., 6.],
[5., 5., 4., 2.]])]
---
[array([[1., 7., 6.],
[7., 2., 2.]]), array([[4.],
[8.]]), array([[7., 7., 5., 6., 8., 0., 1., 6.],
[6., 4., 6., 9., 5., 5., 4., 2.]])]
[[9. 0.]
[1. 8.]
[3. 5.]
[8. 2.]
[7. 3.]
[0. 8.]
[7. 7.]
[5. 9.]
[4. 2.]
[1. 9.]
[9. 6.]
[3. 7.]]
Out[5]:
[array([[9., 0.],
[1., 8.],
[3., 5.],
[8., 2.]]), array([[7., 3.],
[0., 8.],
[7., 7.],
[5., 9.]]), array([[4., 2.],
[1., 9.],
[9., 6.],
[3., 7.]])]
In [8]:
#Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
print(b is a)
b.shape = 3,4
print(a.shape)
print(id(a))
print(id(b)) # 名字不同 指向的内存地址是一样的
True
(3, 4)
86084464
86084464
In [7]:
#The view method creates a new array object that looks at the same data.
c = a.view()# 相当于是一个浅复制 id值不一样 但是公用的是一套值
print(c is a)
c.shape = 2,6
#print a.shape
c[0,4] = 1234
a
False
Out[7]:
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
In [9]:
#The copy method makes a complete copy of the array and its data.
d = a.copy() # 深复制 么有关系的
print(d is a)
d[0,0] = 9999
print(a)
print(b)
False
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
本博客源码Github地址:
请随手给个star,谢谢!