Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creation Functions for 2D Arrays | NumPy Basics
Ultimate NumPy
course content

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

Creation Functions for 2D Arrays

Similarly to 1D arrays, NumPy has creation functions for 2D arrays. We will cover the most common one, the eye() function.

eye()

The numpy.eye() function creates a matrix in the format of a 2D array where the elements with equal row and column indices are 1, while all other elements are 0.

The two most important parameters are N and M, which specify the number of rows and columns respectively. The M parameter is optional, so you can specify only N to create a square NxN matrix.

Let’s take a look at an example:

1234567
import numpy as np # Creating a 2x2 identity matrix identity_matrix = np.eye(2) print(f'2x2 identity matrix:\n{identity_matrix}') # Creating a 4x3 matrix with np.eye() rectangular_matrix = np.eye(4, 3, dtype=np.int8) print(f'4x3 matrix:\n{rectangular_matrix}')
copy

In our example, we created an identity matrix by specifying only the N parameter and a rectangular matrix by specifying both N and M. We also set the dtype to np.int8 for the rectangular matrix, which can be useful when working with only integers (np.float64 is the default value for dtype).

The resulting 2D arrays look as follows:

Regarding applications, the eye() function is primarily used to create identity matrices for specific linear algebra operations and to initialize matrices in machine learning algorithms.

Note

Here is the documentation for this function: eye.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 5
toggle bottom row

Creation Functions for 2D Arrays

Similarly to 1D arrays, NumPy has creation functions for 2D arrays. We will cover the most common one, the eye() function.

eye()

The numpy.eye() function creates a matrix in the format of a 2D array where the elements with equal row and column indices are 1, while all other elements are 0.

The two most important parameters are N and M, which specify the number of rows and columns respectively. The M parameter is optional, so you can specify only N to create a square NxN matrix.

Let’s take a look at an example:

1234567
import numpy as np # Creating a 2x2 identity matrix identity_matrix = np.eye(2) print(f'2x2 identity matrix:\n{identity_matrix}') # Creating a 4x3 matrix with np.eye() rectangular_matrix = np.eye(4, 3, dtype=np.int8) print(f'4x3 matrix:\n{rectangular_matrix}')
copy

In our example, we created an identity matrix by specifying only the N parameter and a rectangular matrix by specifying both N and M. We also set the dtype to np.int8 for the rectangular matrix, which can be useful when working with only integers (np.float64 is the default value for dtype).

The resulting 2D arrays look as follows:

Regarding applications, the eye() function is primarily used to create identity matrices for specific linear algebra operations and to initialize matrices in machine learning algorithms.

Note

Here is the documentation for this function: eye.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 1. Capítulo 5
toggle bottom row

Creation Functions for 2D Arrays

Similarly to 1D arrays, NumPy has creation functions for 2D arrays. We will cover the most common one, the eye() function.

eye()

The numpy.eye() function creates a matrix in the format of a 2D array where the elements with equal row and column indices are 1, while all other elements are 0.

The two most important parameters are N and M, which specify the number of rows and columns respectively. The M parameter is optional, so you can specify only N to create a square NxN matrix.

Let’s take a look at an example:

1234567
import numpy as np # Creating a 2x2 identity matrix identity_matrix = np.eye(2) print(f'2x2 identity matrix:\n{identity_matrix}') # Creating a 4x3 matrix with np.eye() rectangular_matrix = np.eye(4, 3, dtype=np.int8) print(f'4x3 matrix:\n{rectangular_matrix}')
copy

In our example, we created an identity matrix by specifying only the N parameter and a rectangular matrix by specifying both N and M. We also set the dtype to np.int8 for the rectangular matrix, which can be useful when working with only integers (np.float64 is the default value for dtype).

The resulting 2D arrays look as follows:

Regarding applications, the eye() function is primarily used to create identity matrices for specific linear algebra operations and to initialize matrices in machine learning algorithms.

Note

Here is the documentation for this function: eye.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Similarly to 1D arrays, NumPy has creation functions for 2D arrays. We will cover the most common one, the eye() function.

eye()

The numpy.eye() function creates a matrix in the format of a 2D array where the elements with equal row and column indices are 1, while all other elements are 0.

The two most important parameters are N and M, which specify the number of rows and columns respectively. The M parameter is optional, so you can specify only N to create a square NxN matrix.

Let’s take a look at an example:

1234567
import numpy as np # Creating a 2x2 identity matrix identity_matrix = np.eye(2) print(f'2x2 identity matrix:\n{identity_matrix}') # Creating a 4x3 matrix with np.eye() rectangular_matrix = np.eye(4, 3, dtype=np.int8) print(f'4x3 matrix:\n{rectangular_matrix}')
copy

In our example, we created an identity matrix by specifying only the N parameter and a rectangular matrix by specifying both N and M. We also set the dtype to np.int8 for the rectangular matrix, which can be useful when working with only integers (np.float64 is the default value for dtype).

The resulting 2D arrays look as follows:

Regarding applications, the eye() function is primarily used to create identity matrices for specific linear algebra operations and to initialize matrices in machine learning algorithms.

Note

Here is the documentation for this function: eye.

Tarea

  1. Use the correct function for matrix to create a matrix where the elements with equal row index and column index are 1, while all other elements are 0.
  2. Specify the first two arguments so that array_1 is a 5x2 matrix.
  3. Set the data type of array_1 elements to np.int8.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 5
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt