Creating arrays
Arrays can be created in a number of ways, for instance from other data structures, by reading files on disk, or from the Web. For the purposes of this chapter, whose aim is to familiarize us with the core characteristics of a NumPy array, we will be creating arrays using lists or various NumPy functions.
Creating arrays from lists
The simplest way to create an array is using the array
function. To create a valid array object, arguments to array functions need to adhere to at least one of the following conditions:
- It has to be a valid iterable value or sequence, which may be nested
- It must have an
__array__
method that returns a valid numpy array
Consider the following snippet:
In [32]: x = np.array([1, 2, 3]) In [33]: y = np.array(['hello', ...
Get NumPy Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.