0% found this document useful (0 votes)
17 views31 pages

Python Programming

Uploaded by

Dilip Das
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views31 pages

Python Programming

Uploaded by

Dilip Das
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 31

ASSGNMENT -01

1. Installation of Python -:
a. Install Python IDLE.
b. Add to Path variable.
c. Go to CMD.
d. python -v.
e. pip -v.
f. pip install numpy.
g. Check if installed -pip show numpy.
h. pip install pandas.
i. pip show pandas.

2. Write a program to find if a number is prime or not in python.


Source Code -:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n//2) + 1):
if n % i == 0:
return False
return True
number = int(input("Enter a number: "))
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")

Output -:
Enter a number: 54
54 s not a prime number.

3. Write a program to concede two strings.


Source Code -:
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
print("Concatenated String:", string1 +" "+ string2)

Output -:
Enter the first string: Hello
Enter the second string: World
Concatenated String: Hello World

4. Write a program to show addition of two square matrix.


Source Code -:
X = [[1,2],
[3,4]]
Y = [[5,6],
[7,8]]
result = [[0,0],
[0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output -:
[6, 8]
[10, 12]

ASSGNMENT -02
1. Write a program to add two matrixes’ manually.
Source Code -:
def get_matrix(size):
matrix = []
print("Enter the elements row-wise:")
for i in range(size):
row = []
for j in range(size):
row.append(int(input(f"Element [{i}][{j}]: ")))
matrix.append(row)
return matrix
def add_matrices(matrix1, matrix2):
size = len(matrix1)
result = [[0 for _ in range(size)] for _ in range(size)]
for i in range(size):
for j in range(size):
result[i][j] = matrix1[i][j] + matrix2[i][j]
return result
def print_matrix(matrix):
for row in matrix:
print(row)
size = int(input("Enter the size of the square matrices: "))
print("Enter elements for the first matrix:")
matrix1 = get_matrix(size)
print("Enter elements for the second matrix:")
matrix2 = get_matrix(size)
result = add_matrices(matrix1, matrix2)
print("The sum of the matrices is:")
print_matrix(result)

Output -:
Enter the size of the square matrices: 2
Enter elements for the first matrix:
Enter the elements row-wise:
Element [0][0]: 1
Element [0][1]: 2
Element [1][0]: 3
Element [1][1]: 4
Enter elements for the second matrix:
Enter the elements row-wise:
Element [0][0]: 5
Element [0][1]: 6
Element [1][0]: 7
Element [1][1]: 8
The sum of the matrices is:
[6, 8]
[10, 12]

2. Write a program in python to multiply two matrices.


Source Code -:
def get_matrix(rows, cols):
matrix = []
print(f"Enter the elements of a {rows}x{cols} matrix row by row:")
for i in range(rows):
row = list(map(int, input().split()))
if len(row) != cols:
print(f"Each row must have exactly {cols} elements.")
return None
matrix.append(row)
return matrix
def matrix_multiply(matrix1, matrix2):
rows_matrix1 = len(matrix1)
cols_matrix1 = len(matrix1[0])
rows_matrix2 = len(matrix2)
cols_matrix2 = len(matrix2[0])
if cols_matrix1 != rows_matrix2:
print("Cannot multiply the matrices. Number of columns in matrix1 must be equal to
the number of rows in matrix2.")
return None
result_matrix = [[0 for _ in range(cols_matrix2)] for _ in range(rows_matrix1)]
for i in range(rows_matrix1):
for j in range(cols_matrix2):
for k in range(cols_matrix1):
result_matrix[i][j] += matrix1[i][k] * matrix2[k][j]
return result_matrix
rows_matrix1 = int(input("Enter the number of rows for the first matrix: "))
cols_matrix1 = int(input("Enter the number of columns for the first matrix: "))
matrix1 = get_matrix(rows_matrix1, cols_matrix1)
if matrix1 is None:
print("Invalid input for the first matrix.")
exit()
rows_matrix2 = int(input("Enter the number of rows for the second matrix: "))
cols_matrix2 = int(input("Enter the number of columns for the second matrix: "))
matrix2 = get_matrix(rows_matrix2, cols_matrix2)
if matrix2 is None:
print("Invalid input for the second matrix.")
exit()
result = matrix_multiply(matrix1, matrix2)
if result:
print("Resultant Matrix:")
for row in result:
print(row)
else:
print("Matrix multiplication was not possible.")

Output -:
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 2
Enter the elements of a 2x2 matrix row by row:
12
34
Enter the number of rows for the second matrix: 2
Enter the number of columns for the second matrix: 2
Enter the elements of a 2x2 matrix row by row:
45
67
Resultant Matrix:
[16, 19]
[36, 43]

3. Write a program on Hadamard of two matrixes.


Source Code -:
def hadamard_product(matrix1, matrix2):
if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
return None
result = []
for i in range(len(matrix1)):
row = []
for j in range(len(matrix1[0])):
row.append(matrix1[i][j] * matrix2[i][j])
result.append(row)
return result
matrix1 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix2 = [
[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
result = hadamard_product(matrix1, matrix2)
if result:
for row in result:
print(row)
else:
print("Matrices must have the same dimensions")

Output -:
[9, 16, 21]
[24, 25, 24]
[21, 16, 9]
ASSGNMENT -03
1. Program -: Checking validates of Python Libraries -:
a. sys b. scipy c. numpy d. matplotlib e. pandas f. sklearn
a. sys
Source code -:
import sys
print('Python:{}'.format(sys.version))
Output -:
Python:3.12.6
b. scipy
Source code -:
import scipy
print('Scipy:{}'.format(scipy.__version__))
Output -:
Scipy:1.13.1
c. numpy
Source code -:
import numpy
print('Numpy:{}'.format(numpy.__version__))
Output -:
Numpy:1.26.4
d. matplotlib
Source code -:
import matplotlib
print('Matplotlib:{}'.format(matplotlib.__version__))
Output -:
Matplotlib:3.9.1
e. pandas
Source Code -:
import pandas
print('Pandas:{}'.format(pandas.__version__))
Output -:
Pandas:2.2.2
f. sklearn
Source Code -:
import sklearn
print('Sklearn:{}'.format(sklearn.__version__))
Output -:
Sklearn:1.5.1

2. Write a python program using numpy to print on 2D array.


Source Code -:
import numpy as np
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("2D Array:")
print(array_2d)

Output -:
2D Array:
[[1 2 3]
[4 5 6]
[7 8 9]]

3. Write a python program using numpy to add two matrixes.


Source Code -:
import numpy as np
matrix1 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
matrix2 = np.array([[9, 8, 7],
[6, 5, 4],
[3, 2, 1]])
result = np.add(matrix1, matrix2)
print("Matrix 1:")
print(matrix1)
print("\nMatrix 2:")
print(matrix2)
print("\nSum of Matrix 1 and Matrix 2:")
print(result)

Output -:
Matrix 1:
[[1 2 3]
[4 5 6]
[7 8 9]]

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

Sum of Matrix 1 and Matrix 2:


[[10 10 10]
[10 10 10]
[10 10 10]]

4. Write a python program using numpy to multipy two matrixes.


Source Code -:
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
C = np.dot(A, B)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
print("Matrix A * Matrix B:")
print(C)

Output -:
Matrix A:
[[1 2]
[3 4]]
Matrix B:
[[5 6]
[7 8]]
Matrix A * Matrix B:
[[19 22]
[43 50]]

5. Write a python program using numpy to implement Sparse Matrix.


Source Code -:
import numpy as np
import scipy
from scipy import sparse
eye = np.eye(4)
print("Numpy array: \n{}".format(eye))
sparse_matrix = sparse.csr_matrix(eye)
print("\nSciPy sparse CSR matrix: \n{}".format(sparse_matrix))
data = np.ones(4)
row_indices = np.arange(4)
col_indices = np.arange(4)
eye_coo = sparse.coo_matrix((data, (row_indices, col_indices)))
print("\nCOO representation: \n{}".format(eye_coo))

Output -:
Numpy array:
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

SciPy sparse CSR matrix:


(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
(3, 3) 1.0

COO representation:
(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
(3, 3) 1.0

6. Write a Python program using NumPy and Matplotlib.


Source Code -:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 5)
print(x)
y = np.sin(x)
print(y)
plt.plot(x, y, marker = "x")
plt.show()
Output-:
[-10. -5. 0. 5. 10.]
[ 0.54402111 0.95892427 0. -0.95892427 -0.54402111]

7. Write a program to show the use case of pandas.


Source Code-:
import pandas as pd
data = {'Name': ["John","Ann","Peter","Linda"], "Location":["New
York","Paris","Berlin","Londan"],"Age":[50,13,53,11]}
data_pandas = pd.DataFrame(data)
filtered_data = data_pandas[data_pandas['Age'] > 30]
print(filtered_data)
Output-:
Name Location Age
0 John New York 50
2 Peter Berlin 53

8. Write a program to show the use case of Loud.


Source Code-:
from pandas import read_csv
from pandas.plotting import scatter_matrix
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv"
names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
dataset = read_csv(url, names = names)
print(dataset)
Output -:
sepal_length sepal_width petal_length petal_width class
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa
.. ... ... ... ... ...
145 6.7 3.0 5.2 2.3 Iris-virginica
146 6.3 2.5 5.0 1.9 Iris-virginica
147 6.5 3.0 5.2 2.0 Iris-virginica
148 6.2 3.4 5.4 2.3 Iris-virginica
149 5.9 3.0 5.1 1.8 Iris-virginica
[150 rows x 5 columns]
9. Write a program to classify iris species.
Source Code -:
from sklearn.datasets import load_iris
iris_dataset = load_iris()
print("keys of iris dataset: \n {}".format(iris_dataset.keys()))
print(iris_dataset['DESCR'][:100]+"\n...")
print("Feature_names :\n{}".format(iris_dataset['feature_names']))
print("Shapeofdata :{}".format(iris_dataset['data'].shape))
print("FirstfiveColumns of data :\n{}".format(iris_dataset['data'][:5]))
Output-:
keys of iris dataset:
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename',
'data_module'])
.. _iris_dataset:
Iris plants dataset
--------------------
**Data Set Characteristics:**
:Number
...
Feature_names :
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Shapeofdata :(150, 4)
FirstfiveColumns of data :
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]]

10.Write a program to calling train_test_split. Function.


Source Code -:
import pandas as pd
import sklearn
import mglearn
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
iris_dataset = load_iris()
X_train, X_test, Y_train, Y_test = train_test_split(iris_dataset.data, iris_dataset.target,
random_state=1)
print(f"X_train shape: {format(X_train.shape)}")
print(f"Y_train shape: {format(Y_train.shape)}")
print(f"X_test shape: {format(X_test.shape)}")
print(f"Y_test shape: {format(Y_test.shape)}")
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
pd.plotting.scatter_matrix(iris_dataframe, c=Y_train, figsize=(15,15), marker='o',
hist_kwds={'bins':20}, s=60, alpha=0.8, cmap=mglearn.cm3)
plt.show()
Output -:
X_train shape: (112, 4)
Y_train shape: (112,)
X_test shape: (38, 4)
Y_test shape: (38,)

ASSGNMENT -04
1. Write a program to implement KNN model and to plot first feature and second feature of
iris dataset.
Source Code -:
import numpy as np
import pandas as pd
import mglearn
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
#generate dataset
X,y=mglearn.datasets.make_forge()
#plot dataset
mglearn.discrete_scatter(X[:,0], X[:, 1], y)
plt.legend(["class 0", "class 1"], loc=4)
plt.xlabel("first feature:")
plt.ylabel("2nd feature:")
print(X,y)
print("X.shape:{}".format(X.shape))
plt.show()
Output -:
[[ 9.96346605 4.59676542]
[11.0329545 -0.16816717]
[11.54155807 5.21116083]
[ 8.69289001 1.54322016]
[ 8.1062269 4.28695977]
[ 8.30988863 4.80623966]
[11.93027136 4.64866327]
[ 9.67284681 -0.20283165]
[ 8.34810316 5.13415623]
[ 8.67494727 4.47573059]
[ 9.17748385 5.09283177]
[10.24028948 2.45544401]
[ 8.68937095 1.48709629]
[ 8.92229526 -0.63993225]
[ 9.49123469 4.33224792]
[ 9.25694192 5.13284858]
[ 7.99815287 4.8525051 ]
[ 8.18378052 1.29564214]
[ 8.7337095 2.49162431]
[ 9.32298256 5.09840649]
[10.06393839 0.99078055]
[ 9.50048972 -0.26430318]
[ 8.34468785 1.63824349]
[ 9.50169345 1.93824624]
[ 9.15072323 5.49832246]
[11.563957 1.3389402 ]] [1 0 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 0 0 1 0 0 0 0 1 0]
X.shape:(26, 2)
2. Write a program of the characterise of the dataset including its key, shape, class and
features.
Source Code -:
import mglearn
import matplotlib.pyplot as plt
X,y=mglearn.datasets.make_wave(n_samples=40)
plt.plot(X, y, 'o')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")
plt.show()
Output -:
3. Write a program of the characterise of breast cancer dataset including its key shape ,
class, features.
Source Code -:
import numpy as np
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
print("cancer,keys(): \n{}".format(cancer.keys()))
print("Shape of cancer data: {}".format(cancer.data.shape))
print("Sample count per class: \n{}".format({n: v for n, v in zip(cancer.target_names,
np.bincount(cancer.target))}))
print("Feature name: \n{}".format(cancer.feature_names))

Output-:
cancer,keys():
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename',
'data_module'])
Shape of cancer data: (569, 30)
Sample count per class:
{'malignant': 212, 'benign': 357}
Feature name:
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
'mean smoothness' 'mean compactness' 'mean concavity'
'mean concave points' 'mean symmetry' 'mean fractal dimension'
'radius error' 'texture error' 'perimeter error' 'area error'
'smoothness error' 'compactness error' 'concavity error'
'concave points error' 'symmetry error' 'fractal dimension error'
'worst radius' 'worst texture' 'worst perimeter' 'worst area'
'worst smoothness' 'worst compactness' 'worst concavity'
'worst concave points' 'worst symmetry' 'worst fractal dimension']

4. Write a program to show how KNN classification work with different numbers of
neighbours.
Source Code -:
import mglearn
from sklearn.datasets import fetch_california_housing
import matplotlib.pyplot as plt
housing = fetch_california_housing()
print("Data shape: {}".format(housing.data.shape))
X,y = mglearn.datasets.load_extended_boston()
print("X.shape:{}".format(X.shape))
#mglearn.plots.plot_knn_classification(n_neighbors=1)
mglearn.plots.plot_knn_classification(n_neighbors=3)
plt.show()
Output -:
Data shape: (20640, 8)
X.shape:(506, 104)

5. Write a program to plot training accuracy and test accuracy and regression on dataset of
breast cancer.
Source Code -:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import mglearn
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target,
stratify=cancer.target, random_state=66)
training_accuracy = []
test_accuracy = []
neighbors_settings = range(1, 11)
for n_neighbors in neighbors_settings:
clf = KNeighborsClassifier(n_neighbors=n_neighbors)
clf.fit(X_train, y_train)
training_accuracy.append(clf.score(X_train, y_train))
test_accuracy.append(clf.score(X_test, y_test))
plt.plot(neighbors_settings, training_accuracy, label="Training accuracy")
plt.plot(neighbors_settings, test_accuracy, label="Test accuracy")
plt.ylabel("Accuracy")
plt.xlabel("n_neighbors")
plt.legend()
mglearn.plots.plot_knn_regression(n_neighbors=1)
mglearn.plots.plot_knn_regression(n_neighbors=3)
plt.show()
Output -:
6. Write a program to implement KNN Regressor and also plot it.
Source Code -:
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import mglearn
X, y = mglearn.datasets.make_wave(n_samples=40)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
plt.plot(X_train, y_train, 'o')
plt.plot(X_test, y_test, '+')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")

reg = KNeighborsRegressor(n_neighbors=3)
reg.fit(X_train, y_train)
print("Test Set Predictions:\n", reg.predict(X_test))
print("Test Set R^2: {:.2f}".format(reg.score(X_test, y_test)))
plt.show()
Output -:
Test Set Predictions:
[-0.05396539 0.35686046 1.13671923 -1.89415682 -1.13881398 -1.63113382
0.35686046 0.91241374 -0.44680446 -1.13881398]
Test Set R^2: 0.83

7. Write a program to implement KNN Regressor and also plot it and also find its train test
result.
Source Code -:
from sklearn.neighbors import KNeighborsRegressor
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import mglearn
X, y = mglearn.datasets.make_wave(n_samples=40)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
plt.plot(X_train, y_train, 'o')
plt.plot(X_test, y_test, '+')
plt.ylim(-3, 3)
plt.xlabel("Feature")
plt.ylabel("Target")

reg = KNeighborsRegressor(n_neighbors=3)
reg.fit(X_train, y_train)
print("Test Set Predictions:\n", reg.predict(X_test))
print("Test Set R^2: {:.2f}".format(reg.score(X_test, y_test)))
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
line = np.linspace(-3, 3, 1000).reshape(-1, 1)
for n_neighbors, ax in zip([1, 3, 9], axes):
reg = KNeighborsRegressor(n_neighbors=n_neighbors)
reg.fit(X_train, y_train)
ax.plot(line, reg.predict(line))
ax.plot(X_train, y_train, 'o',c=mglearn.cm2(0), markersize=8)
ax.plot(X_test, y_test, '+',c=mglearn.cm2(1), markersize=8)
ax.set_title(f"n_neighbors = {n_neighbors}\nTrain score: {reg.score(X_train,
y_train):.2f}\nTest score: {reg.score(X_test, y_test):.2f}")
ax.set_ylabel("Target")
axes[0].legend(["Model prediction","Training data/target","Test data/target"],loc="best")
plt.show()
Output -:
Test Set Predictions:
[-0.05396539 0.35686046 1.13671923 -1.89415682 -1.13881398 -1.63113382
0.35686046 0.91241374 -0.44680446 -1.13881398]
Test Set R^2: 0.83
ASSGNMENT -05
1. Write a program using Gaussian NB to find accuracy score, confusion matrix ,actual value ,
predicted value, F1 score.
Source Code -:
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix, ConfusionMatrixDisplay,
f1_score
import matplotlib.pyplot as plt
X, y = make_classification(
n_features=6,
n_classes=3,
n_samples=800,
n_informative=2,
random_state=1,
n_clusters_per_class=1
)
plt.scatter(X[:, 0], X[:, 1], c=y, marker="*")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=125
)
model = GaussianNB()
model.fit(X_train, y_train)
predicted = model.predict([X_test[6]])
print("Actual Value:", y_test[6])
print("Predicted Value:", predicted[0])
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_pred, y_test)
f1 = f1_score(y_pred, y_test, average="weighted")
print("Accuracy:", accuracy)
print("F1 Score:", f1)
labels = [0, 1, 2]
cm = confusion_matrix(y_test, y_pred, labels=labels)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=labels)
disp.plot()
plt.show()

Output -:
Actual Value: 0
Predicted Value: 0
Accuracy: 0.8484848484848485
F1 Score: 0.8491119695890328

ASSGNMENT -06
1. Write a program to implement SVM in dataset and also plot it .
Source Code-:
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
import numpy as np
X, y = make_blobs(centers=4, random_state=8)
y=y%2
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
linear_svm = LinearSVC().fit(X, y)
mglearn.plots.plot_2d_separator(linear_svm, X)
X_new = np.hstack([X, X[:,1:]**2])
figure = plt.figure()
ax = figure.add_subplot(projection='3d', elev=-152, azim=-26)
mask = y ==0
ax.scatter(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b',label='Class 0' , s=60)
ax.scatter(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r',
marker='^',label='Class 1' , s=60)
ax.set_xlabel("Feature 0")
ax.set_ylabel("Feature 1")
ax.set_zlabel("Feature 1 **2")
ax.legend()
plt.show()

Output -:
ASSGNMENT -07
1. Write a program to implement KNN Classifier by taking any dataset.
Source Code -:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
url = "https://raw.githubusercontent.com/plotly/datasets/master/timeseries.csv"
dataset = pd.read_csv(url)
print(dataset.head())
X = dataset.drop(columns=['Date', 'G'])
Y = dataset['G']
Y_binned = pd.cut(Y, bins=3, labels=[0, 1, 2])
X_train, X_test, Y_train, Y_test = train_test_split(X, Y_binned, random_state=0)
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, Y_train)
X_new = pd.DataFrame([[5, 2, 3, 1, 4, 6]], columns=X.columns)
prediction = knn.predict(X_new)
print(f"Prediction: {prediction}")
y_pred = knn.predict(X_test)
print(f"Test set predictions: \n{y_pred}")
print(f"Test set score: {knn.score(X_test, Y_test):.2f}")

Output -:
Date A B C D E F G
0 2008-03-18 24.68 164.93 114.73 26.27 19.21 28.87 63.44
1 2008-03-19 24.18 164.89 114.75 26.22 19.07 27.76 59.98
2 2008-03-20 23.99 164.63 115.04 25.78 19.01 27.04 59.61
3 2008-03-25 24.14 163.92 114.85 27.41 19.61 27.84 59.41
4 2008-03-26 24.44 163.45 114.84 26.86 19.53 28.02 60.09
Prediction: [1]
Test set predictions:
[1 0 1]
Test set score: 1.00

ASSGNMENT -08
1. Write a program to implement SVM in dataset and also plot it in 3D with hyperplane.
Source Code-:
from sklearn.datasets import make_blobs
import mglearn
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
import numpy as np
X, y = make_blobs(centers=4, random_state=8)
y=y%2
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
linear_svm = LinearSVC().fit(X, y)
mglearn.plots.plot_2d_separator(linear_svm, X)
X_new = np.hstack([X, X[:,1:]**2])
figure = plt.figure()
ax = figure.add_subplot(projection='3d', elev=-152, azim=-26)
mask = y ==0
ax.scatter(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b',label='Class 0' , s=60)
ax.scatter(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r',
marker='^',label='Class 1' , s=60)
print(mask)
print(~mask)
ax.set_xlabel("Feature 0")
ax.set_ylabel("Feature 1")
ax.set_zlabel("Feature 1 **2")
linear_svm_3d = LinearSVC().fit(X_new, y)
coef, intercept = linear_svm_3d.coef_.ravel(), linear_svm_3d.intercept_
figure = plt.figure()
ax = figure.add_subplot(projection='3d', elev=-152, azim=-26)
xx = np.linspace(X_new[:, 0].min()-2, X_new[:, 0].max() +2, 50)
yy = np.linspace(X_new[:, 1].min()-2, X_new[:, 1].max() +2, 50)
XX, YY = np.meshgrid(xx, yy)
ZZ = (coef[0] * XX + coef[1] * YY + intercept) / -coef[2]
ax.plot_surface(XX, YY, ZZ, rstride=8, cstride=8, alpha=0.3)
ax.scatter(X_new[mask, 0], X_new[mask, 1], X_new[mask, 2], c='b',label='Class 0' , s=60)
ax.scatter(X_new[~mask, 0], X_new[~mask, 1], X_new[~mask, 2], c='r',
marker='^',label='Class 1' , s=60)
ax.set_xlabel("Feature 0")
ax.set_ylabel("Feature 1")
ax.set_zlabel("Feature 1 **2")
ax.legend()
plt.show()
Output -:
[False True False False False True True False False False True True
False False False True True False False True True False True True
False True True False True False False False False False True True
False True False True False False True True True True False True
False False False True True True False True True True True False
False True False True True False False False True True False True
True False True True False True False False True True False True
False True True True True False False True False True True False
False False False False]
[ True False True True True False False True True True False False
True True True False False True True False False True False False
True False False True False True True True True True False False
True False True False True True False False False False True False
True True True False False False True False False False False True
True False True False False True True True False False True False
False True False False True False True True False False True False
True False False False False True True False True False False True
True True True True]
ASSGNMENT -09
1. Write a program to implement Decision Tree Classifier in python.
Source Code -:
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer
from sklearn.tree import DecisionTreeClassifier, plot_tree
cancer = load_breast_cancer()
X_train,X_test,Y_train,Y_test=train_test_split(cancer.data,cancer.target,stratify=cancer.tar
get,random_state=42)
'''
tree=DecisionTreeClassifier(random_state=0)
tree.fit(X_train,Y_train)
print("Accuracy on trining set: {:.3f}".format(tree.score(X_train,Y_train)))
print("Accuracy on test set: {:.3f}".format(tree.score(X_test,Y_test)))
'''
tree=DecisionTreeClassifier(max_depth=3,random_state=0)
tree.fit(X_train,Y_train)
print("Accuracy on trining set: {:.3f}".format(tree.score(X_train,Y_train)))
print("Accuracy on test set: {:.3f}".format(tree.score(X_test,Y_test)))
from sklearn.tree import export_graphviz
export_graphviz(tree,out_file="tree.dot",class_names=["malignant","benign"],feature_na
mes=cancer.feature_names,impurity=False,filled=True)
import graphviz
with open("tree.dot")as f:
dot_graph=f.read()
print(dot_graph)
plt.figure(figsize=(12, 8))
plot_tree(tree,
filled=True,
feature_names=cancer.feature_names,
class_names=["malignant", "benign"],
rounded=True,
fontsize=10)
plt.title("Decision Tree for Breast Cancer Classification")
plt.show()

Output -:
Accuracy on trining set: 0.977
Accuracy on test set: 0.944
digraph Tree {
node [shape=box, style="filled", color="black", fontname="helvetica"] ;
edge [fontname="helvetica"] ;
0 [label="worst radius <= 16.795\nsamples = 426\nvalue = [159, 267]\nclass = benign",
fillcolor="#afd7f4"] ;
1 [label="worst concave points <= 0.136\nsamples = 284\nvalue = [25, 259]\nclass =
benign", fillcolor="#4ca6e8"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="radius error <= 1.048\nsamples = 252\nvalue = [4, 248]\nclass = benign",
fillcolor="#3c9fe5"] ;
1 -> 2 ;
3 [label="samples = 251\nvalue = [3, 248]\nclass = benign", fillcolor="#3b9ee5"] ;
2 -> 3 ;
4 [label="samples = 1\nvalue = [1, 0]\nclass = malignant", fillcolor="#e58139"] ;
2 -> 4 ;
5 [label="worst texture <= 25.62\nsamples = 32\nvalue = [21, 11]\nclass = malignant",
fillcolor="#f3c3a1"] ;
1 -> 5 ;
6 [label="samples = 12\nvalue = [3, 9]\nclass = benign", fillcolor="#7bbeee"] ;
5 -> 6 ;
7 [label="samples = 20\nvalue = [18, 2]\nclass = malignant", fillcolor="#e88f4f"] ;
5 -> 7 ;
8 [label="texture error <= 0.473\nsamples = 142\nvalue = [134, 8]\nclass = malignant",
fillcolor="#e78945"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
9 [label="samples = 5\nvalue = [0, 5]\nclass = benign", fillcolor="#399de5"] ;
8 -> 9 ;
10 [label="worst concavity <= 0.191\nsamples = 137\nvalue = [134, 3]\nclass =
malignant", fillcolor="#e6843d"] ;
8 -> 10 ;
11 [label="samples = 5\nvalue = [2, 3]\nclass = benign", fillcolor="#bddef6"] ;
10 -> 11 ;
12 [label="samples = 132\nvalue = [132, 0]\nclass = malignant", fillcolor="#e58139"] ;
10 -> 12 ; }
ASSGNMENT -10
1. Write a program to implement K Mean Clustering using python. Given dataset is: A1(2,10);
A2(2,5); A3(8,4); A4(5,8); A5(7,5); A6(6,4); A7(1,2); A8(4,9).
Source code -:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
data = np.array([[2, 10], [2, 5], [8, 4], [5, 8], [7, 5], [6, 4], [1, 2], [4, 9]])
k=3
kmeans = KMeans(n_clusters=k, random_state=0)
kmeans.fit(data)
centroids = kmeans.cluster_centers_
labels = kmeans.labels_
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap='rainbow', marker='o', label='Data
points')
plt.scatter(centroids[:, 0], centroids[:, 1], s=200, c='black', marker='X', label='Centroids')
plt.title(f"K-means Clustering with k={k}")
plt.xlabel('X Coordinate')
plt.ylabel('Y Coordinate')
plt.legend()
plt.show()
print("Cluster Centroids:\n", centroids)
print("Cluster Labels:\n", labels)

Output -:
Cluster Centroids:
[[7. 4.33333333]
[3.66666667 9. ]
[1.5 3.5 ]]
Cluster Labels:
[1 2 0 1 0 0 2 1]
ASSGNMENT -11
1. Write a program to implement Ward’s Algorithm without using linkage and dendrogram.
Given dataset in (x,y): 1(4,4); 2(8,4); 3(15,8); 4(24,12); 5(24,12).
Source Code -:
import numpy as np
import matplotlib.pyplot as plt
def get_data_points():
n = int(input("Enter the number of data points: "))
data = []
print("Enter the coordinates of each data point (x y):")
for _ in range(n):
x, y = map(float, input().split())
data.append((x, y))
return np.array(data)
data = get_data_points()
def euclidean_distance(a, b):
return np.sqrt(np.sum((a - b) ** 2))
n = len(data)
distance_matrix = np.zeros((n, n))
for i in range(n):
for j in range(i + 1, n):
distance_matrix[i, j] = euclidean_distance(data[i], data[j])
distance_matrix[j, i] = distance_matrix[i, j]
clusters = [[i] for i in range(n)]
positions = np.arange(n)
def ward_distance(c1, c2):
combined_cluster = np.vstack((data[c1], data[c2]))
mean_combined = np.mean(combined_cluster, axis=0)
variance = np.sum((combined_cluster - mean_combined) ** 2)
return variance
merge_history = []
heights = []
while len(clusters) > 1:
min_distance = float('inf')
clusters_to_merge = (None, None)
for i in range(len(clusters)):
for j in range(i + 1, len(clusters)):
dist = ward_distance(clusters[i], clusters[j])
if dist < min_distance:
min_distance = dist
clusters_to_merge = (i, j)
points_i = [f"({data[p][0]}, {data[p][1]})" for p in clusters[i]]
points_j = [f"({data[p][0]}, {data[p][1]})" for p in clusters[j]]
print(f"Distance between clusters {points_i} and {points_j}: {dist}")
i, j = clusters_to_merge
new_cluster = clusters[i] + clusters[j]
clusters = [clusters[k] for k in range(len(clusters)) if k not in (i, j)]
clusters.append(new_cluster)
new_position = (positions[i] + positions[j]) / 2
positions = np.delete(positions, [i, j])
positions = np.append(positions, new_position)
merge_history.append((i, j))
heights.append(min_distance)
def plot_dendrogram(merge_history, heights):
plt.figure(figsize=(12, 6))
current_positions = np.arange(n)
colors = plt.cm.viridis(np.linspace(0, 1, len(merge_history)))
for idx, (merge, height) in enumerate(zip(merge_history, heights)):
i, j = merge
plt.plot([current_positions[i], current_positions[i]], [0, height], color=colors[idx])
plt.plot([current_positions[j], current_positions[j]], [0, height], color=colors[idx])
plt.plot([current_positions[i], current_positions[j]], [height, height], color=colors[idx])
new_position = (current_positions[i] + current_positions[j]) / 2
current_positions = np.delete(current_positions, [i, j])
current_positions = np.append(current_positions, new_position)
for idx, pos in enumerate(np.arange(n)):
plt.text(pos, -0.5, f'({data[idx][0]}, {data[idx][1]})',
ha='center', va='top', fontsize=12, color='red')
plt.title("Dendrogram ")
plt.xlabel("Data Points")
plt.ylabel("Distance")
plt.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()
plot_dendrogram(merge_history, heights)
Output -:
Enter the number of data points: 5
Enter the coordinates of each data point (x y):
44
84
15 8
24 4
24 12
Distance between clusters ['(4.0, 4.0)'] and ['(8.0, 4.0)']: 8.0
Distance between clusters ['(15.0, 8.0)'] and ['(24.0, 4.0)']: 48.5
Distance between clusters ['(24.0, 4.0)'] and ['(24.0, 12.0)']: 32.0
Distance between clusters ['(15.0, 8.0)'] and ['(4.0, 4.0)', '(8.0, 4.0)']: 72.66666666666666
Distance between clusters ['(24.0, 4.0)', '(24.0, 12.0)'] and ['(15.0, 8.0)', '(4.0, 4.0)', '(8.0,
4.0)']: 383.2

You might also like