zeros, ones, empty(๊ถ์ฅ), arange, linspace ๋ฑ์ ํจ์๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ์์ฑํ ์ ์์ต๋๋ค!
import numpy as np
# zeros() : ํฌ๊ธฐ๊ฐ ์ ํด์ ธ ์๋ ๋ชจ๋ ๊ฐ์ด 0์ธ ๋ฐฐ์ด ์์ฑ, ์ธ์๋ ๋ฐฐ์ด์ ํฌ๊ธฐ
z_ar = np.zeros(5)
print('zeros array 1d \n', z_ar, z_ar.dtype)
# ๋ค์ฐจ์ ๋ฐฐ์ด: ์ธ์์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ ํํ ์ฌ์ฉ
z_2dar = np.zeros((2, 3))
print('zeros array 2d \n', z_2dar, z_2dar.dtype)
# ์๋ฃํ ์
๋ ฅ ์ง์
dt_z_ar = np.zeros((5, 2), dtype='i')
print('zeros array dt \n', dt_z_ar, dt_z_ar.dtype)
str_z_ar = np.zeros(5, dtype='U')
print('zeros array str \n', str_z_ar, str_z_ar.dtype)
# ๋ฐ์ดํฐ ๋ฃ๊ธฐ... ํ๋๋ง ๋ค์ด๊ฐ๋ค
str_z_ar[0] = 'teststr'
print(str_z_ar)
#-----------------------------------------------------------------------
print('-' * 100)
o_ar = np.ones((2, 3, 4), dtype='i8')
print('ones array \n', o_ar, o_ar.dtype)
# ํฌ๊ธฐ๋ฅผ ๋ช
์ํ์ง ์๊ณ ๋ค๋ฅธ ๋ฐฐ์ด๊ณผ ๊ฐ์ ํฌ๊ธฐ๋ฅผ ๋ง๋๋ ๊ฒฝ์ฐ
ol_ar = np.ones_like(o_ar, dtype='f')
zl_ar = np.ones_like(z_2dar, dtype='f')
print(ol_ar, '\n', zl_ar)
#-----------------------------------------------------------------------
print('-' * 100)
e_ar = np.empty((4, 3))
print(e_ar)
#-----------------------------------------------------------------------
print('-' * 100)
aran1 = np.arange(10)
print(aran1)
aran2 = np.arange(3, 21, 2) # ์์, ๋, ๊ฐ๊ฒฉ
print(aran2)
#-----------------------------------------------------------------------
print('-' * 100)
lin_sp = np.linspace(0, 100, 5) # ์์, ๋. ๊ฐ์
print(lin_sp)
[์ฝ๋ ์คํ ๊ฒฐ๊ณผ]
zeros array 1d
[0. 0. 0. 0. 0.] float64
zeros array 2d
[[0. 0. 0.]
[0. 0. 0.]] float64
zeros array dt
[[0 0]
[0 0]
[0 0]
[0 0]
[0 0]] int32
zeros array str
['' '' '' '' ''] ['t' '' '' '' '']
----------------------------------------------------------------------------------------------------
ones 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]]] int64
[[[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.]]
----------------------------------------------------------------------------------------------------
[[0.0078125 0.0078125 0.0078125]
[0.0078125 0.0078125 0.0078125]
[0.0078125 0.0078125 0.0078125]
[0.0078125 0.0078125 0.0078125]]
----------------------------------------------------------------------------------------------------
[0 1 2 3 4 5 6 7 8 9]
[ 3 5 7 9 11 13 15 17 19]
----------------------------------------------------------------------------------------------------
[ 0. 25. 50. 75. 100.]
Process finished with exit code 0