NumPy | Multiply 2D Array to 1D Array Last Updated : 09 Feb, 2024 Summarize Comments Improve Suggest changes Share 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 G 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 Like