Numpy Articles

Page 2 of 81

How to iterate over Columns in Numpy

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 2K+ Views

NumPy provides several methods to iterate over columns in a 2D array. The most common approaches include using nditer() with transpose, array transpose directly, apply_along_axis(), and manual iteration with indexing. Syntax Here are the key functions used for column iteration ? np.nditer(array.T) # Iterator with transpose array.T # Array transpose np.apply_along_axis() # Apply function along axis array.shape[1] ...

Read More

How to choose elements from the list with different probability using NumPy?

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 1K+ Views

NumPy provides several methods to randomly choose elements from a list with different probabilities. The probability values must sum to 1.0. Here are three main approaches using NumPy's random module. Using numpy.random.choice() The choice() function randomly samples elements from a 1-D array with specified probabilities ? Syntax numpy.random.choice(a, size=None, replace=True, p=None) Parameters: a − Input array or list of elements size − Output shape (optional) replace − Whether sampling is with replacement (default: True) p − Probabilities for each element (must sum to 1) Example 1: 1-D Array ...

Read More

How to check whether the element of a given NumPy array is non-zero?

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 365 Views

NumPy provides several methods to check whether elements in an array are non-zero. Each approach serves different purposes: checking if all elements are non-zero, finding indices of non-zero elements, or counting non-zero elements. Using np.all() for Boolean Check The np.all() function checks if all elements in an array are non-zero (truthy). It returns True if all elements are non-zero, False otherwise ? import numpy as np arr = np.array([2, 5, 8, 11, 14, 17]) print("Original array:", arr) if np.all(arr): print("All elements are non-zero") else: print("Array contains ...

Read More

How to check whether specified values are present in NumPy array?

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 9K+ Views

NumPy provides several methods to check whether specified values are present in an array. The most common approaches are using the in keyword, np.isin() function, and np.where() function. Using the "in" Keyword The in keyword checks if a single element exists in the array ? import numpy as np arr = np.array([10, 30, 2, 40.3, 56, 456, 32, 4]) print("The Original array:", arr) if 4 in arr: print("The element 4 is present in the array.") else: print("The element 4 is not present in the array.") ...

Read More

Compute the outer product of two given vectors using NumPy in Python

Niharika Aitam
Niharika Aitam
Updated on 27-Mar-2026 416 Views

The outer product of two vectors is a matrix obtained by multiplying each element of the first vector with each element of the second vector. In NumPy, the outer product of vectors a and b is denoted as a ⊗ b. Outer Product Formula: a = [a₀, a₁, a₂, ...] b = [b₀, b₁, b₂, ...] a ⊗ b = a₀×b₀ a₀×b₁ a₀×b₂ a₁×b₀ a₁×b₁ a₁×b₂ a₂×b₀ a₂×b₁ a₂×b₂ ...

Read More

Singular Value Decomposition

Jaisshree
Jaisshree
Updated on 27-Mar-2026 666 Views

Singular Value Decomposition (SVD) is a powerful mathematical technique used in machine learning to analyze large and complex datasets. It decomposes a matrix into three simpler matrices, making it easier to understand patterns and reduce dimensionality. For any matrix A, SVD factorizes it as A = UΣVT, where: U contains the left singular vectors (eigenvectors of AAT) Σ is a diagonal matrix of singular values (square roots of eigenvalues) VT contains the right singular vectors (eigenvectors of ATA) Mathematical Algorithm The SVD computation follows these steps: Given matrix A, compute ATA (transpose of ...

Read More

How to Convert 1D Array of Tuples to 2D Numpy Array?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 6K+ Views

When working with data in Python, it is often necessary to transform and manipulate arrays to facilitate analysis and computations. One common scenario is converting a 1D array of tuples into a 2D NumPy array. This conversion allows for easier indexing, slicing, and applying vectorized operations. In this article, we'll explore three methods to convert a 1D array of tuples into a 2D NumPy array, each with its own advantages and use cases. Understanding the Data Structures 1D Array of Tuples A 1D array of tuples refers to a data structure where tuples are arranged sequentially ...

Read More

How to Convert a Numpy Array to Tensor?

Mukul Latiyan
Mukul Latiyan
Updated on 27-Mar-2026 7K+ Views

NumPy is a popular Python library used for numerical computing and scientific computing, providing a powerful array object for handling large and multi-dimensional arrays. However, when it comes to machine learning, deep learning, and neural networks, PyTorch is a widely used library that provides an efficient and flexible platform for building and training these models. While NumPy arrays and PyTorch tensors are similar in many ways, they have different properties and methods, which makes it necessary to convert a NumPy array to a PyTorch tensor when using PyTorch for machine learning applications. In this article, we will explore two ...

Read More

How to normalize a NumPy array so the values range exactly between 0 and 1?

Tarun Singh
Tarun Singh
Updated on 27-Mar-2026 4K+ Views

NumPy is a powerful library in Python for numerical computing that provides an array object for the efficient handling of large datasets. Often, it is necessary to normalize the values of a NumPy array to ensure they fall within a specific range. One common normalization technique is to scale the values between 0 and 1. In this article, we will learn how to normalize a NumPy array so the values range exactly between 0 and 1. We will explore different approaches that can be used to achieve this using NumPy and scikit-learn, along with syntax and complete examples. ...

Read More

How to Get Values of a NumPy Array at Certain Index Positions?

Way2Class
Way2Class
Updated on 27-Mar-2026 966 Views

NumPy provides powerful indexing capabilities to access specific values from arrays at certain positions. Whether working with 1D arrays or multidimensional arrays, understanding indexing is essential for data manipulation and analysis in Python. Syntax NumPy arrays use zero-based indexing with square brackets. For different array dimensions: 1D Array: array[index] 2D Array: array[row_index, column_index] 3D Array: array[depth, row, column] Basic 1D Array Indexing Access individual elements from a one-dimensional array using their index positions − import numpy as np # Create a 1D array numbers = np.array([10, 20, 30, 40, 50]) ...

Read More
Showing 11–20 of 802 articles
« Prev 1 2 3 4 5 81 Next »
Advertisements