MojoからNumPyを使う
NumPyの配列と関数を呼び出します
「MojoからNumPyを使う」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎯
よくある質問
「MojoからNumPyを使う」レッスンは無料ですか?
はい。「MojoからNumPyを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「MojoからNumPyを使う」で何を学びますか?
NumPyの配列と関数を呼び出します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「MojoからNumPyを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- MojoからNumPyを使う
- MojoのデータをPythonに渡す
- Pythonオブジェクトを読み戻す
- 相互運用におけるパフォーマンスの注意点