NumPy Essentials Tips with Python

NumPy Essentials Tips With Python

NumPy Essentials Tips With Python

NumPy is the main library for numeric work in Python. AI, data science, and machine learning depend on it. This guide shows essential tips to help you use NumPy with a clean and fast workflow.

1. Creating Arrays

Use np.array() to create arrays.

import numpy as np

a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])
  • Use lists for simple arrays.
  • Use nested lists for matrices.

2. Check Shape and Size

Shape tells the structure. Size tells the number of elements.

print(b.shape)   # (2, 2)
print(b.size)    # 4

Always check shape before model training or matrix operations.

3. Useful Array Creation Functions

  • np.zeros(). array of zeros
  • np.ones(). array of ones
  • np.arange(). range of numbers
  • np.linspace(). evenly spaced numbers
  • np.eye(). identity matrix
z = np.zeros((3, 3))
r = np.arange(0, 10, 2)

4. Indexing and Slicing

Indexing helps you access elements. Slicing helps you extract parts of arrays.

x = np.array([10, 20, 30, 40, 50])

print(x[0])       
print(x[1:4])     
print(x[:3])      
print(x[::2])     

Indexing in 2D

m = np.array([[5, 6], [7, 8], [9, 10]])

print(m[0, 1])    
print(m[:, 0])    
print(m[1:, :])   

5. Vectorized Operations

NumPy operations apply to all elements. No need for manual loops.

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b)
print(a * b)
print(a ** 2)

Vectorization improves speed and simplifies code.

6. Matrix Operations

Matrix work is common in AI.

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])

print(A.dot(B))
print(np.matmul(A, B))
  • dot and matmul perform matrix multiplication.

7. Broadcasting

Broadcasting lets NumPy combine arrays with different shapes when possible.

a = np.array([1, 2, 3])
b = 2

print(a * b)      

This reduces code and increases speed.

8. Boolean Masking

Masking filters arrays using conditions.

x = np.array([3, 8, 1, 9, 4])

mask = x > 5
print(x[mask])    

Useful for cleaning data or selecting features.

9. Aggregation Functions

NumPy offers fast summary tools.

arr = np.array([2, 4, 6, 8])

print(arr.sum())
print(arr.mean())
print(arr.min())
print(arr.max())
print(arr.std())

10. Reshape Arrays

Reshape changes array form without changing values.

v = np.array([1, 2, 3, 4, 5, 6])
m = v.reshape(2, 3)

Reshape is important for neural networks, CNNs, and model inputs.

11. Stacking and Concatenation

Stack arrays vertically or horizontally.

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(np.vstack([a, b]))
print(np.hstack([a, b]))

12. Random Module

NumPy includes tools for random generation.

np.random.seed(0)
print(np.random.rand(3))
print(np.random.randint(0, 10, size=5))

Useful for model testing and reproducibility.

13. Performance Tips

  • Replace loops with vectorized operations.
  • Use astype() to control data types.
  • Use copy() when needed to avoid shared memory issues.
  • Always confirm shapes before matrix ops.

14. Mini Projects With NumPy

Project 1. Normalize a Dataset

  • Load numeric array.
  • Subtract mean.
  • Divide by standard deviation.

Project 2. Implement Simple Distance Function

  • Create two arrays.
  • Compute Euclidean distance using vector operations.

Project 3. Build a Small Matrix Calculator

  • Take two arrays.
  • Perform multiply, add, subtract, and transpose.

NumPy Essentials in Moroccan Darija

NumPy kay3awnk tkhddem b arrays b speed kbir. F AI daba, NumPy howa base. Had tips kay3awnouk tebni code n9i w rapide.

  • np.array() bach tsawb arrays.
  • .shape bach tchouf structure.
  • Indexing bach taccessi values.
  • Vectorization bach tkhadam bla loops.
  • Matrix ops bach tdir multiplication.
  • Masking bach tfiltri data.
  • Reshape bach tbadal form.

Ila fhamti had essentials, t9der tebni models w data pipelines bla t3qid.

Conclusion

NumPy offers fast numeric work for AI and data tasks. Learn arrays, shapes, indexing, vectorization, and matrix operations. These tips build strong skills for machine learning, neural networks, and data science projects.

Share:

Ai With Darija

Discover expert tutorials, guides, and projects in machine learning, deep learning, AI, and large language models . start learning to boot your carrer growth in IT تعرّف على دروس وتوتوريالات ، ومشاريع فـ الماشين ليرنين، الديب ليرنين، الذكاء الاصطناعي، والنماذج اللغوية الكبيرة. بّدا التعلّم باش تزيد تقدم فـ المسار ديالك فـ مجال المعلومات.

Blog Archive