初识numpy

numpy.arange(start,end,step)

创建一个numpy 对象

numpy.arange(1,7) #[1 2 3 4 5 6]
跟序列的有点相似,range方法吧

numpy对象的dtype 属性是一个描述对象本身的一个类型

a =- numpy.arange(1,7)
b = a.astype(numpy.str_)
print(a,b,sep="\n")
#[1 2 3 4 5 6]
#['1' '2' '3' '4' '5' '6']

a.astype 并不会改变a对这个地址的描述,改变的只是b对这个地址的描述
numpy.dtype 打印出元素类型

print(a.dtype) #int32
a.shape #(6,)  shape 表示数组的维度
a.reshape((2,3)) #改变数组的维度 二行三列 [[1 2 3]
									[4 5 6]]

ravel 将numpy对象变成一维

a = np.arange(1,7)
a.reshape(2,3)
print(a)
print(a.ravel())
[[1 2 3]
 [4 5 6]]
[1 2 3 4 5 6]
[1 2 3 4 5 6]

复制变维 flatten
复制,类似于 深拷贝

就地变维
nmpy.shape = ()

组合

垂直组合
v = numpy.vstack((u,d))

# b [0 2 3 4 5 6]
# c [1 2 3 4 5 6]
c = np.vstack((b,c))
print(c)
[[0 2 3 4 5 6]
 [1 2 3 4 5 6]]

水平组合

h = numpy.hstack((l,r))
# b [0 2 3 4 5 6]
# c [1 2 3 4 5 6]
c = numpy.hstack((b,c))
[0 2 3 4 5 6 1 2 3 4 5 6]

行组合

r = numpy.row_stack((u,d))
u: [1 2 3]
d: [4 5 6]
r :[[1 2 3]
     [4 5 6]]

类组合

c = numpy.column_stack ((l, r))
l: [1 2 3]
r: [4 5 6]
c: [[1 4]
     [2 5]
     [3 6]]

分割

1)垂直分割

u,m,d = numpy.vsplit(v,3)

# b [[1 2 3] [ 3 4 5 ]]
a,v = np.vsplit(b,2)
print(a,v)
#[[1 2 3]] [[4 5 6]]

2)水平分割

a,b,c = numpy.hsplit(v,3)

[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[1]
 [4]
 [7]]
[[2]
 [5]
 [8]]

3)深度分割

x, y = numpy.dsplit (d, 2)

猜你喜欢

转载自blog.csdn.net/weixin_43847832/article/details/88095333