In this Numpy article we want to learn about Python Numpy Tutorial, so Python is powerful and popular programming language and it offers different libraries for different tasks, one of them are Numpy, and it is one of the most fundamental and powerful libraries for numerical computing in Python. NumPy provides high performance multidimensional array object, along with a collection of functions for performing efficient mathematical operations.
First of all we need to import the required libraries
1 |
pip install numpy |
Creating NumPy Arrays
NumPy core functionality revolves around its powerful array object, called ndarray. We can create arrays using different methods, such as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import numpy as np # Create 1D array arr1d = np.array([1, 2, 3, 4, 5]) print(arr1d) # Create 2D array arr2d = np.array([[1, 2, 3], [4, 5, 6]]) print(arr2d) # Create an array of zeros zeros = np.zeros((2, 3)) print(zeros) # Create an array of ones ones = np.ones((3, 2)) print(ones) # Create an array with a range of values range_arr = np.arange(0, 10, 2) print(range_arr) |
This will be the result
Numpy Array Manipulation
NumPy provides several functions for manipulating arrays, including reshaping, slicing and concatenation. Let’s explore some of these operations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import numpy as np # Reshape an array arr = np.array([1, 2, 3, 4, 5, 6]) reshaped_arr = arr.reshape((2, 3)) # Accessing array elements element = arr[0] slice_arr = arr[1:4] # Concatenating arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) concatenated_arr = np.concatenate((arr1, arr2)) |
Numpy Mathematical Operations
NumPy simplifies mathematical operations on arrays by providing builtin functions that operate element-wise. This enables us to perform operations without explicitly writing loops. Let’s see some examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import numpy as np # Reshape an array arr = np.array([1, 2, 3, 4, 5, 6]) reshaped_arr = arr.reshape((2, 3)) # Accessing array elements element = arr[0] slice_arr = arr[1:4] # Concatenating arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) concatenated_arr = np.concatenate((arr1, arr2)) |
Numpy Broadcasting
NumPy broadcasting feature allows mathematical operations between arrays of different shapes and sizes. It automatically aligns the dimensions to perform element-wise operations. Let’s see this example.
1 2 3 4 5 6 7 |
import numpy as np arr = np.array([1, 2, 3]) scalar = 2 broadcasted_arr = arr * scalar print(broadcasted_arr) |
This will be the result
Numpy Random Number Generation
NumPy provides functions to generate random numbers efficiently. This is useful for simulations, testing and generating data for different applications. Let’s generate a random array:
1 |
random_arr = np.random.rand(3, 3) |
Learn More on Python Numpy
- Python Numpy for Machine Learning
- How to Install Numpy
- Working with Linear Algebra in Numpy
- Numpy vs Pandas
- Advance Python Numpy Techniques
- Numpy Array Indexing and Slicing
- Numpy Mathematical Functions
- Numpy Random Number Generation in Python
- Working with Multi-Dimensional Arrays in Numpy
Subscribe and Get Free Video Courses & Articles in your Email