0Pricing
Pandas & NumPy Academy · درس

إنشاء مصفوفات NumPy

أنشئ مصفوفات أحادية وثنائية الأبعاد باستخدام np.array وnp.zeros وnp.ones وnp.arange، ثم افحص shape وdtype الخاصين بها.

إنشاء مصفوفات NumPy درس مجاني في Pandas & NumPy Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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) وفتح باقي دورة Pandas & NumPy Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Pandas & NumPy Academy 4 دروس في المجموع.

ماذا ستتعلم في «إنشاء مصفوفات NumPy»؟

أنشئ مصفوفات أحادية وثنائية الأبعاد باستخدام np.array وnp.zeros وnp.ones وnp.arange، ثم افحص shape وdtype الخاصين بها. تتمرن على Pandas & NumPy Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Pandas & NumPy Academy؟

لا تُشترط خبرة سابقة. Pandas & NumPy Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «إنشاء مصفوفات NumPy»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Pandas & NumPy Academy هذا؟

نعم. كل درس في Pandas & NumPy Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنشاء مصفوفات NumPy
  2. سمات المصفوفات وفحصها
  3. الحسابات عنصرًا بعنصر
  4. تقطيع المصفوفات وفهرستها
← العودة إلى Pandas & NumPy Academy