0Pricing
Pandas & NumPy Academy · 강의

NumPy 배열 만들기

np.array, np.zeros, np.ones, np.arange로 1차원 및 2차원 배열을 만들고 shape과 dtype을 확인합니다.

NumPy 배열 만들기은(는) CoddyKit의 무료 Pandas & NumPy Academy 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Pandas & NumPy Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What Is a NumPy Array?

NumPy is Python's go-to library for number crunching. Its core is the ndarray — a grid of same-typed values that's far faster and leaner than a list.

import numpy as np

# NumPy arrays are fast, typed, and memory-efficient
print(np.__version__)

Creating Arrays from Lists

The simplest way to make an array: pass a list to np.array(). NumPy picks the dtype for you, and a list of lists becomes a 2-D matrix.

import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a)          # [1 2 3 4 5]
print(a.dtype)    # int64

m = np.array([[1, 2, 3], [4, 5, 6]])
print(m.shape)    # (2, 3)

np.zeros and np.ones

Need a blank slate? np.zeros fills an array with 0.0 and np.ones fills it with 1.0 — perfect for setting up an array before you compute its real values.

import numpy as np

z = np.zeros(5)
print(z)          # [0. 0. 0. 0. 0.]

o = np.ones((3, 4))
print(o.shape)    # (3, 4)
print(o.dtype)    # float64

np.arange for Integer Sequences

np.arange works like Python's range but returns an array. Give it a start, stop, and step — the stop value isn't included.

import numpy as np

a = np.arange(0, 10, 2)
print(a)   # [0 2 4 6 8]

b = np.arange(1.0, 2.0, 0.25)
print(b)   # [1.   1.25 1.5  1.75]

np.linspace for Evenly-Spaced Floats

Want evenly spaced floats? np.linspace gives you a set number of values between two endpoints, both included — no rounding drift.

import numpy as np

x = np.linspace(0, 1, 5)
print(x)   # [0.   0.25 0.5  0.75 1.  ]

y = np.linspace(0, 360, 7)
print(y)   # [  0.  60. 120. 180. 240. 300. 360.]

np.full and np.eye

Use np.full to fill an array with any constant you like, and np.eye to build an identity matrix — ones on the diagonal, zeros everywhere else.

import numpy as np

f = np.full((2, 3), 7)
print(f)
# [[7 7 7]
#  [7 7 7]]

I = np.eye(3)
print(I)
# [[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]

Specifying dtype at Creation

Every array has one dtype for all its values. Set it yourself with dtype= — picking float32 over float64, for example, can cut memory in half.

import numpy as np

a = np.array([1, 2, 3], dtype=np.float32)
print(a.dtype)     # float32
print(a.nbytes)    # 12  (3 elements x 4 bytes)

b = np.zeros(4, dtype=np.int32)
print(b.dtype)     # int32

Random Arrays with np.random

Need random data? np.random generates it for you. Set a seed first so you get the same numbers every run — key for repeatable experiments. 🎲

import numpy as np

np.random.seed(42)
u = np.random.rand(3, 3)   # uniform [0, 1)
print(u)

n = np.random.randn(5)     # standard normal
print(n.round(2))

Checking Array Shape and Dimensions

After making an array, you'll usually check its shape — a tuple of sizes per dimension. Use .ndim to see how many dimensions it has.

import numpy as np

a = np.arange(12)
print(a.shape)   # (12,)
print(a.ndim)    # 1

m = a.reshape(3, 4)
print(m.shape)   # (3, 4)
print(m.ndim)    # 2

Converting Lists and Tuples

np.array() takes more than lists — tuples, ranges, even other arrays all work. Just avoid ragged inner lists, which kill NumPy's speed.

import numpy as np

# From tuple
t = np.array((10, 20, 30))
print(t)   # [10 20 30]

# From range
r = np.array(range(5))
print(r)   # [0 1 2 3 4]

# Nested lists -> 2D
m = np.array([[1, 2], [3, 4], [5, 6]])
print(m.shape)  # (3, 2)

np.empty for Uninitialised Arrays

np.empty grabs memory without filling it, so it's a touch faster than zeros. The catch: it holds leftover garbage — always write before you read.

import numpy as np

buf = np.empty(5)
# Values are garbage — do NOT read before writing
for i in range(5):
    buf[i] = i * 2
print(buf)  # [0. 2. 4. 6. 8.]

Quick Check

Test your understanding of NumPy array creation from this lesson.

Lesson Recap

Nice work! You can turn lists into arrays with np.array(), and build them from scratch with zeros, ones, arange, and linspace. Next up: array attributes.

자주 묻는 질문

“NumPy 배열 만들기” 강의는 무료인가요?

네 — “NumPy 배열 만들기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Pandas & NumPy Academy 강의 전체를 잠금 해제할 수 있습니다. Pandas & NumPy Academy 강의에는 총 4개의 강의가 포함되어 있습니다.

“NumPy 배열 만들기”에서 뭘 배우나요?

np.array, np.zeros, np.ones, np.arange로 1차원 및 2차원 배열을 만들고 shape과 dtype을 확인합니다. 브라우저에서 직접 실행하는 실습 코드로 Pandas & NumPy Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Pandas & NumPy Academy을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Pandas & NumPy Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“NumPy 배열 만들기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Pandas & NumPy Academy 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Pandas & NumPy Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. NumPy 배열 만들기
  2. 배열 속성과 검사
  3. 원소별 산술 연산
  4. 배열 슬라이싱과 인덱싱
← Pandas & NumPy Academy(으)로 돌아가기