Fundamentals 21 min read

Mastering NumPy: From Arrays to Advanced Operations in Python

This comprehensive guide walks through NumPy fundamentals—creating ndarrays, setting data types, performing vectorized arithmetic, indexing, slicing, boolean and fancy indexing, transposition, ufuncs, where, statistical functions, linear algebra, random generation, reshaping, and array splitting/concatenation—illustrated with concrete code examples and step‑by‑step explanations.

AI Large-Model Wave and Transformation Guide
AI Large-Model Wave and Transformation Guide
AI Large-Model Wave and Transformation Guide
Mastering NumPy: From Arrays to Advanced Operations in Python

1. NumPy Overview

NumPy (Numerical Python) introduces the ndarray object, a multi‑dimensional array that supports vectorized operations, fast computation, and memory efficiency, plus a rich library of mathematical functions for array and matrix calculations.

2. Creating ndarrays

An ndarray is an N‑dimensional array where all elements share the same type. Key attributes are ndim (number of dimensions), shape (size of each dimension), and dtype (data type).

# -*- coding: utf-8 -*-
import numpy

print 'Using a list to create a 1‑D array'
data = [1,2,3,4,5,6]
x = numpy.array(data)
print x               # [1 2 3 4 5 6]
print x.dtype         # int64

print 'Using a list to create a 2‑D array'
data = [[1,2],[3,4],[5,6]]
x = numpy.array(data)
print x               # [[1 2]
                      #  [3 4]
                      #  [5 6]]
print x.ndim          # 2
print x.shape         # (3, 2)

print 'Creating arrays with zeros/ones/empty'
print numpy.zeros(6)               # [0. 0. 0. 0. 0. 0.]
print numpy.zeros((2,3))          # [[0. 0. 0.]
                                 #  [0. 0. 0.]]
print numpy.ones((2,3))           # [[1. 1. 1.]
                                 #  [1. 1. 1.]]
print numpy.empty((3,3))          # uninitialized values

print 'Generating sequences with arange'
print numpy.arange(6)             # [0 1 2 3 4 5]
print numpy.arange(0,6,2)          # [0 2 4]

3. Specifying element types

print 'Creating an array with a specific dtype'
x = numpy.array([1,2.6,3], dtype=numpy.int64)
print x        # [1 2 3]
print x.dtype  # int64

x = numpy.array([1,2,3], dtype=numpy.float64)
print x        # [1. 2. 3.]
print x.dtype  # float64

print 'Copying and converting types with astype()'
x = numpy.array([1,2.6,3], dtype=numpy.float64)
y = x.astype(numpy.int32)
print y        # [1 2 3]
print x        # [1.  2.6 3. ]

z = y.astype(numpy.float64)
print z        # [1. 2. 3.]

print 'Converting string arrays to numeric'
x = numpy.array(['1','2','3'], dtype=numpy.string_)
y = x.astype(numpy.int32)
print x        # ['1' '2' '3']
print y        # [1 2 3]

4. Vectorized calculations

print 'Array and scalar operations'
x = numpy.array([1,2,3])
print x*2          # [2 4 6]
print x>2          # [False False  True]

y = numpy.array([3,4,5])
print x+y          # [4 6 8]
print x>y          # [False False False]

5. Basic indexing and slicing

One‑dimensional indexing mirrors Python lists; multi‑dimensional indexing uses arr[row_slice, col_slice] or arr[row, col]. The colon : selects an entire dimension.

# Basic indexing
x = numpy.array([[1,2],[3,4],[5,6]])
print x[0]          # [1 2]
print x[0,1]        # 2

# Copy vs view
x = numpy.array([[[1,2],[3,4]],[[5,6],[7,8]]])
y = x[0].copy()   # copy
z = x[0]           # view
print y             # [[1 2]
                    #  [3 4]]
y[0,0] = 0
z[0,0] = -1
print x             # [[[-1  2]
                    #   [ 3  4]]
                    #  [[5 6]
                    #   [7 8]]]

# Slicing
x = numpy.array([1,2,3,4,5])
print x[1:3]        # [2 3]
print x[:3]         # [1 2 3]
print x[1:]         # [2 3 4 5]
print x[0:4:2]      # [1 3]

x = numpy.array([[1,2],[3,4],[5,6]])
print x[:2]        # [[1 2]
                    #  [3 4]]
print x[:2,:1]      # [[1]
                    #  [3]]

x[:2,:1] = 0        # scalar broadcast assignment
print x             # [[0 2]
                    #  [0 4]
                    #  [5 6]]

x[:2,:1] = [[8],[6]]
print x             # [[8 2]
                    #  [6 4]
                    #  [5 6]]

6. Boolean and fancy indexing

# Boolean indexing
x = numpy.array([3,2,3,1,3,0])
y = numpy.array([True,False,True,False,True,False])
print x[y]          # [3 3 3]
print x[y==False]   # [2 1 0]
print x>=3          # [ True False  True False  True False]
print x[~(x>=3)]    # [2 1 0]
print (x==2)|(x==1) # [False  True False  True False False]
print x[(x==2)|(x==1)] # [2 1]

x[(x==2)|(x==1)] = 0
print x             # [3 0 3 0 3 0]

# Fancy indexing with integer arrays
x = numpy.array([1,2,3,4,5,6])
print x[[0,1,2]]    # [1 2 3]
print x[[-1,-2,-3]] # [6 5 4]

x = numpy.array([[1,2],[3,4],[5,6]])
print x[[0,1]]      # [[1 2]
                    #  [3 4]]
print x[[0,1],[0,1]]# [1 4]
print x[numpy.ix_([0,1],[0,1])] # [[1 2]
                                 #  [3 4]]

x[[0,1],[0,1]] = [0,0]
print x             # [[0 2]
                    #  [3 0]
                    #  [5 6]]

7. Transpose and axis swapping

k = numpy.arange(9)
m = k.reshape((3,3))
print m.T            # transpose
print numpy.dot(m,m.T)  # matrix product

k = numpy.arange(8).reshape(2,2,2)
print k.transpose((1,0,2))  # swap first two axes
print k.swapaxes(0,1)       # same result

m = numpy.arange(9).reshape((3,3))
print m.swapaxes(1,0)        # another transpose

8. Universal functions (ufuncs)

Ufuncs apply element‑wise operations.

# Unary ufuncs
x = numpy.arange(6)
print numpy.square(x)   # [ 0  1  4  9 16 25]

x = numpy.array([1.5,1.6,1.7,1.8])
frac, intpart = numpy.modf(x)
print frac   # [0.5 0.6 0.7 0.8]
print intpart# [1. 1. 1. 1.]

# Binary ufuncs
x = numpy.array([[1,4],[6,7]])
y = numpy.array([[2,3],[5,8]])
print numpy.maximum(x,y) # [[2 4]
                         #  [6 8]]
print numpy.minimum(x,y) # [[1 3]
                         #  [5 7]]

9. The where function

cond = numpy.array([True,False,True,False])
print numpy.where(cond, -2, 2)   # [-2  2 -2  2]

cond = numpy.array([1,2,3,4])
print numpy.where(cond>2, -2, 2)   # [ 2  2 -2 -2]

x = numpy.where(cond>2, numpy.array([-1,-2,-3,-4]), numpy.array([1,2,3,4]))
print x                         # [1 2 -3 -4]

# Nested where
x = numpy.where(cond>5, numpy.zeros(6), numpy.where(cond>2, numpy.array([-1,-2,-3,-4,-5,-6]), numpy.array([1,2,3,4,5,6])))
print x                         # [ 1.  2. -3. -4. -5.  0.]

10. Common statistical methods

x = numpy.array([[1,2],[3,3],[1,2]])
print x.mean()            # 2.0
print x.mean(axis=1)     # [1.5 3.  1.5]
print x.sum()            # 12
print x.max()            # 3
print x.cumsum()         # [1 3 6 9 10 12]

Boolean‑array statistics:

sum – counts True values

any – checks if any element is True all – checks if all elements are

True
x = numpy.array([[True,False],[True,False]])
print x.sum()        # 2
print x.any(axis=0)  # [True False]
print x.all(axis=1)  # [False False]

11. Sorting

x = numpy.array([[1,6,2],[6,1,3],[1,5,2]])
x.sort(axis=1)      # in‑place sort per row
print x               # [[1 2 6]
                       #  [1 3 6]
                       #  [1 2 5]]
# Non‑in‑place: numpy.sort(x) returns a sorted copy

12. Uniqueness and set operations

x = numpy.array([[1,6,2],[6,1,3],[1,5,2]])
print numpy.unique(x)      # [1 2 3 5 6]
print numpy.in1d(x, [1,6,5]) # boolean mask
print numpy.setdiff1d(x, [1,6,5]) # [2 3]
print numpy.intersect1d(x, [1,6,5]) # [1 5 6]

13. Linear algebra (numpy.linalg)

import numpy.linalg as nla
x = numpy.array([[1,2],[3,4]])
y = numpy.array([[1,3],[2,4]])
print x.dot(y)          # [[5 11]
                        #  [11 25]]
print nla.inv(x)        # [[ 2. -1.]
                        #  [-1.  1.]]
print nla.det(x)        # -2.0

14. Random number generation

import numpy.random as npr
x = npr.randint(0,2,size=100000)   # coin flip simulation
print (x>0).sum()   # number of heads
print npr.normal(size=(2,2))  # 2x2 array of normal values

15. Reshaping arrays

x = numpy.arange(0,6)
print x.reshape((2,3))   # [[0 1 2]
                         #  [3 4 5]]
print x.reshape((2,3)).reshape((3,2)) # [[0 1]
                                        #  [2 3]
                                        #  [4 5]]
print x.ravel()          # view, changes affect original
x.ravel()[0] = -1
print x                 # [[-1 1 2]
                        #  [3 4 5]]
print numpy.arange(15).reshape((5,-1)) # automatic column count -> (5,3)

16. Splitting and concatenating arrays

x = numpy.array([[1,2,3],[4,5,6]])
y = numpy.array([[7,8,9],[10,11,12]])
print numpy.concatenate([x,y], axis=0)  # vertical stack
print numpy.concatenate([x,y], axis=1)  # horizontal stack
print numpy.vstack((x,y))
print numpy.hstack((x,y))
print numpy.split(x,2,axis=0)          # split rows
print numpy.split(x,3,axis=1)          # split columns

import numpy as np
arr = np.arange(6)
arr1 = arr.reshape((3,2))
arr2 = np.random.randn(3,2)
print np.r_[arr1, arr2]   # row‑wise stacking
print np.c_[np.r_[arr1, arr2], arr]
print np.c_[1:6, -10:-5]

17. Repeating elements

x = numpy.array([[1,2],[3,4]])
print x.repeat(2)          # repeat each element
print x.repeat(2,axis=0)   # repeat rows
print x.repeat(2,axis=1)   # repeat columns

x = numpy.array([1,2])
print numpy.tile(x,2)       # [1 2 1 2]
print numpy.tile(x,(2,2))   # [[1 2 1 2]
                           #  [1 2 1 2]]
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonArrayTutorialNumPyDataScienceScientificComputing
AI Large-Model Wave and Transformation Guide
Written by

AI Large-Model Wave and Transformation Guide

Focuses on the latest large-model trends, applications, technical architectures, and related information.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.