ปรับรูปร่างและทำอาร์เรย์ให้แบน
เปลี่ยนมิติโดยไม่สูญเสียข้อมูล
ปรับรูปร่างและทำอาร์เรย์ให้แบน เป็นบทเรียน Data Science Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Data Science Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Data Science Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Same Data, New Shape
An array is just numbers plus a shape. Reshaping rearranges those numbers into new dimensions without copying or changing a single value.
Calling reshape
Use reshape to lay 12 numbers out as a 3 by 4 grid. The total count must stay the same, or NumPy raises an error.
import numpy as np
a = np.arange(12)
b = a.reshape(3, 4)Rows Times Columns
The new shape only works if the dimensions multiply to the original size. Here 3 times 4 equals 12, so the reshape fits perfectly.
Let NumPy Do the Math
Pass -1 for one dimension and NumPy computes it for you. It is a handy shortcut when you only know one side of the grid.
a = np.arange(12)
b = a.reshape(3, -1) # becomes 3 x 4Going to 1D With ravel
Need a flat line of numbers again? ravel collapses any array into a single dimension, reading values row by row.
grid = np.arange(6).reshape(2, 3)
flat = grid.ravel() # [0 1 2 3 4 5]flatten Makes a Copy
The flatten method also returns 1D, but always as a fresh copy. Editing the result never touches the original array.
flat = grid.flatten()
flat[0] = 99 # grid is untouchedravel May Share Memory
Unlike flatten, ravel often returns a view that shares memory with the source. Change the view and the original can change too.
Add a Dimension
A 1D array can grow into a column with reshape. Turning shape (3,) into (3, 1) is common before stacking or matrix math.
v = np.array([1, 2, 3])
col = v.reshape(3, 1)Transpose Flips Axes
The .T attribute swaps rows and columns. A 2 by 3 array instantly becomes 3 by 2, mirroring the data across its diagonal.
grid = np.arange(6).reshape(2, 3)
turned = grid.T # shape 3 x 2Shape Must Stay Consistent
You cannot reshape 10 numbers into a 3 by 4 grid. The element count on both sides must match, so plan dimensions before you reshape.
Why Reshaping Matters
Models and plotting tools expect specific shapes. Reshaping lets one block of numbers serve a vector, a matrix, or an image as needed.
Quick Check
You want a guaranteed independent 1D copy of an array.
Reshape Recap
You reshaped data while keeping its values, used -1 for auto sizing, and learned that flatten copies while ravel may share memory. 🎯
คำถามที่พบบ่อย
บทเรียน “ปรับรูปร่างและทำอาร์เรย์ให้แบน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ปรับรูปร่างและทำอาร์เรย์ให้แบน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ปรับรูปร่างและทำอาร์เรย์ให้แบน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Data Science Academy นี้ได้ไหม
ได้ บทเรียน Data Science Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ปรับรูปร่างและทำอาร์เรย์ให้แบน
- ผลรวม ค่าเฉลี่ย และเคล็ดลับแกน
- หน้ากากบูลีนสำหรับการเลือก
- ตัวเลขสุ่มและเมล็ดสุ่ม