NumPy Basics

    Yash Kabra4 min readUpdated

    NumPy is the library that gives Python fast, multi-dimensional arrays and the mathematical operations to work on them. Almost every machine learning tool in Python — including Pandas and scikit-learn — is built on top of NumPy arrays. Learning NumPy is the first practical step after basic Python for anyone heading into AI.

    Why not just use Python lists?

    Python lists are flexible but slow for maths, and they do not support whole-array operations directly. NumPy's array stores numbers of one type in a compact block of memory and runs operations in fast lower-level code. The result is both faster and simpler to write: you operate on entire arrays at once instead of looping element by element.

    import numpy as np
    
    # A NumPy array from a Python list.
    a = np.array([1, 2, 3, 4])
    
    # Whole-array maths, no loop needed. This is "vectorisation".
    print(a * 2)      # [2 4 6 8]
    print(a + 10)     # [11 12 13 14]
    print(a.sum())    # 10
    

    That last idea — applying an operation to a whole array at once — is called vectorisation, and it is the habit that makes NumPy code both fast and readable.

    Creating arrays

    There are several handy ways to make arrays without typing every value:

    import numpy as np
    
    np.zeros(3)            # [0. 0. 0.]
    np.ones((2, 2))        # 2x2 array of ones
    np.arange(0, 10, 2)    # [0 2 4 6 8] — like range(), as an array
    np.linspace(0, 1, 5)   # 5 evenly spaced values from 0 to 1
    

    Shape: the size of each dimension

    Every array has a shape, a tuple giving its size along each dimension. A 1-D array of 4 numbers has shape (4,); a table of 2 rows and 3 columns has shape (2, 3). Shape matters constantly in machine learning, where data is usually a 2-D array of "one row per example, one column per feature".

    import numpy as np
    
    m = np.array([[1, 2, 3],
                  [4, 5, 6]])
    
    print(m.shape)   # (2, 3): 2 rows, 3 columns
    print(m.ndim)    # 2: number of dimensions
    print(m.size)    # 6: total number of elements
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Indexing and slicing

    You can read and write parts of an array with square brackets. For 2-D arrays, give a row and a column.

    import numpy as np
    
    m = np.array([[10, 20, 30],
                  [40, 50, 60]])
    
    print(m[0, 1])     # 20: row 0, column 1
    print(m[:, 0])     # [10 40]: every row, column 0
    print(m[1])        # [40 50 60]: the whole second row
    
    # Boolean indexing: keep only values above 25.
    print(m[m > 25])   # [30 40 50 60]
    

    Boolean indexing — selecting elements that meet a condition — is especially useful for filtering data.

    Vectorised maths across arrays

    NumPy lets you combine arrays element by element, and it can "broadcast" a smaller array across a larger one when their shapes are compatible.

    import numpy as np
    
    prices = np.array([100, 200, 300])
    tax_rate = 0.18
    
    # Multiply every price by the tax rate in one step (broadcasting).
    tax = prices * tax_rate
    print(tax)   # [18. 36. 54.]
    

    This avoids slow Python loops and is the standard way numerical code is written.

    Common mistakes

    • Confusing shape (4,) with (4, 1). A 1-D array and a single-column 2-D array behave differently. Many scikit-learn functions expect 2-D inputs, so reshape when needed with a.reshape(-1, 1).
    • Expecting copies but getting views. A slice often shares memory with the original; changing it can change the original. Use .copy() when you need an independent array.
    • Mixing data types. A NumPy array holds one type. Putting a string into a number array can silently convert everything to strings.
    • Looping when you could vectorise. If you find yourself writing a for loop over array elements, there is usually a faster whole-array operation.

    FAQ

    Do I need NumPy if I use Pandas? Pandas is built on NumPy and exposes NumPy arrays underneath, so understanding NumPy makes Pandas clearer. See Pandas basics.

    Is NumPy hard to learn? The basics — arrays, shapes, indexing, vectorised maths — come quickly with practice. Advanced features can wait.

    Why does shape matter so much in ML? Models expect inputs in a specific shape (usually 2-D). Shape errors are one of the most common beginner bugs.

    Keep learning

    Explore the full AI & machine learning hub for more guides, or join the waitlist for our structured Artificial Intelligence course at Infoplanet, Jalgaon, to build your NumPy and ML skills with mentor support.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Yash Kabra

    Founder, Atlee Technologies

    Yash Kabra is the founder of Atlee Technologies, a product studio that ships SaaS products end-to-end. He owns products from strategy through launch and growth — including Infoplanet, TrackRise and Perqee — and teaches AI, Machine Learning and Data Science at Infoplanet with a focus on how these tools are used to build real products.

    Related guides