维小维写作训练营python学习笔记

python:数组的形状、查看形状和修改形状

2019-08-12  本文已影响0人  书生_Scholar
import numpy as np
t5 = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])
print(t5)


# 输出如下
out:
array([[3,4,5,6,7,8],
          [4,5,6,7,8,9]])
t5.shape

# 输出如下(2行6列):
out:
(2,6)

当不知道该数据是几纬数组的时候,可以通过以下代码将其修改为一维:

# 1、方法一:t5.shape[0] 代表行数, t5.shape[1]代表列数,行乘列=数据的个数
t6 = t5.reshape((t5.shape[0] * t5.shape[1],))

# 2、方法二:用flatten 方法
t5.flatten()
t5.reshape((6,2))   # 将t5改为6行2列

# 输出如下:
array([[3, 4],
       [5, 6],
       [7, 8],
       [4, 5],
       [6, 7],
       [8, 9]])

-4 数组的计算

#1、数组和数字的计算

in[21]:t5 = np.arange(24).reshape(4,6)
in[21]:t5
Out[22]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
in[23]:t5 + 2
Out[23]: 
array([[ 2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13],
       [14, 15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24, 25]])
in[24]:t5 * 2
Out[24]: 
array([[ 0,  2,  4,  6,  8, 10],
       [12, 14, 16, 18, 20, 22],
       [24, 26, 28, 30, 32, 34],
       [36, 38, 40, 42, 44, 46]])
in[25]:t5 / 2
Out[25]: 
array([[ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5],
       [ 3. ,  3.5,  4. ,  4.5,  5. ,  5.5],
       [ 6. ,  6.5,  7. ,  7.5,  8. ,  8.5],
       [ 9. ,  9.5, 10. , 10.5, 11. , 11.5]])


# python 中 NAN代表0/0,不是一个数字。inf是无限的,不是一个数字。

in[26]:t5 / 0              
Out[26]: C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevconsole.py:1: RuntimeWarning: divide by zero encountered in true_divide
  '''
C:\Program Files\JetBrains\PyCharm Community Edition 2019.2\helpers\pydev\pydevconsole.py:1: RuntimeWarning: invalid value encountered in true_divide
  '''
array([[nan, inf, inf, inf, inf, inf],
       [inf, inf, inf, inf, inf, inf],
       [inf, inf, inf, inf, inf, inf],
       [inf, inf, inf, inf, inf, inf]])

# 2、数组和数组的计算

in[27]:t6 = np.arange(100,124).reshape((4,6))
in[28]:t6
Out[28]: 
array([[100, 101, 102, 103, 104, 105],
       [106, 107, 108, 109, 110, 111],
       [112, 113, 114, 115, 116, 117],
       [118, 119, 120, 121, 122, 123]])

in[29]:t6 + t5
Out[29]: 
array([[100, 102, 104, 106, 108, 110],
       [112, 114, 116, 118, 120, 122],
       [124, 126, 128, 130, 132, 134],
       [136, 138, 140, 142, 144, 146]])
#  3、不一样的数组之间的计算

in[30]:t7 = np.arange(0,6)
in[31]:t7 
Out[31]: array([0, 1, 2, 3, 4, 5])
in[32]:t7 + t5
Out[32]: 
array([[ 0,  2,  4,  6,  8, 10],
       [ 6,  8, 10, 12, 14, 16],
       [12, 14, 16, 18, 20, 22],
       [18, 20, 22, 24, 26, 28]])
in[33]:t5 -t7
Out[33]: 
array([[ 0,  0,  0,  0,  0,  0],
       [ 6,  6,  6,  6,  6,  6],
       [12, 12, 12, 12, 12, 12],
       [18, 18, 18, 18, 18, 18]])
in[34]:t8 = np.arange(4).reshape(4,1)
in[35]:t8
Out[35]: 
array([[0],
       [1],
       [2],
       [3]])
in[36]:t5 - t8              
Out[36]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 5,  6,  7,  8,  9, 10],
       [10, 11, 12, 13, 14, 15],
       [15, 16, 17, 18, 19, 20]])
#  4、当数组之间行或列个数不一致时候,无法计算,程序报错(数组的形状不一致)
in[37]:t9 = np.arange(10)
in[38]:t9
Out[38]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
in[39]:t5 - t9
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-39-184b275be222>", line 1, in <module>
    t5 - t9
ValueError: operands could not be broadcast together with shapes (4,6) (10,) 
in[40]:
in[41]:
in[42]:
in[43]:
in[44]:



上一篇下一篇

猜你喜欢

热点阅读