码迷,mamicode.com
首页 > 其他好文 > 详细

02_numpy

时间:2019-08-25 20:22:01      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:man   基本   tee   inline   lock   package   lua   geo   nal   

numpy get started

导入numpy库,并查看numpy版本

import numpy as np
np.__version__
'1.13.0'
import matplotlib.pyplot as plt
cat = plt.imread('cat.jpg')
print(cat)
[[[231 186 131]
  [232 187 132]
  [233 188 133]
  ..., 
  [100  54  54]
  [ 92  48  47]
  [ 85  43  44]]

 [[232 187 132]
  [232 187 132]
  [233 188 133]
  ..., 
  [100  54  54]
  [ 92  48  47]
  [ 84  42  43]]

 [[232 187 132]
  [233 188 133]
  [233 188 133]
  ..., 
  [ 99  53  53]
  [ 91  47  46]
  [ 83  41  42]]

 ..., 
 [[199 119  82]
  [199 119  82]
  [200 120  83]
  ..., 
  [189  99  65]
  [187  97  63]
  [187  97  63]]

 [[199 119  82]
  [199 119  82]
  [199 119  82]
  ..., 
  [188  98  64]
  [186  96  62]
  [188  95  62]]

 [[199 119  82]
  [199 119  82]
  [199 119  82]
  ..., 
  [188  98  64]
  [188  95  62]
  [188  95  62]]]
type(cat)
numpy.ndarray
cat.shape
(456, 730, 3)
plt.imshow(cat)
plt.show()

技术图片

#请问电影是什么,nd.array 四维
#(x,456,760,3)

一、创建ndarray

1. 使用np.array()由python list创建

参数为列表:
[1, 4, 2, 5, 3]

注意:

  • numpy默认ndarray的所有元素的类型是相同的
  • 如果传进来的列表中包含不同的类型,则统一为同一类型,优先级:str>float>int
l = [3,1,4,5,9,6]
n = np.array(l)
display(n,l)
array([3, 1, 4, 5, 9, 6])
[3, 1, 4, 5, 9, 6]
display(n.shape,l.shape)
--------------------------------------------------------------

AttributeError               Traceback (most recent call last)

<ipython-input-15-5eeacc6c47ae> in <module>()
----> 1 display(n.shape,l.shape)
AttributeError: 'list' object has no attribute 'shape'
n2 = np.array([[3,4,7,1],[3,0,1,8],[2,4,6,8]])
display(n2.shape)
(3, 4)
n3 = np.array(['0',9.18,20])
n3
array(['0', '9.18', '20'],
      dtype='<U4')
n4 = np.array([1,2,3.14])
n4
array([ 1.  ,  2.  ,  3.14])

2. 使用np的routines函数创建

包含以下常见创建方法:

1) np.ones(shape, dtype=None, order=‘C‘)

n = np.ones((4,5))
n
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
n2 = np.ones((4,5,6), dtype=int)
n2
array([[[1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1]],

       [[1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1],
        [1, 1, 1, 1, 1, 1]]])

2) np.zeros(shape, dtype=float, order=‘C‘)

n3 = np.zeros((4,5))
n3
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

3) np.full(shape, fill_value, dtype=None, order=‘C‘)

n = np.full((4,5), dtype=int, fill_value=8)
n
array([[8, 8, 8, 8, 8],
       [8, 8, 8, 8, 8],
       [8, 8, 8, 8, 8],
       [8, 8, 8, 8, 8]])

4) np.eye(N, M=None, k=0, dtype=float)
对角线为1其他的位置为0

n = np.eye(4,5)
n
# 满秩矩阵


# x + y = 10
# x - y = 5
# 1  1 
# 1  -1

# 第二行减去第一行
# 1   1
# 0   -2

# 1/2乘于第二行
# 1   1
# 0   -1

# 第二行加上第一行
# 1   0
# 0   -1

# 第二行乘与-1
# 1   0
# 0   1

# x + y 
# 2x + 2Y 
# 无解
# 1    1
# 2    2
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.]])

5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

n = np.linspace(0, 100, num=50, dtype=int,retstep=True, endpoint=False)
n
(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, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66,
        68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]), 2.0)
n = np.linspace(0, 150, num=50, dtype=np.int8)
n
# line 
# 2^(n-1) -1
# lin = linear algebra
array([   0,    3,    6,    9,   12,   15,   18,   21,   24,   27,   30,
         33,   36,   39,   42,   45,   48,   52,   55,   58,   61,   64,
         67,   70,   73,   76,   79,   82,   85,   88,   91,   94,   97,
        101,  104,  107,  110,  113,  116,  119,  122,  125, -128, -125,
       -122, -119, -116, -113, -110, -106], dtype=int8)

6) np.arange([start, ]stop, [step, ]dtype=None)

n = np.arange(10)
n
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
n = np.arange(1, 11, step=2)
n
array([1, 3, 5, 7, 9])

7) np.random.randint(low, high=None, size=None, dtype=‘l‘)

n = np.random.randint(10)
n 
8
n = np.random.randint(0, 255, size=(3,4,5))
n
array([[[ 89,  68,  18, 202,  49],
        [118, 159,  48, 190, 227],
        [177, 104, 232, 158,  64],
        [112, 125,   0,   7, 216]],

       [[  2, 180,  33, 152, 244],
        [ 46,  66, 185, 155, 253],
        [180, 135,  80, 135,  86],
        [ 64, 218,  69, 128,  90]],

       [[163,   7,  55,  60,  12],
        [ 15,  14, 181,  87,  62],
        [218,   7, 166, 100, 217],
        [137,   0,  42,  49, 194]]])
image = np.random.randint(0,255, size=(456,730,3))
image.shape
(456, 730, 3)
plt.imshow(image)
plt.show(image)

技术图片

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-97-a28aaec0347e> in <module>()
      1 plt.imshow(image)
----> 2 plt.show(image)
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\pyplot.py in show(*args, **kw)
    251     """
    252     global _show
--> 253     return _show(*args, **kw)
    254 
    255 
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\pylab\backend_inline.py in show(close, block)
     39         # only call close('all') if any to close
     40         # close triggers gc.collect, which can be slow
---> 41         if close and Gcf.get_all_fig_managers():
     42             matplotlib.pyplot.close('all')
     43 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

技术图片

8) np.random.randn(d0, d1, ..., dn)

标准正太分布

n = np.random.randn(10)
n
array([-0.4173303 , -0.41736696, -0.11888109, -0.51925789,  1.24985884,
        1.52967696,  0.05327912,  0.84738899,  1.03118302, -0.64532473])

9)np.random.normal(loc=0.0, scale=1.0, size=None)

n = np.random.normal(175, scale=5.0, size=50)
n 
array([177.62703208, 176.50746247, 173.26956915, 162.29355083,
       172.05271936, 177.61948035, 172.52243162, 175.43294252,
       181.14225673, 175.21450574, 179.56055092, 170.883815  ,
       170.91435313, 176.25008762, 176.3347509 , 183.90347049,
       178.91856559, 168.84725605, 176.32881783, 172.77973728,
       173.12257339, 174.75054378, 166.60349541, 171.68263799,
       168.83419713, 174.25085091, 175.66113435, 174.12039025,
       177.22772738, 169.01523024, 175.57587527, 172.89083838,
       179.52153939, 173.70318334, 179.06473552, 176.50099117,
       175.83008746, 174.78059027, 175.58909128, 178.11274357,
       183.45771692, 172.43399789, 179.56800892, 182.14239994,
       176.43701867, 177.37866513, 179.55215095, 174.5389049 ,
       175.48698667, 168.73145269])

10) np.random.random(size=None)

生成0到1的随机数,左闭右开

n = np.random.random(10)
n
array([0.22608606, 0.62764532, 0.62219649, 0.05348086, 0.94994404,
       0.29048963, 0.49340728, 0.04651386, 0.59005488, 0.59901244])

二、ndarray的属性

4个必记参数:
ndim:维度
shape:形状(各维度的长度)
size:总长度

dtype:元素类型

cat.ndim
3
cat.shape
(456, 730, 3)
cat.size
998640
cat.dtype
dtype('uint8')

三、ndarray的基本操作

1. 索引

一维与列表完全一致
多维时同理

l = [1,2,3,4,5]
l[2:4]
[3, 4]
n = np.array(l)
n[2]
3
# 找一个二维ndarray中的某个数
n2 = np.random.randint(0,255, size=(4,4))
n2
array([[  8, 117, 209,  86],
       [156, 192, 117, 180],
       [ 33,  70,  53, 179],
       [ 56, 236,  72,  45]])
# 查找53
n2[2][2]
53
n2[2,2]
53
n3 = np.random.randint(0,255, size=(4,5,6))
n3
array([[[128,  60, 108,  12, 112,  60],
        [234, 111, 237,  54,  22,  95],
        [127, 226,  30, 181,  20,  85],
        [239, 233, 210, 165, 186,  57],
        [ 27,  17,  72, 237, 208, 120]],

       [[199, 169, 190, 153, 181,  75],
        [179, 205, 116,  33, 239, 228],
        [154, 204, 138,   5, 231,  97],
        [ 55, 193, 245, 105,  78, 210],
        [157, 227, 239, 230, 242, 185]],

       [[ 67, 232, 113, 189, 245, 206],
        [220,  56, 241, 141, 146,  59],
        [ 46, 206, 152, 240, 105, 105],
        [176, 252, 185, 212, 180, 127],
        [165, 130, 206,  77,  11,  56]],

       [[194,  82,  72,  80,  94, 237],
        [179, 143, 191,  56,  37, 236],
        [194,  65, 223,  45, 223, 125],
        [ 92, 162,  94,  93,  69,   3],
        [ 39, 179, 213, 180,  23, 141]]])
n3[1,2,3]
5
np.random.seed(100)
np.random.seed(100)
np.random.randn(10)
array([-1.74976547,  0.3426804 ,  1.1530358 , -0.25243604,  0.98132079,
        0.51421884,  0.22117967, -1.07004333, -0.18949583,  0.25500144])
n = np.array([1,2,3,np.nan])
np.sum(n)
np.nansum(n)
6.0

根据索引修改数据

n3[1,2,3] = 8
n3
array([[[128,  60, 108,  12, 112,  60],
        [234, 111, 237,  54,  22,  95],
        [127, 226,  30, 181,  20,  85],
        [239, 233, 210, 165, 186,  57],
        [ 27,  17,  72, 237, 208, 120]],

       [[199, 169, 190, 153, 181,  75],
        [179, 205, 116,  33, 239, 228],
        [154, 204, 138,   8, 231,  97],
        [ 55, 193, 245, 105,  78, 210],
        [157, 227, 239, 230, 242, 185]],

       [[ 67, 232, 113, 189, 245, 206],
        [220,  56, 241, 141, 146,  59],
        [ 46, 206, 152, 240, 105, 105],
        [176, 252, 185, 212, 180, 127],
        [165, 130, 206,  77,  11,  56]],

       [[194,  82,  72,  80,  94, 237],
        [179, 143, 191,  56,  37, 236],
        [194,  65, 223,  45, 223, 125],
        [ 92, 162,  94,  93,  69,   3],
        [ 39, 179, 213, 180,  23, 141]]])

2. 切片

一维与列表完全一致
多维时同理

l = [1,2,3,4,5]
l[::-1]
[5, 4, 3, 2, 1]
l[::-2]
l
[1, 2, 3, 4, 5]

将数据反转,例如[1,2,3]---->[3,2,1]

n = np.random.randint(0, 255, size=(4,5))
n
array([[211, 112,  94, 165,   6],
       [ 86,  15, 241,  38, 139],
       [185, 247,  99,  91,  31],
       [221,  33,  40, 137, 162]])

两个::进行切片

n[::-1]
n
array([[211, 112,  94, 165,   6],
       [ 86,  15, 241,  38, 139],
       [185, 247,  99,  91,  31],
       [221,  33,  40, 137, 162]])

3. 变形

使用reshape函数,注意参数是一个tuple!

n = np.arange(6)
n
array([0, 1, 2, 3, 4, 5])
n2 = np.reshape(n,(3,2))
n2
array([[0, 1],
       [2, 3],
       [4, 5]])
cat.shape
(456, 730, 3)
n = np.reshape(cat, (8322, 120))
n
array([[231, 186, 131, ..., 235, 190, 135],
       [237, 192, 137, ..., 237, 192, 137],
       [237, 192, 137, ..., 239, 192, 138],
       ...,
       [203, 125,  89, ..., 201, 121,  86],
       [200, 120,  85, ..., 197, 117,  82],
       [197, 117,  82, ..., 188,  95,  62]], dtype=uint8)

4. 级联

  1. np.concatenate()
    级联需要注意的点:
  • 级联的参数是列表:一定要加中括号或小括号
  • 维度必须相同
  • 形状相符
  • 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
  • 可通过axis参数改变级联的方向
n1 = np.random.randint(0,255, size=(5,6))
n2 = np.random.randint(0,255, size=(5,6))
display(n1,n2)
array([[ 67, 115, 248,  66, 212, 248],
       [ 66, 156, 231, 250,  39, 195],
       [248, 172,  19,  21, 200, 206],
       [139,  25,   3,  18,   3,  49],
       [ 55,  21,  12,   6, 218, 116]])
array([[182, 251, 137,  33,  60,   6],
       [169, 117, 245, 218,  96, 168],
       [231,  59, 117, 179,  76,  84],
       [  6,  24,  25,  51, 136,  89],
       [ 67, 156, 135, 101, 147,  90]])
np.concatenate((n1,n2),axis=1)
array([[ 67, 115, 248,  66, 212, 248, 182, 251, 137,  33,  60,   6],
       [ 66, 156, 231, 250,  39, 195, 169, 117, 245, 218,  96, 168],
       [248, 172,  19,  21, 200, 206, 231,  59, 117, 179,  76,  84],
       [139,  25,   3,  18,   3,  49,   6,  24,  25,  51, 136,  89],
       [ 55,  21,  12,   6, 218, 116,  67, 156, 135, 101, 147,  90]])
  1. np.hstack与np.vstack
    水平级联与垂直级联,处理自己,进行维度的变更
# hstack h 
new_image = np.hstack((cat, image))
plt.imshow(new_image)
plt.show()

技术图片

# vstack vertical
new_image = np.vstack((cat, image))
plt.imshow(new_image)
plt.show()

技术图片

5. 切分

与级联类似,三个函数完成切分工作:

  • np.split
  • np.vsplit
  • np.hsplit
n = np.random.randint(0,100,size = (4,6))
n
array([[92,  7, 55,  5, 20, 53],
       [42, 61, 91, 64, 95, 18],
       [25, 93, 48, 35, 39, 13],
       [42, 97, 73, 57, 14, 59]])
np.vsplit(n,(1,2))
[array([[92,  7, 55,  5, 20, 53]]),
 array([[42, 61, 91, 64, 95, 18]]),
 array([[25, 93, 48, 35, 39, 13],
        [42, 97, 73, 57, 14, 59]])]
n = np.random.randint(0,100, size=(6,6))
n
array([[48, 77, 69, 24, 83, 20],
       [80, 92, 21, 97, 16, 37],
       [52, 99,  2, 33, 28,  3],
       [ 5, 53, 34,  3,  0, 95],
       [27, 73, 95, 85,  8, 48],
       [30, 54, 49, 75, 44, 90]])
np.vsplit(n, (2,5))
[array([[48, 77, 69, 24, 83, 20],
        [80, 92, 21, 97, 16, 37]]), array([[52, 99,  2, 33, 28,  3],
        [ 5, 53, 34,  3,  0, 95],
        [27, 73, 95, 85,  8, 48]]), array([[30, 54, 49, 75, 44, 90]])]
np.split(n, 3, axis=1)
[array([[48, 77],
        [80, 92],
        [52, 99],
        [ 5, 53],
        [27, 73],
        [30, 54]]), array([[69, 24],
        [21, 97],
        [ 2, 33],
        [34,  3],
        [95, 85],
        [49, 75]]), array([[83, 20],
        [16, 37],
        [28,  3],
        [ 0, 95],
        [ 8, 48],
        [44, 90]])]
np.vsplit(n, 3)
[array([[48, 77, 69, 24, 83, 20],
        [80, 92, 21, 97, 16, 37]]), array([[52, 99,  2, 33, 28,  3],
        [ 5, 53, 34,  3,  0, 95]]), array([[27, 73, 95, 85,  8, 48],
        [30, 54, 49, 75, 44, 90]])]
np.hsplit(n, 3)
[array([[48, 77],
        [80, 92],
        [52, 99],
        [ 5, 53],
        [27, 73],
        [30, 54]]), array([[69, 24],
        [21, 97],
        [ 2, 33],
        [34,  3],
        [95, 85],
        [49, 75]]), array([[83, 20],
        [16, 37],
        [28,  3],
        [ 0, 95],
        [ 8, 48],
        [44, 90]])]
np.hsplit(n,(2,4))
[array([[33, 46],
        [98, 40],
        [47, 53],
        [34, 91]]), array([[53,  7],
        [12, 55],
        [69, 50],
        [32, 52]]), array([[56, 43],
        [18, 64],
        [69,  7],
        [83, 38]])]
cat.shape
(456, 730, 3)
456
730
result = np.split(cat, 2, axis = 0)
plt.imshow(result[0])
plt.show()

技术图片

s_result = np.split(cat,2,axis = 1)
len(s_result)
2
plt.imshow(s_result[0])
plt.show()

技术图片

6. 副本

所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。

l = [1,2,3,4]
l2 = l
l2[2] = 5
l
[1, 2, 5, 4]
n1 = np.arange(10)
n2 = n1
n2[3] = 100
n1
array([  0,   1,   2, 100,   4,   5,   6,   7,   8,   9])
n3 = n1.copy()
n3[5]  = 200
n1
array([  0,   1,   2, 100,   4,   5,   6,   7,   8,   9])

可使用copy()函数创建副本

四、ndarray的聚合操作

1. 求和np.sum

n = np.arange(11)
n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
np.sum(n)
55
n = np.random.randint(0,100, size=(5,6))
n
array([[80, 20, 30, 66, 48, 50],
       [52, 33,  3, 76, 35,  9],
       [70, 99, 69, 50, 44, 31],
       [40, 13, 52, 50, 33, 45],
       [69, 42, 55, 30, 61, 22]])
np.sum(n, axis=1)
array([294, 208, 363, 233, 279])

2. 最大最小值:np.max/ np.min

同理

n = np.arange(11)
n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
np.median(n)
5.0
np.mean(n)
5.0
n = np.random.randint(0,100,size=10)
n
array([42, 64, 40,  7,  0, 79, 32, 95, 95, 59])
np.mean(n)
51.3
np.median(n)
50.5
np.max(n)
10
np.min(n)
0
n = np.random.randint(0,100, size=(5,6))
n
array([[82, 44,  0, 33, 72, 99],
       [66, 25, 36, 88, 74, 78],
       [ 3, 53, 33, 76, 96, 69],
       [62, 10, 16, 22, 12, 31],
       [41, 57, 43, 79, 34,  7]])
np.max(n, axis=0)
array([82, 57, 43, 88, 96, 99])

3. 其他聚合操作

Function Name   NaN-safe Version    Description
np.sum  np.nansum   Compute sum of elements
np.prod np.nanprod  Compute product of elements
np.mean np.nanmean  Compute mean of elements
np.std  np.nanstd   Compute standard deviation
np.var  np.nanvar   Compute variance
np.min  np.nanmin   Find minimum value
np.max  np.nanmax   Find maximum value
np.argmin   np.nanargmin    Find index of minimum value
np.argmax   np.nanargmax    Find index of maximum value
np.median   np.nanmedian    Compute median of elements
np.percentile   np.nanpercentile    Compute rank-based statistics of elements
np.any  N/A Evaluate whether any elements are true
np.all  N/A Evaluate whether all elements are true
np.power 幂运算
np.argmin(n, axis=0)
array([2, 3, 0, 3, 3, 4], dtype=int64)
cat.shape
(456, 730, 3)
cat2 = cat.reshape((-1,3))
cat2.shape
(332880, 3)
n = np.random.randint(0,10, size=(4,5))
n
array([[8, 8, 9, 1, 5],
       [7, 9, 9, 5, 9],
       [4, 1, 0, 0, 1],
       [6, 5, 4, 2, 9]])
np.reshape(n,(-1,))
array([8, 8, 9, 1, 5, 7, 9, 9, 5, 9, 4, 1, 0, 0, 1, 6, 5, 4, 2, 9])
cat3 = cat.reshape((456*730,3))
cat3.shape
(332880, 3)
cat3.max(axis = 0)
array([255, 242, 219], dtype=uint8)
max_cat = cat.max(axis = (0,1))
max_cat
array([255, 242, 219], dtype=uint8)
max_cat.shape
(3,)
cat.min()
0

np.sum 和 np.nansum 的区别
nan not a number

a = np.array([1,2,np.nan])
a
array([ 1.,  2., nan])
np.nansum(a)
3.0

操作文件

使用pandas打开文件president_heights.csv
获取文件中的数据

import pandas as pd
data = pd.read_csv('president_heights.csv')
type(data)
data
order name height(cm)
0 1 George Washington 189
1 2 John Adams 170
2 3 Thomas Jefferson 189
3 4 James Madison 163
4 5 James Monroe 183
5 6 John Quincy Adams 171
6 7 Andrew Jackson 185
7 8 Martin Van Buren 168
8 9 William Henry Harrison 173
9 10 John Tyler 183
10 11 James K. Polk 173
11 12 Zachary Taylor 173
12 13 Millard Fillmore 175
13 14 Franklin Pierce 178
14 15 James Buchanan 183
15 16 Abraham Lincoln 193
16 17 Andrew Johnson 178
17 18 Ulysses S. Grant 173
18 19 Rutherford B. Hayes 174
19 20 James A. Garfield 183
20 21 Chester A. Arthur 183
21 23 Benjamin Harrison 168
22 25 William McKinley 170
23 26 Theodore Roosevelt 178
24 27 William Howard Taft 182
25 28 Woodrow Wilson 180
26 29 Warren G. Harding 183
27 30 Calvin Coolidge 178
28 31 Herbert Hoover 182
29 32 Franklin D. Roosevelt 188
30 33 Harry S. Truman 175
31 34 Dwight D. Eisenhower 179
32 35 John F. Kennedy 183
33 36 Lyndon B. Johnson 193
34 37 Richard Nixon 182
35 38 Gerald Ford 183
36 39 Jimmy Carter 177
37 40 Ronald Reagan 185
38 41 George H. W. Bush 188
39 42 Bill Clinton 188
40 43 George W. Bush 182
41 44 Barack Obama 185
heights = data['height(cm)']
heights
type(heights)
pandas.core.series.Series
np.max(heights)
193
np.mean(heights)
179.73809523809524
np.std(heights)
6.931843442745893
np.min(heights)
163

五、ndarray的矩阵操作

1. 基本矩阵操作

1) 算术运算符:

  • 加减乘除
n = np.random.randint(0,10, size=(4,5))
n
array([[2, 5, 0, 4, 6],
       [0, 0, 7, 5, 0],
       [6, 3, 2, 9, 2],
       [5, 7, 0, 4, 5]])
# 加
n + 1
array([[ 3,  6,  1,  5,  7],
       [ 1,  1,  8,  6,  1],
       [ 7,  4,  3, 10,  3],
       [ 6,  8,  1,  5,  6]])
# 减 
n - 1
array([[ 1,  4, -1,  3,  5],
       [-1, -1,  6,  4, -1],
       [ 5,  2,  1,  8,  1],
       [ 4,  6, -1,  3,  4]])
# 两个矩阵相加
n2 = np.random.randint(0,10,size=(4,5))
n2
array([[2, 4, 2, 5, 9],
       [6, 6, 9, 6, 2],
       [9, 7, 5, 6, 1],
       [4, 6, 7, 2, 9]])
n + n2
array([[ 4,  9,  2,  9, 15],
       [ 6,  6, 16, 11,  2],
       [15, 10,  7, 15,  3],
       [ 9, 13,  7,  6, 14]])
n3 = np.random.randint(0,10,size=(2,5))
n3
array([[8, 0, 0, 5, 8],
       [4, 0, 3, 6, 7]])
n2 + n3
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-97-5f0861827bc6> in <module>()
----> 1 n2 + n3
ValueError: operands could not be broadcast together with shapes (4,5) (2,5) 

2) 矩阵积np.dot()

n1 = np.random.randint(0,10,size=(2,3))
n1
array([[8, 9, 4],
       [1, 7, 5]])
n2 = np.random.randint(0,10, size=(3,4))
n2
array([[4, 4, 2, 7],
       [4, 4, 7, 3],
       [1, 3, 2, 7]])
np.dot(n1,n2)
array([[ 72,  80,  87, 111],
       [ 37,  47,  61,  63]])

2. 广播机制

【重要】ndarray广播机制的两条规则

  • 规则一:为缺失的维度补1
  • 规则二:假定缺失元素用已有值填充

例1:
m = np.ones((2, 3))
a = np.arange(3)
求M+a

m = np.ones((2,3),dtype=int)
m
array([[1, 1, 1],
       [1, 1, 1]])
n = np.arange(3)
n
array([0, 1, 2])
m + n 
array([[1, 2, 3],
       [1, 2, 3]])

例2:
a = np.arange(3).reshape((3, 1))
b = np.arange(3)
求a+b

a = np.arange(3).reshape((3,1))
a
array([[0],
       [1],
       [2]])
b = np.arange(3)
b
array([0, 1, 2])
a + b
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

习题
a = np.ones((4, 1))
b = np.arange(4)
求a+b

六、ndarray的排序

小测验:
使用以上所学numpy的知识,对一个ndarray对象进行选择排序。

def Sortn(x):

代码越短越好

n = [5,2,3,6,9]

def bubble(n):
    for i in range(len(n) -1):
        for j in range(i+1, len(n)):
            if n[i] > n[j]:
                n[i], n[j] = n[j], n[i]
                

bubble(n)
n
[2, 3, 5, 6, 9]
# 选择排序
def select(n):
    for i in range(len(n)):
        # 选出最小值的索引
        index = np.argmin(n[i:]) + i
        # 把最小值和当前值的位置换一下
        n[i], n[index] = n[index], n[i]
        
n = [4, 6,1,0,3]
select(n)
n
[0, 1, 3, 4, 6]

1. 快速排序

np.sort()与ndarray.sort()都可以,但有区别:

  • np.sort()不改变输入
  • ndarray.sort()本地处理,不占用空间,但改变输入
n = np.random.randint(0,10,size=6)
n
array([6, 7, 1, 1, 8, 3])
np.sort(n)
array([1, 1, 3, 6, 7, 8])
np.sort(n)
n
array([6, 7, 1, 1, 8, 3])
n.sort()
n
array([1, 1, 3, 6, 7, 8])

02_numpy

标签:man   基本   tee   inline   lock   package   lua   geo   nal   

原文地址:https://www.cnblogs.com/pankypan/p/11408738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!