# Python Numpy Exercises With Solutions

#### Exercise 1 || Solution

Using the numpy library, write a program that allows you to create a matrix of the 3x3 type formed by the integers 1 , 2 , 3 , ... , 9.

#### Exercise 2 || Solution

Create a program that calculates the transpose of the following matrix using the numpy library:

``````A = numpy.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9,]])``````

#### Exercise 3 || Solution

Write a function in python which takes as argument a square numpy matrix of type nxn and which returns its trace.
We recall that the trace of a square matrix A = (aij) i , j is the number Tr(A) = a11 + a22 + ... + ann

#### Exercise 4 || Solution

Resume the previous exercise (Exercice3) without using the trace() method

#### Exercise 5 || Solution

Write a numpy python program that converts a binary numpy matrix (containing only 0s and 1s) into a numpy boolean matrix (i.e. the '1 will be replaced by True and the '0' by False)

#### Exercise 6 || Solution

Write a numpy python program that allows to stack 2 numpy marices horizontally, i.e. 2 arrays having the same 1st dimension (same number of rows)

#### Exercise 7 || Solution

Write a numpy python program allowing to stack 2 matrices numpy vertically, i.e. 2 arrays having the same last dimension ( same number of columns)

#### Exercise 8 || Solution

Write a python-numpy program that allows you to generate a numpy matrix by repeating a smaller one of 2 dimensions, 5 times.

#### Exercise 9 || Solution

Write a numpy python program that returns the multiplication of two numpy matrices.

#### Exercise 11 || Solution

Indicate the output of the following program:

``````A = np.zeros(7)
print(A)``````

#### Exercise 12 || Solution

Indicate the output of the following program:

``````A = np.zeros(7)
A[3] = 2
print(A)``````

#### Exercise 13 || Solution

Indicate the output of the following program:

``````A = np.arange(10,20)
print(A)``````

#### Exercise 14|| Solution

Indicate the output of the following program:

``````A = np.arange(10, 20, 3)
print(A)``````

#### Exercise 15 || Solution

What should the following program return:

``````A = np.arange(10)
A = A[::-1]
print(A)``````

#### Exercise 16 || Solution

Write a python-numpy program that allows to reverse the matrix:

``A = np.array([1, 2, 3, 4, 5])``

in the matrix:

``B = np.array([5, 4, 3, 2, 1])``

#### Exercise 17 || Solution

We consider the matrix:

``A = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])``

Write a python-numpy program that transforms the type of this matrix into 3x3

#### Exercise 18 || Solution

Create a python-numpy program that generates the following 9x9 type matrix:

``````[[1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 2. 2. 2. 2. 2. 2. 2. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1.]]``````

#### Exercise 19 || Solution

Write an algorithm in Python numpy as a function that tests the type of a matrix and returns True if the matrix is ​​square of type nxn and False if not.

#### Exercise 20 || Solution

Write a python program numpy which takes a numpy matrix as a parameter and returns its determinant when the matrix is ​​square and a message telling the user to choose a square matrix otherwise.

#### Exercise 21 || Solution

The conditioning of a square matrix A is C(A) = ||A||x||A-1| | (product of the standard of A with the standard of its reverse A.A-1). In the case of norm 2 it is also the ratio between the largest and the smallest of the absolute values ​​of the eigenvalues ​​of the matrix.
Write a numpy python program that returns the conditioning of a numpy matrix.

Younes Derfoufi
my-courses.net