A General Introduction to Numpy
NumPy: the absolute basics for beginners
A General Introduction to Numpy
NumPy: the absolute basics for beginners
NumPy(Numerical Python) is a fundamental library for Python numerical computing. It provides efficient multi-dimensional array objects and various mathematical functions for handling large datasets, making it a critical tool for professionals in fields that require heavy computation.
In this article, we are going to walk through the basics of Numpy, which is an important skill to be mastered for Data Science.
Key Features of NumPy
NumPy has various features that make it popular over lists.
- N-Dimensional Arrays: NumPy’s core feature is
ndarray, an N-dimensional array object that supports homogeneous data types. - Arrays with High Performance: Arrays are stored in contiguous memory locations, enabling faster computations than Python lists (Please see Numpy Array vs Python List for details).
- Broadcasting: This allows element-wise computations between arrays of different shapes. It simplifies operations on arrays of various shapes by automatically aligning their dimensions without creating new data.
- Vectorization: Eliminates the need for explicit Python loops by applying operations directly on entire arrays.
- Linear algebra: NumPy contains routines for linear algebra operations, such as matrix multiplication, decompositions, and determinants.
Installing NumPy in Python
To begin using NumPy, you need to install it first. This can be done using the following pip command:
pip install numpy
Once installed, import the library with the alias np
import numpy as np
To understand well, we can group Numpy into 15 levels. Let’s jump into each level.
Level 1: Creating Numpy Arrays
We shall check how to create a NumPy array from a Python list. Arrays are the core data structure in NumPy and are optimized for numerical operations.
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
print(numbers)
[1 2 3 4 5]
Level 2: Arrays with Zeros and Ones
NumPy allows you to initialize with zeros and ones quickly. This is useful for placeholders and matrix multiplication.
import numpy as np
zero_array = np.zeros(4)
one_array = np.ones((2,3))
print(zero_array)
print(one_array)
[0. 0. 0. 0.]
[[1. 1. 1.]
[1. 1. 1.]]
Level 3: Random Number Arrays
Random arrays are useful for simulations, testing, and experiments. NumPy can generate random values within a given shape.
import numpy as np
random_values = np.random.rand(3, 2)
print(random_values)
[[0.93562587 0.17394292]
[0.71929706 0.62396122]
[0.2143406 0.08835186]]
Level 4: Reshaping Arrays
Reshaping changes the structure of an array without modifying its data. This is helpful when working with multidimensional data.
import numpy as np
data = np.array([2 , 4, 6, 8, 10, 12])
reshaped_data = data.reshape(3,2)
print(reshaped_data)
[[ 2 4]
[ 6 8]
[10 12]]
Level 5: Array Indexing and Slicing
Indexing and slicing allow you to access specific elements or ranges from an array. This is essential for data selection.
import numpy as np
values = np.array([5, 10, 15, 20, 25])
print(values[3])
print(values[-1:])
20
[25]
Level 6: Basic Mathematical Operations
NumPy supports element-wise operations between arrays of the same or compatible shapes.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a * b
print(result)
result = a + b
print(result)
[ 4 10 18]
[5 7 9]
Level 7: Statistical Operations
NumPy provides built-in statistical functions such as mean, median, and standard deviation for data analysis.
import numpy as np
scores = np.array([10, 20, 30, 40, 50])
print(np.mean(scores))
print(np.median(scores))
print(np.std(scores))
30.0
30.0
14.142135623730951
Level 8: Broadcasting
Broadcasting allows NumPy to perform operations between arrays of different shapes with automatically expanding dimensions.
import numpy as np
arr = np.array([1, 2, 3])
scaled = arr + 5
print(scaled)
[6 7 8]
Level 9: Boolean Indexing
Boolean indexing filters array elements based on conditions, making data selection simple and expressive.
import numpy as np
data = np.array([12, 18, 25, 30, 42])
filtered = data[data>20]
print(filtered)
[25 30 42]
Level 10: Vectorized Operations
Vectorization allows operations on entire arrays without loops resulting in faster and cleaner code.
import numpy as np
nums = np.array([1, 2, 3, 4])
result = np.sqrt(nums)
print(result)
[1. 1.41421356 1.73205081 2. ]
Level 11: Linear Algebra Operations
NumPy includes powerful linear algebra tools such as matrix multiplication and determinant calculation.
import numpy as np
matrix_a = np.array([[1, 2], [3, 4]])
matrix_b = np.array([[2, 0], [1, 2]])
product = np.dot(matrix_a, matrix_b)
determinant = np.linalg.det(matrix_a)
print(product)
print(determinant)
[[ 4 4]
[10 8]]
-2.0000000000000004
Level 12: Aggregation Functions
Aggregation functions summarize data using operations like sum, minimum, maximum, and cumulative sum.
import numpy as np
values = np.array([3, 6, 9, 12])
print(np.sum(values))
print(np.min(values))
print(np.max(values))
print(np.cumsum(values))
30
3
12
[ 3 9 18 30]
Level 13: Sorting and Searching
Sorting arranges data in order, while searching helps locate elements or their indices that match specific conditions.
import numpy as np
arr = np.array([45, 10, 30, 20])
sorted_arr = np.sort(arr)
indexes = np.where(arr> 25)
print(sorted_arr)
print(indexes)
[10 20 30 45]
(array([0, 2]),)
Level 14: Concatenation and Splitting
Arrays can be combined or split into smaller parts, which is useful for data processing and restructuring.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
combined = np.concatenate((x, y))
split_arrays = np.split(combined, 2)
print(combined)
print(split_arrays)
[1 2 3 4 5 6]
[array([1, 2, 3]), array([4, 5, 6])]
Level 15: Data Type Conversion
NumPy allows easy conversion of array data types, which is important for memory optimization and formatting.
import numpy as np
numbers = np.array([2.5, 4.8, 6.1])
int_numbers = numbers.astype(int)
string_numbers = int_numbers.astype(str)
print(int_numbers)
print(string_numbers)
[2 4 6]
['2' '4' '6']
NumPy and Machine Learning
NumPy is a core library used to work with numerical data in the machine learning domain. Datasets are represented by arrays, which make operations fast (based on arrays or vectors) and allow any operations that are performed in a typical linear algebra/statistics/data processing application.
Examples of operations accomplished through NumPy include normalization of datasets, calculation of loss functions, and updating gradient values.
The libraries used to build machine learning models, including scikit-learn, TensorFlow, and PyTorch, are modeled after many of the same principles as NumPy. For this reason, mastering NumPy is a critical first step for anyone entering machine learning, as it provides the performance and functionality necessary to work with data at scale.
That’s a Wrap!:
I believe this article has given a general overview of what NumPy is in Python. I have tried to write this article in a simple manner so that even a beginner can understand. I hope you have liked it.
Reference:
Connect With Me
You can also connect with me on Twitter, Kaggle, and LinkedIn.
Feel free to hold down the clap button 👏 (you can clap up to 50 times!) to help others find this article. What are your thoughts? Let me know in the responses!
Cheers,
Samith Chimminiyan
메타데이터
- post_id
- 030508fea207
- slug
- a-general-introduction-to-numpy-030508fea207
- url
- https://medium.com/@samithc/a-general-introduction-to-numpy-030508fea207
- canonical_url
- https://medium.com/@samithc/a-general-introduction-to-numpy-030508fea207
- author_url
- https://medium.com/@samithc
- status
- ok
- fetched_at
- 2026-06-15 20:49:13