Python PB 3 System
Python PB 3 System
1. Write the program to get the maximum and minimum value from a given matrix.
Code :
import numpy as np
matrix = np.array([[4, 2, 7],[1, 9, 3],[5, 6, 8]])
max_value = np.max(matrix)
min_value = np.min(matrix)
print("Maximum value:", max_value)
print("Minimum value:", min_value)
Output :
Maximum value: 9
Minimum value: 1
2. Create nDArray with values ranging from 10 to 49 each saved with difference of 3.
Code :
import numpy as np
nDArray = np.arange(10, 49, 3)
print(nDArray)
Output :
[10 13 16 19 22 25 28 31 34 37 40 43 46 49]
3. Find the number of rows and columns of a given matrix using NumPy. Extract First row and
second column.
Code :
import numpy as np
matrix = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
num_rows, num_columns = matrix.shape
first_row = matrix[0, :]
second_column = matrix[:, 1]
print("Number of rows:", num_rows)
print("Number of columns:", num_columns)
print("First row:", first_row)
print("Second column:", second_column)
Output :
Number of rows: 3
Number of columns: 3
First row: [1 2 3]
Second column: [2 5 8]
KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023
4. Write a program to get Addition, Subtracting and division of two Matrices in Python.
Code :
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
addition = matrix1 + matrix2
subtraction = matrix1 - matrix2
division = matrix1 / matrix2
print("Addition:\n", addition)
print("Subtraction:\n", subtraction)
print("Division:\n", division)
Output :
Addition:
[[ 6 8]
[10 12]]
Subtraction:
[[-4 -4]
[-4 -4]]
Division:
[[0.2 0.33333333]
[0.42857143 0.5 ]]
import numpy as np
array1d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array2d = array1d.reshape(3, -1)
print(array2d)
Output :
[[1 2 3]
[4 5 6]
KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023
[7 8 9]]
6. Write a numpy program to create two Array A and B and calculate covariance and correlation
coefficient of two arrays.
Code :
import numpy
A = numpy.array([1, 2, 3, 4, 5])
B = numpy.array([5, 4, 3, 2, 1])
covariance = numpy.cov(A, B)[0, 1]
correlation = numpy.corrcoef(A, B)[0, 1]
print("Covariance:", covariance)
print("Correlation coefficient:", correlation)
Output :
Covariance: -2.5
Correlation coefficient: -0.9999999999999999
7. Matrix Multiplication in NumPy example, for two matrices A and B. A = [[1, 2], [2, 3]] B = [[4,
5], [6, 7]]
Code :
import numpy
A = numpy.array([[1, 2], [2, 3]])
B = numpy.array([[4, 5], [6, 7]])
result = numpy.dot(A, B)
print("Matrix Multiplication result:")
print(result)
Output :
import pandas
data = [[1, 'Name'],[2, 'Name2'],[3, 'Name3']]
df = pandas.DataFrame(data, columns=['ID', 'Name'])
print(df)
KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023
Output :
ID Name
0 1 Name
1 2 Name2
2 3 Name3
import pandas
data = {'Name': ['Name', 'Name2', 'Name3'],'Age': [25, 30, 55]}
df = pandas.DataFrame(data)
print(df)
Output :
Name Age
0 Name 25
1 Name2 30
2 Name3 55
• Demo.csv
Name,Age
Name,25
Name2,30
Name3,55
• Q10.py
import pandas
df = pandas.read_csv('data.csv')
print(df)
Output :
Name Age
KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023
0 Name 25
1 Name2 30
2 Name3 55
import pandas
data = {'Name': ['Name', 'Name2', 'Name5'],'Age': [25, 30, 55]}
df = pandas.DataFrame(data)
df.to_csv('data.csv', index=False)
Output :
• Demo.csv
Name,Age
Name,25
Name2,30
Name5,55
12. Write a program to create following data frame and find out first 3 and last 3 rows form that.
Code :
import pandas as pd
# Create a DataFrame
df = pd.DataFrame(data)
KD DD
Python Problem Sheet – 3 Sub Date : 25 October 2023
print("First 3 rows:")
print(first_3_rows)
Output :
First 3 rows:
e_no lname fname Street City st zip dept payrate
0 1 Hurwood Roger 1234 Stirrup La Green Valley MD 20441 2020 16.0
1 2 Kerin Linda 802 Wilderness Dr Wild Woods VA 33256 2020 17.5
2 3 Gross Mary 303 Stagecoach Rd Green Valley MD 20441 2020 10.0
Last 3 rows:
e_no lname fname Street City st zip dept payrate
2 3 Gross Mary 303 Stagecoach Rd Green Valley MD 20441 2020 10.00
3 4 Wilson Arthur 211 Main St Yellow Fountain VA 33210 2020 17.50
4 5 Chung Yung 422 Maple St Garden City MD 20331 1050 12.25
KD DD