Numpy array from existing dataNumPy provides us the way to create an array by using the existing data. numpy.asarrayThis routine is used to create an array by using the existing data in the form of lists, or tuples. This routine is useful in the scenario where we need to convert a python sequence into the numpy array object. The syntax to use the asarray() routine is given below. It accepts the following parameters.
Example: creating numpy array using the listOutput: <class 'numpy.ndarray'> [1 2 3 4 5 6 7] Example: creating a numpy array using TupleOutput: <class 'numpy.ndarray'> [1 2 3 4 5 6 7] Example: creating a numpy array using more than one listOutput: <class 'numpy.ndarray'> [list([1, 2, 3, 4, 5, 6, 7]) list([8, 9])] numpy.frombufferThis function is used to create an array by using the specified buffer. The syntax to use this buffer is given below. It accepts the following parameters.
ExampleOutput: <class 'bytes'> [b'h' b'e' b'l' b'l' b'o' b' ' b'w' b'o' b'r' b'l' b'd'] <class 'numpy.ndarray'> numpy.fromiterThis routine is used to create a ndarray by using an iterable object. It returns a one-dimensional ndarray object. The syntax is given below. It accepts the following parameters.
ExampleOutput: [0. 2. 4. 6.] <class 'numpy.ndarray'> Next TopicNumpy Arrays within the numerical range |