การใช้ NumPy จาก Mojo
เรียกใช้อาร์เรย์และฟังก์ชันของ NumPy
การใช้ NumPy จาก Mojo เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
NumPy, From Mojo
NumPy powers most Python number-crunching, and Mojo lets you call it directly, so you keep that ecosystem while writing fast code. 🔢
Import It First
Bring NumPy in the same way you import any Python module: call Python.import_module with the name "numpy".
var np = Python.import_module("numpy")Make an Array
With the handle in hand, call NumPy functions through dot access. Here you build an array from a Python list of numbers.
var a = np.array([1, 2, 3])Use Array Functions
Every NumPy creator works: np.zeros and np.ones build filled arrays, perfect for setting up buffers before computation.
var z = np.zeros(5)Math on Arrays
Call NumPy math functions just like in Python. np.sum adds every element of an array and hands back the total.
var total = np.sum(a)Whole-Array Ops
NumPy shines at vectorized math: one call like np.sqrt works on the entire array at once, no manual loop needed.
var roots = np.sqrt(a)Reading Shape
Access an array's attributes through dot syntax. The shape attribute tells you the dimensions of your array.
var dims = a.shapeIt May Raise
Calls across the bridge can fail, so NumPy work belongs in a function marked raises to handle any error cleanly.
fn run() raises:
var np = Python.import_module("numpy")Same API You Know
The NumPy API is identical to Python's: if you know np.linspace or np.dot, you already know how to call them from Mojo.
var grid = np.linspace(0, 1, 10)Build, Then Use
A common pattern is to create an array, then reshape it. np.reshape reorganizes the same data into new dimensions for you.
var m = np.reshape(a, (3, 1))Best of Both
You get NumPy's mature, battle-tested routines while orchestrating them from Mojo, blending convenience with Mojo's performance mindset. ⚡
Quick Check
Recall how you start using NumPy inside a Mojo program.
Recap
You imported NumPy with Python.import_module, then built arrays and ran vectorized math using the very same API you know from Python. 🎯
คำถามที่พบบ่อย
บทเรียน “การใช้ NumPy จาก Mojo” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การใช้ NumPy จาก Mojo” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การใช้ NumPy จาก Mojo”
เรียกใช้อาร์เรย์และฟังก์ชันของ NumPy คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การใช้ NumPy จาก Mojo” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การใช้ NumPy จาก Mojo
- การส่งข้อมูล Mojo ไปยัง Python
- การอ่านออบเจ็กต์ Python กลับมา
- จุดที่ต้องระวังด้านประสิทธิภาพของการทำงานร่วมกัน