0Pricing
Data Science Academy · บทเรียน

การทำดัชนีและการตัดแบ่งอาร์เรย์

ดึงแถว คอลัมน์ และช่วงข้อมูล

การทำดัชนีและการตัดแบ่งอาร์เรย์ เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Grab the Pieces You Need

Real analysis means pulling out parts of your data. NumPy's indexing lets you grab single values, rows, columns, or ranges with ease. ✂️

Single Elements by Position

Just like a list, you reach one element by its index. Counting starts at zero, so arr[0] is the very first value.

arr = np.array([10, 20, 30])
arr[0]  # 10

Count From the End

Negative indices count backward. arr[-1] hands you the last element without needing to know the array's length.

arr[-1]  # 30

Slices Grab a Range

A slice uses start:stop to pull a range. The start is included, the stop is not, just like Python lists.

np.arange(10)[2:5]  # 2 3 4

Steps Inside a Slice

Add a third number for a step. Using ::2 keeps every other element, an easy way to thin out data.

np.arange(10)[::2]  # 0 2 4 6 8

Two Dimensions, One Bracket

For a 2D array, give row and column in one bracket: arr[row, col]. This is cleaner than chaining two sets of brackets.

m = np.array([[1, 2], [3, 4]])
m[1, 0]  # 3

Grab a Whole Row

A lone colon means take everything along that axis. So m[0, :] returns the entire first row.

m[0, :]  # array([1, 2])

Grab a Whole Column

Flip it to pull a column. m[:, 0] sweeps every row and keeps just the first column's values.

m[:, 0]  # array([1, 3])

Slices Are Views, Not Copies

Careful: a slice is a view into the original. Change the slice and the source array changes too, which can surprise you.

s = arr[0:2]
s[0] = 99  # arr also changes

Make a Real Copy

When you need independence, call copy. It hands back a separate array so edits never touch the original.

safe = arr[0:2].copy()

Slice to Assign, Too

Indexing works on the left side as well. Assign to a slice and you update a whole range in one move.

arr[0:2] = 0  # first two become 0

Quick Check

Let's test selecting a column from a 2D array.

Recap

You can now carve up arrays: index single values, slice ranges with steps, target rows and columns, and copy when you need to stay safe. 🎉

คำถามที่พบบ่อย

บทเรียน “การทำดัชนีและการตัดแบ่งอาร์เรย์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การทำดัชนีและการตัดแบ่งอาร์เรย์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Data Science Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การทำดัชนีและการตัดแบ่งอาร์เรย์”

ดึงแถว คอลัมน์ และช่วงข้อมูล คุณปฏิบัติ Data Science Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Data Science Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Data Science Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การทำดัชนีและการตัดแบ่งอาร์เรย์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม

ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. จากรายการ Python สู่ ndarray
  2. รูปร่าง ขนาด และ dtype
  3. คณิตศาสตร์แบบเวกเตอร์โดยไม่ใช้ลูป
  4. การทำดัชนีและการตัดแบ่งอาร์เรย์
← กลับไปที่ Data Science Academy