0% found this document useful (0 votes)
18 views

Abhi ML

Uploaded by

singhopila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Abhi ML

Uploaded by

singhopila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

/*Name : Abhishek Kumar Singh

Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to use different techniques for filling the missing values.
Source Code : */

import pandas as pd
data = {
'A': [1, 2, None, 4, 5],
'B': [3, None, 5, None, 7],
'C': [None, 2, 4, 6, None]
}
print("Original DataFrame:")
df = pd.DataFrame(data)
print(df)
print("\nImputed Data using Mean:")
df_mean=df.fillna(value=df['A'].mean())
print(df_mean)
print("\nImputed Data using Median:")
df_median=df.fillna(value=df['B'].median())
print(df_median)
print("\nImputed Data using Mode:")
df_mode=df.fillna(value=df['C'].mode()[0])
print(df_mode)

/*Output

Original DataFrame:
A B C
0 1.0 3.0 NaN
1 2.0 NaN 2.0
2 NaN 5.0 4.0
3 4.0 NaN 6.0
4 5.0 7.0 NaN

Imputed Data using Mean:


A B C
0 1.0 3.0 3.0
1 2.0 3.0 2.0
2 3.0 5.0 4.0
3 4.0 3.0 6.0
4 5.0 7.0 3.0
Imputed Data using Median:
A B C
0 1.0 3.0 5.0
1 2.0 5.0 2.0
2 5.0 5.0 4.0
3 4.0 5.0 6.0
4 5.0 7.0 5.0

Imputed Data using Mode:


A B C
0 1.0 3.0 2.0
1 2.0 2.0 2.0
2 2.0 5.0 4.0
3 4.0 2.0 6.0
4 5.0 7.0 2.0

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to generate the synthetic dataset..
Source Code : */

import pandas as pd
from sklearn.datasets import make_classification
X, y = make_classification(
n_samples=1000,
n_features=10,
n_informative=5,
n_redundant=0,
random_state=42
)
df = pd.DataFrame(X, columns=[f'Feature_{i}' for i in range(1, 11)])
df['Target'] = y
df.to_csv('synthetic_dataset.csv', index=False)
print('Synthetic dataset generated and saved successfully!')

/*Output

Synthetic dataset generated and saved successfully!

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to add two matrices.
Source Code : */

def add_matrices(matrix1, matrix2):


if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
print("Error: Matrices must have the same dimensions for addition")
return None
result = [[0] * len(matrix1[0]) for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] + matrix2[i][j]
return result

matrix1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
result_matrix = add_matrices(matrix1, matrix2)
if result_matrix:
print("Resultant Matrix:")
for row in result_matrix:
print(row)

/*Output

Resultant Matrix:
[10, 10, 10]
[10, 10, 10]
[10, 10, 10]

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to multiply matrices.
Source Code : */

def multiply_matrices(matrix1, matrix2):


if len(matrix1[0]) != len(matrix2):
print("Error: Number of columns in the first matrix must be equal to the number of rows in the
second matrix for multiplication")
return None

result = [[0] * len(matrix2[0]) for _ in range(len(matrix1))]


for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result
matrix1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
matrix2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]
]
result_matrix = multiply_matrices(matrix1, matrix2)
if result_matrix:
print("Resultant Matrix:")
for row in result_matrix:
print(row)

/*Output

Resultant Matrix:
[30, 24, 18]
[84, 69, 54]
[138, 114, 90]

*/
/*Name : Aman Samant
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to find the combinatorics of a given input number.
Source Code : */

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

def combinations(n, k):


return factorial(n) // (factorial(k) * factorial(n - k))

n = int(input("Enter the value of n: "))


k = int(input("Enter the value of k: "))
if n >= k >= 0:
result = combinations(n, k)
print(f"The number of combinations ({n} choose {k}) is: {result}")
else:
print("Invalid input: k must be less than or equal to n and both must be non-negative integers.")

/*Output

Enter the value of n: 4


Enter the value of k: 2
The number of combinations (4 choose 2) is: 6

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to find the permutation of a given number.
Source Code : */

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

def permutations(n, k=None):


if k is None:
return factorial(n)
else:
if n >= k >= 0:
return factorial(n) // factorial(n - k)
else:
return None

n = int(input("Enter the value of n: "))


k = int(input("Enter the value of k (optional): "))
if k is not None:
result = permutations(n, k)
if result is not None:
print(f"The number of permutations ({n}P{k}) is: {result}")
else:
print("Invalid input: k must be less than or equal to n and both must be non-negative integers.")
else:
result = permutations(n)
print(f"The number of permutations of {n} elements is: {result}")

/*Output

Enter the value of n: 6


Enter the value of k (optional): 2
The number of permutations (6P2) is: 30

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to find factorial of a given number.
Source Code : */

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

num = int(input("Enter a number to find its factorial: "))


if num < 0:
print("Factorial is not defined for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
result = factorial(num)
print(f"The factorial of {num} is: {result}")

/*Output

Enter a number to find its factorial: 5


The factorial of 5 is: 120

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to print the transpose of a matrix.
Source Code : */

def transpose_matrix(matrix):
return [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]

def print_matrix(matrix):
for row in matrix:
print(row)

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("Original Matrix:")
print_matrix(matrix)
print("\nTranspose of the Matrix:")
transpose = transpose_matrix(matrix)
print_matrix(transpose)

/*Output

Original Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Transpose of the Matrix:


[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

*/
/*Name : Abhishek Kumar Singh
Course : BCA
Section : F
Roll Number : 3
Problem Statement : Write a python program to generate a dataset and implement a K-Means
clustering algorithm..
Source Code : */

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

# Generate random dataset


X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Plot the dataset


plt.scatter(X[:, 0], X[:, 1], s=50)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Randomly Generated Dataset')
plt.show()

# Apply K-Means clustering


kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)

# Plot the clustered data


plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('K-Means Clustering')
plt.show()
/*Output

*/

You might also like