import numpy as np
A = np.array([1,1,1])
B = np.array([2,2,2])
#数组上下合并
C = np.vstack((A,B))
print(C)
"""
[[1 1 1]
[2 2 2]]
"""
#左右合并
D = np.hstack((A,B))
print(D)
"""
[1 1 1 2 2 2]
"""
print(A.shape,C.shape,D.shape)
"""
(3,) (2, 3) (6,)
"""
#将数组转化为纵向
print(A[:,np.newaxis])
"""
[[1]
[1]
[1]]
"""
print(A.reshape(A.size,1))
"""
[[1]
[1]
[1]]
"""
print(A.reshape(-1,1))
"""
[[1]
[1]
[1]]
"""
#纵向组合
D = np.hstack((A[:,np.newaxis],B[:,np.newaxis]))
print(D)
"""
[[1 2]
[1 2]
[1 2]]
"""
E = np.c_[A,B]
print(E)
"""
[[1 2]
[1 2]
[1 2]]
"""
D = np.concatenate((A[:,np.newaxis],B[:,np.newaxis],B[:,np.newaxis]),axis=1)
print(D)
"""
[[1 2 2]
[1 2 2]
[1 2 2]]
"""
D = np.hstack((A[:,np.newaxis],B[:,np.newaxis],B[:,np.newaxis]))
print(D)
"""
[[1 2 2]
[1 2 2]
[1 2 2]]
"""
#数组分割
a = np.arange(12).reshape((3,4))
print(a)
"""
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
"""
#横向分割,列分割(均等)
c,d = np.split(a,2,axis=1)
print(c,d)
"""
[[0 1]
[4 5]
[8 9]] [[ 2 3]
[ 6 7]
[10 11]]
"""
print(np.hsplit(a,2))
"""
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
"""
#纵向分割,行分割(均等)
print(np.split(a,1,axis=0))
"""
[array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])]
"""
print(np.split(a,3,axis=0))
"""
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
"""
#print(np.split(a,6,axis=0))
"""
ValueError: array split does not result in an equal division
"""
print(np.vsplit(a,3))
"""
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
"""
#纵向分割,行分割(不均等)
print(np.array_split(a,3,axis=1))
"""
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2],
[ 6],
[10]]), array([[ 3],
[ 7],
[11]])]
"""
#赋值篇
a = np.arange(4)
b=a
d=b
print(a)
"""
[0 1 2 3]
"""
a[0]=5
print(a)
"""
[5 1 2 3]
"""
#判断b是否完全是a(地址和值)
print(b is a)
"""
True
"""
print(b)
"""
[5 1 2 3]
"""
print(d is a)
"""
True
"""
d[1:3] = [22,33]
print(a)
"""
[ 5 22 33 3]
"""
#copy拥有一样的值,但不关联
b = a.copy()
print(b is a)
"""
False
"""
print(b)
""""
[ 5 22 33 3]
"""
a[1] = 222
print(b)
"""
[ 5 22 33 3]
"""
b = a[:]
print(b is a)
"""
False
"""
python--numpy下
最新推荐文章于 2024-06-13 00:23:49 发布