Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the numpy.asarray() function in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The numpy.asarray() function in Python converts an input to an array.

Syntax

numpy.asarray(a, dtype=None)

Parameter value

The numpy.asarray() function takes the following parameter values:

  • a: This represents the input data converted to an array. It could be a list, lists of tuples, tuples of lists, and arrays.
  • dtype: This represents the data type of the desired array. By default, the data type of the array is determined from the input data. This is optional.

Return value

The numpy.asarray() function returns the array interpretation of the input data passed to it.

Code

import numpy as np
# creating an input data
x = [1, 2, 3, 4, 5]
# converting the input data to an array
myarray = np.asarray(x, dtype = np.double)
print(myarray)
Implementing the numpy.asarray() function

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an input data variable, x, with five elements.
  • Line 7: We convert the input data x to a float type array using the numpy.asarray() function. The output is assigned to a variable, myarray.
  • Line 9: We print the myarray variable.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring