Array’s are the foundation for all data science in Python. Arrays can be multidimensional, & all elements in an array need to be of the same type, all integers or all floats.
Advantages of Using an Array
- Arrays can handle very large datasets efficiently
- Computationally-memory efficient
- Faster calculations & analysis than lists
- Diverse functionality (many functions in Python packages). With several Python packages that make trend modeling, statistics, & visualization easier
Basics of an Array
In Python, we can create new datatypes, called arrays using the NumPy package. NumPy arrays are optimized for numerical analyses & contain only a single data type.
We first import NumPy & then use the
array() function to create an array. The
array()function takes a list as an input.
The type of
my_array is a
In the below example, we will convert a list to an array using the
array() function from NumPy. We will create a list
a_list comprising of integers. Then, using the
array() function, convert it an array.
Example of Creating an Array
Example of an Array Operation
In the below example, we add two numpy arrays. The result is an element-wise sum of both the arrays.
Example of Array Indexing
We can select a specific index element of an array using indexing notation.
We can also slice a range of elements using the slicing notation specifying a range of indices.
Note: When people say arrays in Python, more often than not, they are talking about Python lists. If that's the case…