Program to make an array immutable using NumPy

In this python numpy program, we will make an array immutable using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Make an array immutable i.e. read-only by setting flags.writeable as False.
  4. When we try to change any value of the array it will show the error as
    ValueError: assignment destination is read-only.
    
				
					import numpy as np
x = np.array([[5, 3],[7, 4]])
x.flags.writeable = False
x[0,1] = 0
				
			

Output :

				
					ValueError                                Traceback (most recent call last)
<ipython-input-6-6fe1d7a57604> in <module>
      3 x = np.array([[5, 3],[7, 4]])
      4 x.flags.writeable = False
----> 5 x[0,1] = 0

ValueError: assignment destination is read-only
				
			

find the median of the matrix along the column.

print every element of an array.

Median of the matrix along the column using NumPy

In this python numpy program, we will find the median of the matrix along the column using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the median of the matrix along the column using np.median() and set the axis equal to 1.
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],[7, 4]])
print("Median of array along column:",np.median(x,axis=1))
				
			

Output :

				
					Median of array along column: [4.  5.5]
				
			

find the median of the matrix along the rows.

make an array immutable i.e. read-only.

Median of the matrix along the rows using NumPy

In this python numpy program, we will find the median of the matrix along the rows using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the median of the matrix along the rows using np.median() and set the axis equal to 0.
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],[7, 4]])
print("Median of array along row:",np.median(x,axis=0))
				
			

Output :

				
					Median of array along row: [6.  3.5]
				
			

find the median of the matrix.

find the median of the matrix along the column.

Find the median of the matrix using NumPy.

In this python numpy program, we will find the median of the matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the median of the matrix using np.median().
  4. Print the output.
				
					import numpy as np
x = np.array([[5, 3],[7, 4]])
print("Median of array:",np.median(x))
				
			

Output :

				
					Median of array: 4.5
				
			

get the number of nonzero elements in an array.

find the median of the matrix along the rows.

Get the number of nonzero elements in an array using NumPy

In this python numpy program, we will get the number of nonzero elements in an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Get the number of nonzero elements in an array using np.count_nonzero().
  4. Print the output.
				
					import numpy as np
x = np.array([[4, 8, 0],[2, 0, 6]])
print("Number of non-zero elements: ",np.count_nonzero(x))
				
			

Output :

				
					Number of non-zero elements:  4
				
			

split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements.

find the median of the matrix.

Split an array of 10 elements into 3 arrays using NumPy.

In this python numpy program, we will split an array of 10 elements into 3 arrays using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements using np.split().
  4. Print the output.
				
					import numpy as np
x = np.arange(1, 11)
print("Original array:",x)
print("After splitting:")
print(np.split(x, [2, 5]))
				
			

Output :

				
					Original array: [ 1  2  3  4  5  6  7  8  9 10]
After splitting:
[array([1, 2]), array([3, 4, 5]), array([ 6,  7,  8,  9, 10])]
				
			

convert 1-D arrays as columns into a 2-D array.

get the number of nonzero elements in an array.

Convert 2 arrays as columns of the matrix using NumPy

In this python numpy program, we will convert 2 arrays as columns of the matrix using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create two 1-dimensional arrays using np.array().
  3. Now convert those two 1-dimensional arrays as columns of the matrix using np.column_stack().
  4. Print the output.
				
					import numpy as np
x = np.array((3,8,5))
y = np.array((4,0,7))
z = np.column_stack((x,y))
print(z)
				
			

Output :

				
					[[3 4]
 [8 0]
 [5 7]]
				
			

remove single-dimensional entries from a specified shape.

split an array of 10 elements into 3 arrays, each of which has 2, 3, and 5 elements.

Find the ith element of an array using NumPy

In this python numpy program, we will find the ith element of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the ith element of an array using indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,7],[2,3,6]])
print("Fifth element: ",x[1,1])
				
			

Output :

				
					Fifth element:  3
				
			

change the data type of an array.

remove single-dimensional entries from a specified shape.

Change the data type of an array using NumPy

In this python numpy program, we will change the data type of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Change the data type of an array using astype(float) and store the result in another variable.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,7],[2,3,6]])
print("Old: \n",x)
y = x.astype(float)
print("New: \n",y)
				
			

Output :

				
					Old: 
 [[4 8 7]
 [2 3 6]]
New: 
 [[4. 8. 7.]
 [2. 3. 6.]]
				
			

find the indices of the minimum value of an array.

find the ith element of an array.

Find the indices of the minimum value of an array using NumPy

In this python numpy program, we will find the indices of the minimum value of an array using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Find the indices of the minimum value of the array using np.argmin().
  4. Print the output.
				
					import numpy as np
x = np.array([76,36,56,90])

print("Index of minimum Values: ",np.argmin(x))
				
			

Output :

				
					Index of manimum Values:  1
				
			

find the indices of the maximum value of an array.

change the data type of an array.