DEV Community

Cover image for Numpy Array Object
Lohith
Lohith

Posted on • Edited on

Numpy Array Object

Understanding NumPy Arrays: Homogeneous Multidimensional Data Structures.

  1. Multidimensional Homogeneous Array:

    • A NumPy array is a fundamental object that represents a multidimensional, homogeneous data structure.
    • "Homogeneous" means that all elements in the array have the same data type (e.g., all integers, all floats, etc.).
  2. Comparison with Python Lists:

    • Unlike Python lists, which can contain elements of different types, a NumPy array enforces a consistent data type for all its elements.
  3. Dimensions and Axes:

    • The dimension of a NumPy array refers to the number of axes it has.
    • For example:
      • A 1-D array (e.g., [1, 2, 3]) behaves like a single axis with three elements. Its length is 3.
      • A 2-D array (e.g., [[1.0, 2.0, 3.0], [4.0, 6.0, 7.0]]) has two axes:
      • The first axis (axis=0) has a length of 2 (i.e., two rows).
      • The second axis (axis=1) has a length of 3 (i.e., three columns).

Creating the NumPy array:

In NumPy, we create N-D arrays using the array() function by passing Python lists or tuples.

Here is the example for 1-D array:

import numpy as np

# Using a tuple to create a 1D NumPy array
array_from_tuple = np.array((1, 2, 3, 4, 5))

# Using a list to create a 1D NumPy array
array_from_list = np.array([6, 7, 8, 9, 10])

print("Array from tuple:", array_from_tuple)
print("Array from list:", array_from_list)
Enter fullscreen mode Exit fullscreen mode
Output:
Array from tuple: [1 2 3 4 5]
Array from list: [ 6  7  8  9 10]
Enter fullscreen mode Exit fullscreen mode

In NumPy, we can explicitly specify the data types of an array using the dtype option in the array() function.
Here is the example:

import numpy as np

# Using a tuple to create a 1D NumPy array
array_from_tuple = np.array((1, 2, 3, 4, 5), dtype=float)

# Using a list to create a 1D NumPy array
array_from_list = np.array([6, 7, 8, 9, 10],dtype=complex)

print("Array from tuple:", array_from_tuple)
print("Array from list:", array_from_list)
Enter fullscreen mode Exit fullscreen mode
Output:
Array from tuple: [1. 2. 3. 4. 5.]
Array from list: [ 6.+0.j  7.+0.j  8.+0.j  9.+0.j 10.+0.j]
Enter fullscreen mode Exit fullscreen mode

I will try to post a separate article on various types of datatypes in NumPy in the upcoming posts.

The array() function in NumPy transforms sequences (such as [1, 2, 3, 4, 5] and (2, 3, 4, 5)) into 1-D arrays. To create a 2-D array, we need to pass sequences of sequences, and for a 3-D array, we use sequences of sequences of sequences and so on.
Here is the following example of 2-D array creation using the array() function:

import numpy as np

# Create a 2-D array with specific values
my_array = np.array([(10, 20, 30), (40, 50, 60)])

print(my_array)
Enter fullscreen mode Exit fullscreen mode
Output:
[[10 20 30]
 [40 50 60]
]
Enter fullscreen mode Exit fullscreen mode

In this code snippet, passing a list of two tuples to the array() function results in a 2-D array, where the first dimension has a length of two(ie.,Rows) and the second, a length of three(ie.,Columns).

Creating the array using numeric range series:-

NumPy provides the arange function, which allows for the creation of an array by specifying a numeric range. This function requires three arguments: start, stop, and step, enabling the construction of an array with the desired sequence of numbers.
Let’s see the following example:

# Create an array with even numbers from 10 to 18
even_numbers = np.arange(start=10, stop=20, step=2)

print(even_numbers)
Enter fullscreen mode Exit fullscreen mode
[10 12 14 16 18]
Enter fullscreen mode Exit fullscreen mode

Remember, the stop value is not included in the array, and the step defines the difference between each number in the sequence. You can also use non-integer steps, such as 0.5, to create arrays with decimal numbers.

In summary, NumPy arrays provide efficient and flexible ways to work with multi-dimensional data, ensuring consistent data types across elements. 🚀

Top comments (0)