Why use numpy.array () on numpy.arange ()? Isn't that redundant?

In my Numpy / Pandas tutorial, I am taught to do data with the following code:

sample_numpy_data = np.array(np.arange(24)).reshape((6,4))

      

Isn't it redundant to use np.array () in np.arange () since np.arange () is already creating an array?

Why is this necessary? Example?

Example found in Lynda's "Pandas for Data Science" Pandas Overview - Operations course.

+3


source to share


1 answer


This is not only redundant but introduces unnecessary overhead because it makes a copy of the array by default .



If you are not sure if something is an array (perhaps because it is an argument to a function), you can use np.asarray

on it. This copies if it is not an array.

+4


source







All Articles