TASK03 改变数组形状

更改形状
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。

numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。

a=np.array([1,2,3,4,5,6,7,8])
print(a)
a.shape=(2,4)
print(a)
[1 2 3 4 5 6 7 8]
[[1 2 3 4]
 [5 6 7 8]]
x=np.array([[1,2,3,4,5,6],
            [7,8,9,10,11,12],
            [13,14,15,16,17,18],
            [19,20,21,22,23,24],
            [25,26,27,28,29,30]])

y=x.flat
print(y)
for i in y:
    print()
    
<numpy.flatiter object at 0x7fa1c3154400>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
y[6]=0
print(x)

[[ 1  2  3  4  5  6]
 [ 0  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]
 [25 26 27 28 29 30]]

numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回。
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。(简记)
order:{'C / F,'A,K},可选使用此索引顺序读取a的元素。'C’意味着以行大的C风格顺序对元素进行索引,最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素,其中第一个索引变化最快,最后一个索引变化最快。请注意,'C’和’F’选项不考虑基础数组的内存布局,仅引用轴索引的顺序.A’表示如果a为Fortran,则以类似Fortran的索引顺序读取元素在内存中连续,否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素,但步幅为负时反转数据除外。默认情况下,使用Cindex顺序。

flatten()函数返回的是拷贝。

x=np.array([[1,2,3,4,5,6],              
            [7,8,9,10,11,12],           
            [13,14,15,16,17,18],        
            [19,20,21,22,23,24],        
            [25,26,27,28,29,30]])       
                                        
y=x.flatten(order='F')                  
print(y)                                
for i in y:                             
    print(i)                            
                                        
y[6]=0                                  
print(x)                
[ 1  7 13 19 25  2  8 14 20 26  3  9 15 21 27  4 10 16 22 28  5 11 17 23
 29  6 12 18 24 30]
                
  [[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]
 [25 26 27 28 29 30]]

是对二维数组按列取出进行了排列。
ravel()返回的是视图:

y=np.ravel(
print(y)   
y[6]=0     
print(x)   
           
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30]
[[ 1  2  3  4  5  6]
 [ 0  8  9 10 11 12]
 [13 14 15 16 17 18]
 [19 20 21 22 23 24]
 [25 26 27 28 29 30]]

numpy.reshape(a, newshape[, order=‘C’])在不更改数据的情况下为数组赋予新的形状。

x = np.arange(12)          
y = np.reshape(x, [3, 4])  
print(x,y)                 
                           
[ 0  1  2  3  4  5  6  7  8  9 10 11] [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

一维数组改变形状为3*4的。

reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。

z=np.reshape(x,[2,-1])  
print(z)               
 
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]

reshape()函数当参数newshape = -1时,表示将数组降为一维

x=np.random.randint(12,size=[2,2,3])   
print(x)                               
y=np.reshape(x,-1)                     
print(y)                               
[[[ 3  6 11]
  [ 2  4  9]]

 [[ 4  9  9]
  [ 7  8  8]]]
[ 3  6 11  2  4  9  4  9  9  7  8  8]

数组转置
numpy.transpose(a, axes=None) Permute the dimensions of an array.
numpy.ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.

x=np.random.rand(5,5)*10               
print(x)                               
y=np.around(x,2)                       
print(x)                               
z=x.T                                  
print(z)                               
e=np.transpose(x)                      
print(e)                               
                    

[[6.82074309 3.32183013 6.2267032 0.92912843 4.70735116]
[8.17910982 3.25051628 6.10372193 5.99106829 5.2467783 ]
[8.84652761 8.35912815 5.706644 9.6244291 1.61874395]
[9.65976554 3.29521321 5.66354818 2.74334953 1.03035155]
[7.72125309 6.61462863 5.1262423 0.97052152 0.66395806]]
[[6.82074309 3.32183013 6.2267032 0.92912843 4.70735116]
[8.17910982 3.25051628 6.10372193 5.99106829 5.2467783 ]
[8.84652761 8.35912815 5.706644 9.6244291 1.61874395]
[9.65976554 3.29521321 5.66354818 2.74334953 1.03035155]
[7.72125309 6.61462863 5.1262423 0.97052152 0.66395806]]
[[6.82074309 8.17910982 8.84652761 9.65976554 7.72125309]
[3.32183013 3.25051628 8.35912815 3.29521321 6.61462863]
[6.2267032 6.10372193 5.706644 5.66354818 5.1262423 ]
[0.92912843 5.99106829 9.6244291 2.74334953 0.97052152]
[4.70735116 5.2467783 1.61874395 1.03035155 0.66395806]]
[[6.82074309 8.17910982 8.84652761 9.65976554 7.72125309]
[3.32183013 3.25051628 8.35912815 3.29521321 6.61462863]
[6.2267032 6.10372193 5.706644 5.66354818 5.1262423 ]
[0.92912843 5.99106829 9.6244291 2.74334953 0.97052152]
[4.70735116 5.2467783 1.61874395 1.03035155 0.66395806]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值