Program to get the magnitude of a NumPy vector

In this python numpy program, we will get the magnitude of a NumPy vector.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create a vector using np.array().
  3. Get the magnitude of the vector using np.linalg.norm().
  4. Print the output.
				
					import numpy as np
x = np.array([3, 5, 1])

print("Magnitude of the given vector: ",np.linalg.norm(x))
				
			

Output :

				
					Magnitude of the given vector:  5.916079783099616
				
			

remove all rows in a NumPy array that contain non-numeric values.

count the frequency of unique values.

Remove all rows that contain non-numeric values from an NumPy array

In this python numpy program, we will remove all rows that contain non-numeric values from a NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Remove all rows that contain non-numeric values from the array using array[~np.isnan(array).any(axis=1)].
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8, np.nan],[2,4,6]])
print("Old: \n",x)

print("New: \n",x[~np.isnan(x).any(axis=1)])
				
			

Output :

				
					Old: 
 [[ 4.  8. nan]
 [ 2.  4.  6.]]
New: 
 [[2. 4. 6.]]
				
			

replace the negative values in an array with 1.

get the magnitude of a vector.

Replace the negative values in an NumPy array

In this python numpy program, we will replace the negative values in an NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Replace the negative values in the given array with 1 using indexing and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,-5],[2,-9,6]])
print("Old: \n",x)

x[x<0] = 1
print("New: \n",x)
				
			

Output :

				
					Old: 
 [[ 4  8 -5]
 [ 2 -9  6]]
New: 
 [[4 8 1]
 [2 1 6]]
				
			

add an extra column to an array.

remove all rows in a NumPy array that contain non-numeric values.

Add an extra column to a NumPy array

In this python numpy program, we will add an extra column to a NumPy array.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Create another array of the same dimension as the given array.
  4. Add the new array as a column to the original array using np.append() and set the axis equal to 1 for the column.
  5. Print the output.
				
					import numpy as np
x = np.array([[4,8,0],[2,0,6]])
y = np.array([[5],[7]])

print(np.append(x, y, axis=1))
				
			

Output :

				
					[[4 8 0 5]
 [2 0 6 7]]
				
			

convert an array of float values to an array of integer values.

replace the negative values in an array with 1.

Convert an array of float datatype to integer

In this python numpy program, we will convert an array of float datatype to integer.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Convert an array having values as float datatype to an integer using astype(int).
  4. Print the output.
				
					import numpy as np
x = np.array([[6.7, 5.2],[9.4, 2.7]])
print("Float array: \n",x)
print("Integer array: \n",x.astype(int))
				
			

Output :

				
					Float array: 
 [[6.7 5.2]
 [9.4 2.7]]
Integer array: 
 [[6 5]
 [9 2]]
				
			

access an array by column.

add an extra column to an array.

Program to access an array by column using NumPy

In this python numpy program, we will access an array by column using NumPy.

Steps to solve the program
  1. Import the numpy library as np.
  2. Create an array using np.array().
  3. Access an array by column using indexing (i.e. array[row, column] ) and logic.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,0],[2,0,6]])
print("1st column: \n",x[:,0])
print("2nd column: \n",x[:,1])
print("3rd column: \n",x[:,2])
				
			

Output :

				
					1st column: 
 [4 2]
2nd column: 
 [8 0]
3rd column: 
 [0 6]
				
			

print squares of all the elements of an array.

convert an array of float values to an array of integer values.

Print squares of all the elements of an array using NumPy.

In this python numpy program, we will print squares of all the elements 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. Use np.square() to get the squares of all the elements in the given array.
  4. Print the output.
				
					import numpy as np 
x = np.array([2,6,3,1])

print("Sqaure of every element in the arrary: ",np.square(x))
				
			

Output :

				
					Sqaure of every element in the arrary:  [ 4 36  9  1]
				
			

print every element of an array.

access an array by column.

Print every element of an array using NumPy

In this python numpy program, we will print every 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. Use for loop with np.nditer() to print every element of the given array.
  4. Print the output.
				
					import numpy as np
x = np.array([[4,8,0],[2,0,6]])

for ele in np.nditer(x):
    print(ele,end=' ')
				
			

Output :

				
					4 8 0 2 0 6
				
			

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

print squares of all the elements of an array.

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.