×

numpy.broadcast_to() in Python

numpy.broadcast_to() in Python

The numpy.broadcast_to() function broadcasts an array to a new shape.

Syntax

numpy.broadcast_to(array, shape, subok=False)

Parameter

The numpy.broadcast_to() function has three parameters which are as follows: 

array: This parameter represents the array to broadcast.

shape: It signifies the shape of the desired array.

subok:  It is an optional parameter which takes Boolean value. If True, then sub-classes will be passed-through, else the returned array will be forced to be a base-class array (default).

Return

This function returns a read-only view on the original array with the given shape. It is typically not contiguous. 

Example 1

#Python Program explaining
#numpy.broadcast_to() function
import numpy as np
array = np.array([2,3,4])
print ('The original array:')
print (array,"\n")
print ('Applying the broadcast_to function:')
print (np.broadcast_to(array, (3, 3)))

Output

The original array:
[2 3 4]
Applying the broadcast_to() function:
[[2 3 4]
[2 3 4]
[2 3 4]]

Related Topics

numpy.ascontiguousarray() in Python with Example

numpy.ascontiguousarray() in Python The ascontiguousarray() returns a contiguous array (ndim>= 1) in memory (C order). Syntax numpy.ascontiguousarray(a, dtype=None) Parameter arr : This parameter includes the input data, in any form that can be converted to an array....

1 minute read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.

numpy.rollaxis() in Python

numpy.rollaxis() in Python The numpy.rollaxis() function rolls the given axis backwards until it lies in the specified position. Syntax numpy.rollaxis(a, axis, start=0) Parameter The numpy. rollaxis() method consists of three parameters, which are as follows: a : It represents an...

1 minute read.

Numpy Attributes

Numpy Attributes The Numpy collections give attributes to the numpy arrays. These attributes assist us with knowing the shape, aspect, and different properties of a given numpy array. As we know,...

4 minutes read.

NumPy vs SciPy

NumPy- NumPy is the most important Python package for scientific computing. It's a Python library that includes a multidimensional array object, derived objects (like masked arrays and matrices), and a...

3 minutes read.

numpy.broadcast_to() in Python

numpy.broadcast_to() in Python The numpy.broadcast_to() function broadcasts an array to a new shape. Syntax numpy.broadcast_to(array, shape, subok=False) Parameter The numpy.broadcast_to() function has three parameters which are as follows:  array: This parameter represents the array to broadcast. shape: It signifies the shape...

1 minute read.

numpy.ndarray.flatten() in Python

numpy.ndarray.flatten() in Python The numpy.ndarray.flatten() returns a copy of the array collapsed into 1-dimension. Syntax ndarray.flatten(order='C') Parameter The numpy. ndarray.flatten() method consists of one parameter, which is as follows: order : This parameter can be either...

1 minute read.

Read numpy array

Numpy is a numerical python that deals with multi-dimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library offers a multidimensional...

3 minutes read.

numpy.asscalar() in Python

The numpy.asscalar() function converts an array of size 1 to its scalar equivalent. Syntax numpy.asscalar(a) Parameter a: This parameter represents an input array of size 1. Return This function returns a scalar representation of parameter ‘a’. The output...

1 minute read.

numpy.hstack() in Python

numpy.hstack() in Python The numpy.hstack() function stacks the arrays in a sequence horizontally (column wise). Syntax numpy.hstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape, except 1-D...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.

numpy.fromstring () in Python

numpy.fromstring () in Python The fromstring() function of Python numpy class creates a new 1-D array initialized from raw binary or text data in a string. Syntax numpy.fromstring(string, dtype=float, count=-1, sep='') Parameter The numpy.fromstring() method...

1 minute read.

numpy.ones() in Python

numpy.ones() in Python The ones() method of Python numpy class returns a new array of given shape and type, filled with ones. Syntax numpy.ones(shape, dtype=None, order='C') Parameter The numpy.ones() method consists of three parameters, which are as...

1 minute read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

Python NumPy Tutorial

NumPy is a scientific library in Python Programming Language. It provides objects and routines for fast operations on arrays, random simulations, statistical operations, sorting, etc. Numpy has an ndarray object which...

2 minutes read.

numpy.reshape() in Python

numpy.reshape() in Python The numpy.reshape() function gives a new shape to an array without changing its data. Syntax numpy.reshape(a, newshape, order='C') Parameter The numpy.reshape() method consists of three parameters, which are as follows: array : It represents our...

1 minute read.

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

1 minute read.

numpy.full() in Python

numpy.full() in Python The full() method of Python numpy class returns a new array of specified shape and type, filled with fill_value. Syntax numpy.full(shape, fill_value, dtype=None, order='C') Parameter shape:This parameter represents the number of rows order :The order parameter can...

1 minute read.