ML File
ML File
ML File
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()
cursor.execute(query)
rows = cursor.fetchall()
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)
linear_regressor = LinearRegression()
linear_regressor.fit(X_train, y_train)
y_pred = linear_regressor.predict(X_test)
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
Tamanna
logistic_regressor = LogisticRegression()
logistic_regressor.fit(X_train, y_train)
y_pred = logistic_regressor.predict(X_test)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)
plt.figure(figsize=(10, 6))
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']
naive_bayes_classifier = GaussianNB()
naive_bayes_classifier.fit(X_train, y_train)
y_pred = naive_bayes_classifier.predict(X_test)
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
knn_classifier = KNeighborsClassifier(n_neighbors=5)
knn_classifier.fit(X_train, y_train)
knn_pred = knn_classifier.predict(X_test)
svm_pred = svm_classifier.predict(X_test)
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
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)
OUTPUT:
Tamanna