What is the difference between np.array and np.asarray?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In the realm of numeric computing in Python, particularly when discussing the numpy library, two functions frequently come under comparison: `np.array()` and `np.asarray()`. At their core, both functions aim to convert input data into a numpy array. However, subtle differences between the two can significantly impact performance and behavior, and understanding these differences is essential for making informed decisions in array manipulation.
Technical Details
`np.array()`
The `np.array()` function is a versatile function designed to create a new array object from different types of input data such as lists, tuples, or nested collections. This function guarantees the creation of a new array, which means copying the input data into a newly allocated numpy array. This process can be memory-intensive, especially for large input data.
Key Parameters:
- `object`: The input data to be converted into an array.
- `dtype`: Desired data type for the array.
- `copy`: By default, this parameter is set to `True`, ensuring that the input is copied.
- `order`: Specifies the memory layout of the array (`'C'` for row-major or `'F'` for column-major).
`np.asarray()`
On the other hand, `np.asarray()` is designed to convert input data into an array without unnecessary copying. If the input data is already in the form of a numpy array with the desired `dtype`, `np.asarray()` will return an array referencing the original data. This behavior results in potential performance gains, especially when handling large datasets.
Key Parameters:
- `a`: The input data to be converted.
- `dtype`: Desired data type for the array.
Key Differences
To illustrate the distinctions between these functions, consider the following Python code examples:
- New Arrays: When you need to create a new array object regardless of the input's current state.
- Order Specification: When the memory layout (order) needs to be explicitly defined.
- Performance Optimization: When converting existing numpy arrays where memory efficiency is crucial.
- Non-Copy Conversion: When you want to maintain the existing input's memory if possible.
Related reading
- What is the difference between np.mean and tf.reduce_mean?
- What is the difference between np.mean and tf.reduce_mean?
- What is the difference between pylab and pyplot?
- What is the difference between supervised learning and unsupervised learning?
- What is the difference between Python's list methods append and extend?
- What is the difference between Set and List?
- What is the difference between null=True and blank=True in Django?
- What is the difference between nullTrue and blankTrue in Django?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.