什么是numpy?

一个在Python中做科学计算的基础库,重在数值计算,也是大部分PYTHON科学计算库的基础库,多用于在大型、多维数组上执行数值运算.

numpy是用来处理数值型数据的

 

import random

import numpy as np

# 使用numpy生成数组,生成numpy.ndarray的类型,这种类型数值之间没有逗号
# np.array(p_object,dtype=None) # p_object列表  dtype指定数据类型
t1 = np.array([1, 2, 3])
print(t1, type(t1))  # 结果[1 2 3] <class 'numpy.ndarray'>
t2 = np.array(range(10))
print(t2)
# 结果 [0 1 2 3 4 5 6 7 8 9]

# arrange使用方法 arange([start,] stop [,step], dtype=None) # dtype指定是当前数组的类型
t3 = np.arange(10)  # 帮助我们生成一堆数组
print(t3)
# 结果 [0 1 2 3 4 5 6 7 8 9]

# dtype 查看当前数组的类型
print(t3.dtype)  # 结果 int64  64当前电脑的位数,存一个数值需要64位,位数越少占用内存越少
t4 = np.array([1, 3, 5], dtype=bool)
print(t4)
# 结果 [0 1 2 3 4 5 6 7 8 9] [ True  True  True]

# astype 复制调原有数组并更改数据类型
t5 = t4.astype('int64')
print(t5)
# 结果 [1 1 1]

# numpy中的小数
t6 = np.array([random.random() for i in range(10)])
print(t6)
# 结果  [0.66398033 0.59250261 0.46354278 0.66351646 0.07548666 0.06377063 0.01887494 0.63553692 0.85932655 0.0017619 ]

# 保留两位小数
t7 = t6.round(2)
print(t7)
# 结果 [0.29 0.92 0.94 0.62 0.26 0.43 0.25 0.33 0.1  0.4 ]

 

数组的形状

# 1.查看数组的形状  shape
In [5]: t1.shape                                                                                                                                                                                            
Out[5]: (12,)

#2.修改数组的形状 reshape,不修改原数组
In [13]: t2 = np.arange(12)                                                                                                                                                                                 

In [14]: t2                                                                                                                                                                                                 
Out[14]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

In [15]:  t3 = t2.reshape(3,4)                                                                                                                                                                              

In [16]: t3                                                                                                                                                                                                 
Out[16]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
# 查看数组的维度
array.ndim

多维数组

#将一维数组变成3纬数组
In [19]: t5 = np.arange(24)                                                                                                                                                                                 

In [20]: t5                                                                                                                                                                                                 
Out[20]: 
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 [21]: t5.reshape(2,3,4)   # 2代表2块数据,3代表每块3行,4代表每块4列                                                                                                                                                                               
Out[21]: 
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]]])
#变成一维数组
t6.reshape((24,))  # 而不是t6.reshape((24,1))

In [23]: t6                                                                                                                                                                                                 

Out[23]: 

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 [24]: t6.flatten()      # 直接将t6变成一维的数组                                                                                                                                                                                 

Out[24]: 

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 [26]: t7                                                                                                                                                                                                 
Out[26]: 
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 [27]: t7+2                                                                                                                                                                                               
Out[27]: 
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 [28]: t1  = np.arange(100,124)                                                                                                                                                                           
In [29]: t1                                                                                                                                                                                                 
Out[29]: 
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 [30]: t7                                                                                                                                                                                                 
Out[30]: 
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 [31]: t7+t1                                                                                                                                                                                              
Out[31]: 
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])

# 两个维度不相同的数计算(前提列数相同)

In [32]: t1 =np.arange(1,5)                                                                                                                                                                                 
In [33]: t1                                                                                                                                                                                                 
Out[33]: array([1, 2, 3, 4])
In [34]: t2  = np.arange(1,4)                                                                                                                                                                               
In [35]: t2                                                                                                                                                                                                 
Out[35]: array([1, 2, 3])
In [36]: t1+t2                                                                                                                                                                                              
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-36-5260db44d29f> in <module>
----> 1 t1+t2

ValueError: operands could not be broadcast together with shapes (4,) (3,) 

#正确的,多维的那个数组中的每行数据都会和一维的那个数据计算
In [39]: t2 = np.arange(0,8).reshape(2,4)                                                                                                                                                                   
In [40]: t2                                                                                                                                                                                                 
Out[40]: 
array([[0, 1, 2, 3],
       [4, 5, 6, 7]])

In [41]: t1                                                                                                                                                                                                 
Out[41]: array([1, 2, 3, 4])

In [42]: t2+t1                                                                                                                                                                                              
Out[42]: 
array([[ 1,  3,  5,  7],
       [ 5,  7,  9, 11]])

 

numpy的广播原则:

如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失维度和(或)轴长度为1的维度上进行。

这是官方的话,我们该怎么理解呢?举个例子

shape为(3,)每行中有3列和shape为(2,3)每行中也是有3列的,这样的是可以计算的,也就是说两个数组中的列数只要相同就可以计算

数组的转置

transpose 将行列对调

T 也有这个功能

In [46]: t2 = np.arange(24).reshape(4,6)                                                                                                                                                                    

In [47]: t2                                                                                                                                                                                                 
Out[47]: 
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 [48]: t2.transpose()                                                                                                                                                                                     
Out[48]: 
array([[ 0,  6, 12, 18],
       [ 1,  7, 13, 19],
       [ 2,  8, 14, 20],
       [ 3,  9, 15, 21],
       [ 4, 10, 16, 22],
       [ 5, 11, 17, 23]])

In [49]:  

 

posted on 2020-04-15 22:58  程序员一学徒  阅读(152)  评论(0)    收藏  举报