1. 程式人生 > >一、numpy入門

一、numpy入門

常用函數 [1] counter bcb launch save unique port a + b

Array

import numpy as np
# create from python list
list_1 = [1, 2, 3, 4]
array_1 = np.array(list_1)#array([1, 2, 3, 4])
list_2 = [5, 6, 7, 8]
array_2 = np.array([list_1, list_2])

  結果:

技術分享圖片
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
View Code
array_2.shape#(2, 4)
array_2.size#8
array_2.dtype#dtype(‘int64‘)
array_3 = np.array([[1.0,2,3],[4.0,5,6]])
array_3.dtype#dtype(‘float64‘)

array_4 = np.arange(1, 10, 2)#array([1, 3, 5, 7, 9])
np.zeros(5)#array([ 0.,  0.,  0.,  0.,  0.])

  

np.zeros([2,3])

  結果:

技術分享圖片
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
View Code
np.eye(5)

  結果:

技術分享圖片
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.]])
View Code
np.eye(5).dtype#dtype(‘float64‘)

  

a = np.arange(1,10)#array([1, 2, 3, 4, 5, 6, 7, 8, 9])
a[1]#2
a[1:5]#array([2, 3, 4, 5])

  

b = np.array([[1,2,3],[4,5,6]])
b

  結果:

技術分享圖片
array([[1, 2, 3],
       [4, 5, 6]])
View Code
b[1][0]#4
b[1,0]#4

  

c = np.array([[1,2,3],[4,5,6],[7,8,9]])
c

  結果:

技術分享圖片
array([[1, 2, 3],
       [4, 5, 6],
       [
7, 8, 9]])
View Code
c[:2,1:]

  結果:

技術分享圖片
array([[2, 3],
       [5, 6]])
View Code

數組與矩陣運算

快速創建數組

import numpy as np
np.random.randn(10)

  結果:

技術分享圖片
array([ 0.28906593,  1.4302902 ,  1.10346334,  0.11146373, -0.47497452,
        0.88859371,  0.18953089, -0.65780036, -2.06789973, -1.45679231])
View Code
np.random.randint(10, size=20).reshape(4, 5)

  結果:

技術分享圖片
array([[8, 5, 8, 4, 5],
       [4, 5, 2, 8, 3],
       [3, 6, 9, 7, 3],
       [3, 0, 4, 7, 0]])
View Code

數組運算

a = np.random.randint(10, size=20).reshape(4, 5)
b = np.random.randint(10, size=20).reshape(4, 5)
a
b

  結果:

技術分享圖片
array([[3, 6, 5, 9, 3],
       [3, 3, 4, 6, 8],
       [3, 3, 2, 3, 4],
       [6, 0, 9, 7, 9]])

array([[1, 5, 6, 0, 6],
       [0, 2, 1, 7, 9],
       [7, 6, 1, 3, 8],
       [4, 4, 3, 1, 0]])
View Code
a + b

  結果:

技術分享圖片
array([[ 4, 11, 11,  9,  9],
       [ 3,  5,  5, 13, 17],
       [10,  9,  3,  6, 12],
       [10,  4, 12,  8,  9]])
View Code
a-b

  結果:

技術分享圖片
array([[ 2,  1, -1,  9, -3],
       [ 3,  1,  3, -1, -1],
       [-4, -3,  1,  0, -4],
       [ 2, -4,  6,  6,  9]])
View Code
a * b

  結果:

技術分享圖片
array([[ 3, 30, 30,  0, 18],
       [ 0,  6,  4, 42, 72],
       [21, 18,  2,  9, 32],
       [24,  0, 27,  7,  0]])
View Code
a / b

  結果:

技術分享圖片
 RuntimeWarning: divide by zero encountered in true_divide
  """Entry point for launching an IPython kernel.

array([[ 3.        ,  1.2       ,  0.83333333,         inf,  0.5       ],
       [        inf,  1.5       ,  4.        ,  0.85714286,  0.88888889],
       [ 0.42857143,  0.5       ,  2.        ,  1.        ,  0.5       ],
       [ 1.5       ,  0.        ,  3.        ,  7.        ,         inf]])
View Code
np.mat([[1,2,3],[4,5,6]])

  結果:

技術分享圖片
matrix([[1, 2, 3],
        [4, 5, 6]])
View Code
a

  結果:

技術分享圖片
array([[3, 6, 5, 9, 3],
       [3, 3, 4, 6, 8],
       [3, 3, 2, 3, 4],
       [6, 0, 9, 7, 9]])
View Code
np.mat(a)

  結果:

技術分享圖片
matrix([[3, 6, 5, 9, 3],
        [3, 3, 4, 6, 8],
        [3, 3, 2, 3, 4],
        [6, 0, 9, 7, 9]])
View Code

矩陣的運算

A = np.mat(a)
B = np.mat(b)
A
B

  結果:

技術分享圖片
matrix([[3, 6, 5, 9, 3],
        [3, 3, 4, 6, 8],
        [3, 3, 2, 3, 4],
        [6, 0, 9, 7, 9]])

matrix([[1, 5, 6, 0, 6],
        [0, 2, 1, 7, 9],
        [7, 6, 1, 3, 8],
        [4, 4, 3, 1, 0]])
View Code
A + B
A - B

  結果:

技術分享圖片
matrix([[ 4, 11, 11,  9,  9],
        [ 3,  5,  5, 13, 17],
        [10,  9,  3,  6, 12],
        [10,  4, 12,  8,  9]])

matrix([[ 2,  1, -1,  9, -3],
        [ 3,  1,  3, -1, -1],
        [-4, -3,  1,  0, -4],
        [ 2, -4,  6,  6,  9]])
View Code
A * B

  結果: ValueError: shapes (4,5) and (4,5) not aligned: 5 (dim 1) != 4 (dim 0)

a = np.mat(np.random.randint(10, size=20).reshape(4, 5))
b = np.mat(np.random.randint(10, size=20).reshape(5, 4))
a
b
a * b

  結果:

技術分享圖片
matrix([[4, 4, 3, 2, 7],
        [4, 7, 2, 4, 5],
        [8, 6, 6, 1, 0],
        [5, 9, 6, 2, 8]])

matrix([[5, 8, 9, 2],
        [8, 4, 3, 7],
        [4, 6, 7, 0],
        [5, 8, 5, 3],
        [0, 6, 9, 5]])

matrix([[ 74, 124, 142,  77],
        [104, 134, 136,  94],
        [117, 132, 137,  61],
        [131, 176, 196, 119]])
View Code

Array常用函數

a = np.random.randint(10, size=20).reshape(4, 5)
np.unique(a)#array([0, 1, 2, 3, 4, 5, 6, 8, 9])
a

  結果:

技術分享圖片
array([[4, 1, 2, 5, 3],
       [9, 8, 1, 4, 0],
       [5, 4, 8, 0, 2],
       [8, 6, 2, 4, 3]])
View Code
sum(a)#array([26, 19, 13, 13,  8])
sum(a[0])#15
sum(a[:,0])#26
a.max()#9
max(a[0])#5
max(a[:,0])#9

Array的input和output

使用pickle序列化numpy array

import pickle
import numpy as np
x = np.arange(10)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
f = open(‘x.pkl‘, ‘wb‘)
pickle.dump(x, f)

  

f = open(‘x.pkl‘, ‘rb‘)
pickle.load(f)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

  

np.save(‘one_array‘, x)

  

np.load(‘one_array.npy‘)#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

  

y = np.arange(20)#array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19])
np.savez(‘two_array.npz‘, a=x, b=y)

  

c = np.load(‘two_array.npz‘)
c[‘a‘]#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
c[‘b‘]#array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19])

  

一、numpy入門