ML File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PRACTICAL 1

Extract the data from the database using python.

CODE:
import mysql.connector

try:
conn = mysql.connector.connect(
host="localhost",
user="root",
password="tamanna@MySQL#7903",
database="college"
)

if conn.is_connected():
print("Connected to MySQL database")

cursor = conn.cursor()

query = "SELECT * FROM students"

cursor.execute(query)

rows = cursor.fetchall()

print("Data from students table:")


for row in rows:
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Course:
{row[3]}")

except mysql.connector.Error as err:


print(f"Error: {err}")

finally:
if conn.is_connected():
cursor.close()
conn.close()
print("MySQL connection is closed")

OUTPUT:

Tamanna
PRACTICAL 2
Write a program to implement linear and logistic regression.

Tamanna
CODE:
Linear Regression Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

linear_regressor = LinearRegression()

linear_regressor.fit(X_train, y_train)

y_pred = linear_regressor.predict(X_test)

print("Linear Regression Coefficients (Slope):",


linear_regressor.coef_)
print("Linear Regression Intercept:", linear_regressor.intercept_)
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R^2 Score:", r2_score(y_test, y_pred))

plt.scatter(X_test, y_test, color='blue', label="Actual Data")


plt.plot(X_test, y_pred, color='red', label="Regression Line")
plt.xlabel("X")
plt.ylabel("y")
plt.title("Linear Regression")
plt.legend()
plt.show()

OUTPUT:

Tamanna
Logistic Regression Implementation
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
import matplotlib.pyplot as plt

iris = load_iris()

X = iris.data[:, :2]
y = (iris.target != 0) * 1

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

Tamanna
logistic_regressor = LogisticRegression()

logistic_regressor.fit(X_train, y_train)

y_pred = logistic_regressor.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


conf_matrix = confusion_matrix(y_test, y_pred)

print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)

plt.figure(figsize=(10, 6))

plt.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap="viridis",


label="Test Data")

x0, x1 = np.meshgrid(np.linspace(X[:, 0].min(), X[:, 0].max(), 500),


np.linspace(X[:, 1].min(), X[:, 1].max(), 500))

X_new = np.c_[x0.ravel(), x1.ravel()]


y_predict_prob = logistic_regressor.predict_proba(X_new)[:, 1]
zz = y_predict_prob.reshape(x0.shape)

contour = plt.contourf(x0, x1, zz, levels=[0, 0.5, 1], cmap="RdYlBu",


alpha=0.8)

plt.xlabel("Sepal Length")
plt.ylabel("Sepal Width")
plt.title("Logistic Regression - Decision Boundary")
plt.colorbar(contour)
plt.show()

OUTPUT:

Tamanna
PRACTICAL 3

Tamanna
Write a program to implement the naïve Bayesian classifier for a sample training data set stored
as a .CSV file. Compute the accuracy of the classifier, considering few test data sets.

CODE:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix

data = pd.read_csv(r'C:\Users\skuma\Downloads\Coding\Code\Python
Programs\student_data.csv') # Ensure the CSV file is in the same
directory

X = data[['Hours_Studied', 'Attendance']]
y = data['Pass/Fail']

y = y.map({'Pass': 1, 'Fail': 0})

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42, stratify=y)

naive_bayes_classifier = GaussianNB()

naive_bayes_classifier.fit(X_train, y_train)

y_pred = naive_bayes_classifier.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


conf_matrix = confusion_matrix(y_test, y_pred, labels=[0, 1])

print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)

OUTPUT:

PRACTICAL 4

Tamanna
Write a program to implement k-nearest neighbors (KNN) and Support Vector Machine (SVM)
Algorithm for classification

CODE:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

knn_classifier = KNeighborsClassifier(n_neighbors=5)
knn_classifier.fit(X_train, y_train)

svm_classifier = SVC(kernel='linear', random_state=42) # Linear kernel


svm_classifier.fit(X_train, y_train)

knn_pred = knn_classifier.predict(X_test)
svm_pred = svm_classifier.predict(X_test)

knn_accuracy = accuracy_score(y_test, knn_pred)


svm_accuracy = accuracy_score(y_test, svm_pred)

knn_conf_matrix = confusion_matrix(y_test, knn_pred)


svm_conf_matrix = confusion_matrix(y_test, svm_pred)

print("K-Nearest Neighbors (KNN) Accuracy:", knn_accuracy)


print("K-Nearest Neighbors (KNN) Confusion Matrix:\n", knn_conf_matrix)

print("\nSupport Vector Machine (SVM) Accuracy:", svm_accuracy)


print("Support Vector Machine (SVM) Confusion Matrix:\n",
svm_conf_matrix)

OUTPUT:

Tamanna
PRACTICAL 5
Implement classification of a given dataset using random forest.

Tamanna
CODE:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix

iris = load_iris()
X = iris.data
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

random_forest_classifier = RandomForestClassifier(n_estimators=100,
random_state=42) # 100 trees in the forest

random_forest_classifier.fit(X_train, y_train)

y_pred = random_forest_classifier.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


conf_matrix = confusion_matrix(y_test, y_pred)

print("Random Forest Accuracy:", accuracy)


print("Confusion Matrix:\n", conf_matrix)

OUTPUT:

Tamanna

You might also like