02_numpy

始めるnumpyの

インポート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()

PNG

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

まず、ndarray作成

Pythonのリストによって作成1.使用np.array()

パラメータのリスト:
[1、4、2、5、3]

注意:

  • すべての要素が同じであるndarrayデフォルトをnumpyのタイプ
  • STR>フロート> 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を使用してルーチンを作成します。

これは、作成、次の一般的な方法が含まれています。

1)np.ones(形状、DTYPE =なし、順序= '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(形状、DTYPE =フロート、順序= '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(形状、fill_value、DTYPE =なし、順序= '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 =なし、K = 0、DTYPE =フロート)
対角と他の位置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()NUM = 50、エンドポイント=真、retstep = Falseを、DTYPE =なし、停止、開始

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(停止、[開始] [ステップ] DTYPE =なし)

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(ロー、ハイ=なし、サイズ=なし、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)

PNG

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

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()

PNG

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、スケール= 1.0、サイズ=なし)

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(サイズ=なし)

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:寸法
形状:形状(各次元の長さ)
サイズ:全長

DTYPE:要素タイプ

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

ndarrayの第三に、基本的な操作

1.インデックス

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.スライス

1次元のリストと完全に一致して
多次元共感

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]])

:: 2スライス

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を使用し、引数がタプルであることに注意してください!

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()
    に注意するカスケードポイント:
  • カスケードは、パラメータのリストです:括弧または大括弧を追加してください
  • 外形寸法は同じでなければなりません
  • 形状に準拠
  • [強調]デフォルトのカスケードの方向は、タプルによって表される第1の寸法値の方向を形作ります
  • カスケード軸方向パラメータによって変化させることができます
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()

PNG

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

PNG

5.セグメンテーション

そして、カスケードのような、3つの完全なセグメンテーション仕事関数:

  • 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()

PNG

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

PNG

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])

のコピーを作成するためにコピー()関数を使用します

四、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
ナンいない番号

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

アクションファイル

president_heights.csvパンダを使用してファイルを開き、
データファイルを取得します

import pandas as pd
data = pd.read_csv('president_heights.csv')
type(data)
data
注文 高さ(cm)
0 1 ジョージ・ワシントン 189
1 2 ジョン・アダムス 170
2 3 トーマス・ジェファーソン 189
3 4 ジェームズ・マディソン 163
4 5 ジェームズ・モンロー 183
5 6 ジョン・クィンシー・アダムズ 171
6 7 アンドリュー・ジャクソン 185
7 8 マーティン・ヴァン・ビューレン 168
8 9 ウィリアム・ハリソン 173
9 10 ジョン・タイラー 183
10 11 ジェームズ・ポーク 173
11 12 ザカリー・テイラー 173
12 13 ミラード・フィルモア 175
13 14 フランクリン・ピアース 178
14 15 ジェームズ・ブキャナン 183
15 16 アブラハムリンカーン 193
16 17 アンドリュー・ジョンソン 178
17 18 ユリシーズ・グラント 173
18 19 ラザフォード・ヘイズ 174
19 20 ジェームズ・ガーフィールド 183
20 21 チェスター・A・アーサー 183
21 23 ベンジャミン・ハリソン 168
22 25 ウィリアム・マッキンリー 170
23 26 セオドア・ルーズベルト 178
24 27 ウィリアム・タフト 182
25 28 ウッドロー・ウィルソン 180
26 29 ウォレン・ハーディング 183
27 30 カルビン・クーリッジ 178
28 31 ハーバート・フーバー 182
29 32 フランクリン・D・ルーズベルト 188
30 33 ハリーS.トルーマン 175
31 34 アイゼンハワー 179
32 35 ジョンF.ケネディ 183
33 36 リンドン・ジョンソン 193
34 37 リチャード・ニクソン 182
35 38 ジェラルド・フォード 183
36 39 ジミー・カーター 177
37 40 ロナルド・レーガン 185
38 41 ジョージHWブッシュ 188
39 42 ビル・クリントン 188
40 43 ジョージ・W・ブッシュ 182
41 44 バラック・オバマ 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を補います
  • ルール番号2:不足している要素は、既存の値で満たされていると仮定

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

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:
= np.arange(3).reshape((3,1))
B = np.arange(3)
求+ 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)
+ Bを見つけます。

六、ndarrayソート

クイズ:
ndarrayオブジェクトの選択ソートのための上記の研究の知見numpyの使用。

デフ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])

おすすめ

転載: www.cnblogs.com/pankypan/p/11408738.html