NumPy | Multiply 2D Array to 1D Array Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Given two NumPy arrays, the task is to multiply a 2D array with a 1D array, each row corresponding to one element in NumPy. You can follow these methods to multiply a 1D array into a 2D array in NumPy: Using np.newaxis()Using axis as noneUsing transpose()Let's understand them better with Python program examples: Using np.newaxis()The np.newaxis() method of the NumPy library allows us to increase the dimension of an array by 1 dimension. We use this method to perform element-wise multiplication by reshaping the 1D array to have a second-dimension Example: Python3 import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) ini_array2 = np.array([0, 2, 3]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = ini_array1 * ini_array2[:, np.newaxis] # printing result print("New resulting array: ", result) Outputinitial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [[ 0 0 0] [ 4 8 10] [ 3 6 9]] Using axis as none We use None, to add a new axis to the 1D NumPy array. This reshapes the 1D array to a 2D array and allows us to multiply it with a 2D array. Example: Python3 # Python code to demonstrate # multiplication of 2d array # with 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) ini_array2 = np.array([0, 2, 3]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = ini_array1 * ini_array2[:, None] # printing result print("New resulting array: ", result) Outputinitial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [[ 0 0 0] [ 4 8 10] [ 3 6 9]] Using transposeUsing NumPy T attribute, we transpose the 2D array to multiply it with a 1D array and then transpose the resulting array to its original form. Example: Python3 # python code to demonstrate # multiplication of 2d array # with 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5], [1, 2, 3]]) ini_array2 = np.array([0, 2, 3]) # printing initial arrays print("initial array", str(ini_array1)) # Multiplying arrays result = (ini_array1.T * ini_array2).T # printing result print("New resulting array: ", result) Outputinitial array [[1 2 3] [2 4 5] [1 2 3]] New resulting array: [[ 0 0 0] [ 4 8 10] [ 3 6 9]] Comment More infoAdvertise with us Next Article NumPy | Multiply 2D Array to 1D Array garg_ak0109 Follow Improve Article Tags : Python Python-numpy Python numpy-program Practice Tags : python Similar Reads Convert a 1D array to a 2D Numpy array Here we will learn how to convert 1D NumPy to 2D NumPy Using two methods. Numpy is a Python package that consists of multidimensional array objects and a collection of operations or routines to perform various operations on the array and processing of the array. Convert a 1D array to a 2D Numpy arr 3 min read Python | Flatten a 2d numpy array into 1d array Given a 2d numpy array, the task is to flatten a 2d numpy array into a 1d array. Below are a few methods to solve the task. Method #1 : Using np.flatten() Python3 # Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4, 5] 2 min read Convert 2D float array to 2D int array in NumPy Converting a 2D float array to a 2D integer array in NumPy is a straightforward process using the astype() method. This conversion can be useful in various data analysis and scientific computing tasks where integer data types are required or where memory efficiency is essential. In this article, we 8 min read How to convert 1D array of tuples to 2D Numpy array? In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array. 2 min read numpy.multiply() in Python The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value). It returns the product of two input array element by element.Syntax:numpy.multiply(arr1, arr2, out=None, where=True, casting='same_kind', order='K', dtype=None 3 min read Python | Numpy numpy.ndarray.__imul__() With the help of numpy.ndarray.__imul__() method, we can multiply a particular value that is provided as a parameter in the ndarray.__imul__() method. Value will be multiplied to every element in a numpy array. Syntax: ndarray.__imul__($self, value, /)Return: self*=value Example #1 : In this example 1 min read How to get all 2D diagonals of a 3D NumPy array? Let's see the program for getting all 2D diagonals of a 3D NumPy array. So, for this we are using numpy.diagonal() function of NumPy library. This function return specified diagonals from an n-dimensional array. Syntax: numpy.diagonal(a, axis1, axis2)Parameters: a: represents array from which diag 3 min read Numpy - Iterating Over Arrays NumPy provides flexible and efficient ways to iterate over arrays of any dimensionality. For a one-dimensional array, iterating is straightforward and similar to iterating over a Python list.Let's understand with the help of an example:Pythonimport numpy as np # Create a 1D array arr = np.array([1, 3 min read Numpy - Array Creation Numpy Arrays are grid-like structures similar to lists in Python but optimized for numerical operations. The most straightforward way to create a NumPy array is by converting a regular Python list into an array using the np.array() function.Let's understand this with the help of an example:Pythonimp 5 min read Python | Numpy numpy.ndarray.__pos__() With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__p 1 min read Like