Python Numpy基本構文


リファレンス


https://www.youtube.com/watch?v=QUT1VHiLmmI&list=WL&index=3&ab_channel=freeCodeCamp.org

Accessing/Changing specific elements, rows, columns, etc

import numpy as np

a=np.arange(1,15) # 1부터 15까지 리스트 나열
print(a)

array1=a.reshape(2,7) # 2행 7열 reshape

print(array1)
# Getting a little more fancy [startindex:endindex:stepsize]

array1[0,1:6:2] #1행에서 1부터6까지 offset 2

"""
array([2, 4, 6])
"""

行列の特定の値の変更

a[1,5]=20 #2행 6열에 있는 숫자를 20으로 변경
print(a)

a[:,2]=5 #3열에 있는 수들 전부 5으로 변경
print(a)

a[:,2]=[1,5] #3열에 있는 수 1,5로 변경
print(a)

a[1,:]=10 #2열에 있는 수 전부 10으로 변경
print(a)

# 3D 예제

# Get specific element (work outside in)

b_1=b[0,1,1] #1번째 행렬, 2열, 2번째 index 값 반환
print(b_1)

b_2=b[:,1,:] #1,2 행렬, 2열, 모든 index 값 반환
print(b_2)

Entirely matrix renumbering

import numpy as np
#All 0s matrix
np.zeros((2,3))
"""
array([[0., 0., 0.],
       [0., 0., 0.]])
"""

#All 1s matrix
np.ones((4,2,2), dtype='int32')
"""
array([[[1, 1],
        [1, 1]],

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

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

       [[1, 1],
        [1, 1]]])
"""
#Any other numbber
np.full((2,2),99, dtype='int32')

#Any other number (full_like)
a=np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
np.full(a.shape.4)
#or np.full_like(a,4)

"""
array([[4, 4, 4, 4, 4, 4, 4],
       [4, 4, 4, 4, 4, 4, 4]])
"""
#Random decimal numbers
np.rand.ran(4,2)
"""
array([[0.48215004, 0.53942106],
       [0.77732571, 0.38774874],
       [0.40803556, 0.82104631],
       [0.76070086, 0.57890891]])
"""
#Random integer values
np.random.randint(7.size=3,3))

#The identity matrix
np.identity(3)
"""
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
"""

#repeating elements
arr=np.array([[1,2,3]])
r1=np.repeat(arr,3,axis=0)
print(r1)
"""
[[1 2 3]
 [1 2 3]
 [1 2 3]]
"""

マトリクスを構築します


output=np.ones((5,5),dtype='int32')
print(output)

z=np.zeros((3,3),dtype='int32')
z[1,1]=9
print(z)

#first to the third row, first to the third column
output[1:4,1:4] =z 
print(output)

Be careful when copying arrays!

a=np.array([1,2,3])
b=a
b[0]=100
print(b)
print(a) #a가 [1,2,3]이 아닌 [100,2,3]이 출력된다!
a=np.array([1,2,3])
b=a.copy()
b[0]=100

print(b)
print(a)
"""
[100   2   3]
[1 2 3]
"""

三角関数も使えます!

# Take the sin
np.cos(a)
np.sin(a)

Linear Algebra

a=np.ones((2,3)) # 2*3 행렬에 모든 원소 1
print(a)

b=np.full((3,2),2) #3*2 행렬에 모든 원소 2
print(b)

np.matmul(a,b) # 그냥 a*b는 행렬 크기들이 달라 에러남

"""
[[1. 1. 1.]
 [1. 1. 1.]]
[[2 2]
 [2 2]
 [2 2]]
array([[6., 6.],
       [6., 6.]])
"""
# Find the determinant

c=np.identity(3)
print(c)
np.linalg.det(c)

"""
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
"""

Statistics

stats=np.array([[1,2,3],[4,5,6]])
"""
array([[1, 2, 3],
       [4, 5, 6]])
"""
# returning minimum values in first row & second row
np.min(stats, axis=1)
"""
array([1, 4])
"""
# returning minimum values in first row
np.min(stats, axis=0)
"""
array([1,2,3])
"""

Reorganizing Arrays

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

after = before.reshape((8,1))
print(after)

Vertically Stacking vectors

# Vertically stacking vectors
v1=np.array([1,2,3,4])
v2=np.array([5,6,7,8])

np.vstack([v1,v2,v1,v2])
"""
array([[1, 2, 3, 4],
       [5, 6, 7, 8],
       [1, 2, 3, 4],
       [5, 6, 7, 8]])
"""
# Horizontal stacking vectors
# Horizontal stacking vectors
h1=np.array([[1,2,3,4],[5,6,7,8]])
h2=np.array([[5,6,7,8],[1,2,3,4]])

np.hstack([h1,h2])

'''
array([[1, 2, 3, 4, 5, 6, 7, 8],
       [5, 6, 7, 8, 1, 2, 3, 4]])
'''

に質問


a=np.arange(1,30+1).reshape(6,5)
print(a)
"""
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]
 [26 27 28 29 30]]
"""
a[[0,1,2,3],[1,2,3,4]] #0~3 행, 1,2,3,4 열 값 출력
a[[0,4,5],3:] # 0,4,5 번째 행, 3~ 끝까지 열 값 출력