NumPy Creating Arrays

NumPy Creating Arrays

NumPy is a Python library that is used for scientific computing. NumPy provides multidimensional array objects, which are faster and smoother than the normal Python arrays. In this tutorial, we will learn how to create arrays using NumPy.

Create an Array using NumPy

To make use of the numpy library, we should first install it. Installation can be done using the pip command as shown below:

pip install numpy

Now that we have installed numpy, let’s see how to create arrays using this module.

We can use the numpy.array() function to create arrays. The elements of the array are initialized as shown in the following example.

import numpy as np

# Create a numpy array
my_array = np.array([1, 2, 3, 4, 5])

# Print the array
print('My Array:', my_array)

The above code will output the following result.

My Array: [1, 2, 3, 4, 5]

We can create arrays with more than one dimension as well. The following example creates a two-dimensional array.

import numpy as np

# Create a 2D numpy array
my_array = np.array([[1, 2], [3, 4], [5, 6]])

# Print the 2D array
print('My 2D Array:')
for row in my_array:
    print(row)

The above code will output the following result.

My 2D Array:
[1 2]
[3 4]
[5 6]

We can create an array using a range function:

NumPy’s arange() function can be used to create arrays containing a range of numbers. The function takes three arguments – start, stop, step. In the following example, we will create an array containing the numbers from 0 to 9 using the arange() function.

import numpy as np

# Create numpy array using arange() function
my_array = np.arange(10)

# Print the array
print('My Array:', my_array)

The above code will output the following result.

My Array: [0 1 2 3 4 5 6 7 8 9]

We can create an array containing all zeros or ones:

NumPy’s zeros() and ones() functions can be used to create arrays containing all zeros or ones respectively. The following example shows how to use the zeros() function to create an array of zeros.

import numpy as np

# Create an array of zeros
my_array = np.zeros(5)

# Print the array
print('Zeros array:', my_array)

The above code will output the following result.

Zeros array: [0. 0. 0. 0. 0.]

The following example shows how to use the ones() function to create an array of ones.

import numpy as np

# Create an array of ones
my_array = np.ones(5)

# Print the array
print('Ones array:', my_array)

The above code will output the following result.

Ones array: [1. 1. 1. 1. 1.]

Creating an identity matrix using numpy

An identity matrix is a square matrix in which all the elements of the main diagonal are ones and the rest of the elements are zeros. NumPy’s eye() function can be used to create an identity matrix. The following example shows how to create a 3 x 3 identity matrix.

import numpy as np

# Creating an Identity matrix
my_array = np.eye(3)

# Print the array
print('Identity matrix:', my_array)

The above code will output the following result.

Identity matrix: 
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

Creating a linspace function:

NumPy’s linspace() function can be used to create an array of linearly spaced values. The function takes three arguments – start, stop, and num (number of samples). The following example shows how to use the linspace() function to create an array of five evenly spaced numbers between 0 and 1.

import numpy as np

# Create numpy array using linspace() function
my_array = np.linspace(start=0, stop=1, num=5)

# Print the array
print('Linspace array:', my_array)

The above code will output the following result.

Linspace array: [0.   0.25 0.5  0.75 1.  ]

NumPy’s reshape() function can be used to change the shape of an array without changing its data. The following example shows how to use the reshape() function to convert a one-dimensional array into a two-dimensional array.

import numpy as np

# Create a one-dimensional array
my_array = np.array([1, 2, 3, 4, 5, 6])

# Reshape the array into a two-dimensional array
my_array_2d = my_array.reshape(3, 2)

# Print the original and the reshaped arrays
print('Original array:', my_array)
print('Reshaped array:')
for row in my_array_2d:
    print(row)

The above code will output the following result.

Original array: [1 2 3 4 5 6]
Reshaped array:
[1 2]
[3 4]
[5 6]

Conclusion:

In this tutorial, we learned how to create arrays using the NumPy library. We saw how to create arrays with multiple dimensions, using numpy’s range, create zeros and ones arrays, identity matrix, linspace function and reshape it. NumPy provides several functions to create different types of arrays, which can be used for scientific computations. Understanding these functions will help us to write efficient code for scientific computing.

Leave a Reply

Your email address will not be published. Required fields are marked *