0% found this document useful (0 votes)
14 views7 pages

Machine Learning Practicals

The document outlines a practical assignment in Machine Learning conducted by a student named Deepika, detailing various machine learning techniques implemented using Python libraries. It includes steps for importing libraries, generating datasets, splitting data, and applying models such as Naïve Bayes, Linear Regression, Logistic Regression, and clustering methods. The document provides code snippets and expected outputs for each model's performance metrics.

Uploaded by

dy499810
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)
14 views7 pages

Machine Learning Practicals

The document outlines a practical assignment in Machine Learning conducted by a student named Deepika, detailing various machine learning techniques implemented using Python libraries. It includes steps for importing libraries, generating datasets, splitting data, and applying models such as Naïve Bayes, Linear Regression, Logistic Regression, and clustering methods. The document provides code snippets and expected outputs for each model's performance metrics.

Uploaded by

dy499810
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/ 7

Machine Learning Practical

Name - Deepika

roll no- 2022593

Class - BA prog( cs + math )

Year - 3rd year

List of Practical :-

# importing Libraries :-

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression, LogisticRegression, Ridge, Lasso

from sklearn.preprocessing import PolynomialFeatures, StandardScaler

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier


from sklearn.tree import DecisionTreeClassifier

from sklearn.svm import SVC

from sklearn.cluster import KMeans, AgglomerativeClustering

from sklearn.neural_network import MLPClassifier

from sklearn.metrics import classification_report, mean_squared_error, accuracy_score

# Generate random datasets

np.random.seed(42)

X = np.random.rand(200, 5) # Common features for all tasks

y_classification = np.random.randint(0, 2, 200) # Binary classification target

y_regression = 5 * X[:, 0] + 3 * X[:, 1] + np.random.randn(200) # Regression target

# Split datasets for training and testing

X_train_clf, X_test_clf, y_train_clf, y_test_clf = train_test_split(X, y_classification, test_size=0.2,


random_state=42)

X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(X, y_regression, test_size=0.2,


random_state=42)

# 1. Naïve Bayes Classifier

nb_model = GaussianNB()

nb_model.fit(X_train_clf, y_train_clf)

print("Naïve Bayes Accuracy:", accuracy_score(y_test_clf, nb_model.predict(X_test_clf)))

output
# 2. Simple Linear Regression (Single Feature)

lr_simple = LinearRegression()

lr_simple.fit(X_train_reg[:, [0]], y_train_reg)

print("Simple Linear Regression MSE:", mean_squared_error(y_test_reg,


lr_simple.predict(X_test_reg[:, [0]])))

output

# 3. Multiple Linear Regression

lr_multiple = LinearRegression()

lr_multiple.fit(X_train_reg, y_train_reg)

print("Multiple Linear Regression MSE:", mean_squared_error(y_test_reg,


lr_multiple.predict(X_test_reg)))

output

# 4. Polynomial Regression (degree=2)

poly = PolynomialFeatures(degree=2)

X_poly_train = poly.fit_transform(X_train_reg)

X_poly_test = poly.transform(X_test_reg)

poly_model = LinearRegression()
poly_model.fit(X_poly_train, y_train_reg)

print("Polynomial Regression MSE:", mean_squared_error(y_test_reg,


poly_model.predict(X_poly_test)))

output

# 5. Lasso and Ridge Regression

lasso_model = Lasso(alpha=0.1)

lasso_model.fit(X_train_reg, y_train_reg)

ridge_model = Ridge(alpha=0.1)

ridge_model.fit(X_train_reg, y_train_reg)

print("Lasso Regression MSE:", mean_squared_error(y_test_reg,


lasso_model.predict(X_test_reg)))

print("Ridge Regression MSE:", mean_squared_error(y_test_reg,


ridge_model.predict(X_test_reg)))

output

# 6. Logistic Regression

logistic_model = LogisticRegression(max_iter=1000)

logistic_model.fit(X_train_clf, y_train_clf)

print("Logistic Regression Accuracy:", accuracy_score(y_test_clf,


logistic_model.predict(X_test_clf)))
output

# 7. Artificial Neural Network (MLP)

ann_model = MLPClassifier(max_iter=500)

ann_model.fit(X_train_clf, y_train_clf)

print("ANN Accuracy:", accuracy_score(y_test_clf, ann_model.predict(X_test_clf)))

output

# 8. KNN Classifier

knn_model = KNeighborsClassifier(n_neighbors=5)

knn_model.fit(X_train_clf, y_train_clf)

print("K-NN Accuracy:", accuracy_score(y_test_clf, knn_model.predict(X_test_clf)))

output

# 9. Decision Tree Classification

tree_model = DecisionTreeClassifier()

tree_model.fit(X_train_clf, y_train_clf)

print("Decision Tree Accuracy:", accuracy_score(y_test_clf, tree_model.predict(X_test_clf)))

output
# 10. SVM Classification

svm_model = SVC(probability=True)

svm_model.fit(X_train_clf, y_train_clf)

print("SVM Accuracy:", accuracy_score(y_test_clf, svm_model.predict(X_test_clf)))

output

# 11. K-Means Clustering

kmeans = KMeans(n_clusters=3, random_state=42)

kmeans.fit(X)

print("K-Means Cluster Centers:", kmeans.cluster_centers_)

output

# 12. Hierarchical Clustering

hc = AgglomerativeClustering(n_clusters=3)

hc_labels = hc.fit_predict(X)

print("Hierarchical Clustering Labels:", np.unique(hc_labels))


output

You might also like