Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with Python NumPy Arrays

Tech May 19 3

Purpose and Performance Advantages

NumPy (Numerical Python) is an open-source library designed for manipulating arrays, alongside handling linear algebra, Fourier transforms, and matrices. While Python natively supports lists, lists become inefficient for large-scale numerical data processing. NumPy addresses this by offering performance up to 50 times faster than standard Python lists.

This speed boost comes from storing data in contiguous memory blocks, allowing rapid access and manipulation. Additionally, NumPy is optimized for modern multi-core and multi-threaded processors. Beyond scientific computing, it serves as a versatile container for general multidimensional data and supports custom data types for database integration.

Setting Up the Environment

To begin, ensure Python and pip are installed on your system. Install NumPy using pip:

pip install numpy

On certain Linux distributions like Ubuntu 24.04, pip might face permission issues. In such cases, use the system package manager:

sudo apt-get install python3-numpy -y

For Fedora-based systems, the pip method functions without issues.

Creating Basic Arrays

Import the library and assign it the standard alias np to make it available in your script:

import numpy as np

Construct a one-dimensional array using the array function:

data_points = np.array([10, 20, 30, 40, 50, 60, 70, 80])
print(data_points)

Executing this script will output:

[10 20 30 40 50 60 70 80]

Reshaping and Copying Arrays

When duplicating arrays, direct assignment (new_array = old_array) only creates a reference. Modifying the original array will alter the referenced copy as well. To create an independent duplicate, use np.copy().

The np.copy() function takes the target array as its primary argument, with optional order (controlling memory layout) and subok (dictating subclass preservation) parameters.

First, generate a structured array using arange and reshape. The elements generated must match the shape dimensions; for instance, a 3x2 matrix requires exactly 6 elements:

import numpy as np

source_matrix = np.arange(start=5, stop=11).reshape(3, 2)
reference_copy = source_matrix
deep_copy = np.copy(source_matrix)

print("Original Matrix:")
print(source_matrix)

# Change a value in the original
source_matrix[1, 0] = 999

print("\nAfter modifying source_matrix[1, 0]:")
print("Source Matrix:")
print(source_matrix)
print("Reference Copy (affected):")
print(reference_copy)
print("Deep Copy (unaffected):")
print(deep_copy)

Running the above code demonstrates that reference_copy reflects the change made to source_matrix because they point to the same memory location, while deep_copy retains its original state.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.