ML Lab Prgms Split
ML Lab Prgms Split
import pandas as pd
import matplotlib.pyplot as plt #K-Means Clustering
import seaborn as sns
from sklearn.model_selection import train_test_split import pandas as pd
from sklearn.preprocessing import LabelEncoder import numpy as np
from sklearn.tree import DecisionTreeClassifier import seaborn as sns
from sklearn.metrics import classification_report, confusion_matrix, ConfusionMatrixDisplay import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Load the dataset from sklearn.datasets import load_iris
colnames = ['Buying_price', 'maint_cost', 'doors', 'persons', 'lug_boot', 'safety', 'decision'] from sklearn.cluster import KMeans
data = pd.read_csv('car_evaluation.csv', names=colnames, header=None)
plt.figure(figsize=(5, 5)) # Plot the distribution of the 'decision' column # Load the dataset
sns.countplot(x='decision', data=data) X, y = load_iris(return_X_y=True)
plt.title('Count plot for decision') # Initialize and fit KMeans
data.decision.replace('vgood', 'acc', inplace=True) # Simplify the categories in 'decision' kmeans = KMeans(n_clusters=3, random_state=2)
data.decision.replace('good', 'acc', inplace=True) kmeans.fit(X)
new_data = data.apply(LabelEncoder().fit_transform) # Encode categorical features pred = kmeans.predict(X)
x = new_data.drop(['decision'], axis=1) # Separate features and target # Plot the results
y = new_data['decision'] plt.figure(figsize=(12, 5))
# Split the data # Plot for the first two features
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42) plt.subplot(1, 2, 1)
dt = DecisionTreeClassifier(criterion="entropy") # Initialize and fit the Decision Tree model plt.scatter(X[:, 0], X[:, 1], c=pred, cmap=cm.Accent)
dt.fit(x_train, y_train) plt.grid(True)
dt_pred = dt.predict(x_test) # Make predictions # Plot cluster centers
cm = confusion_matrix(y_test, dt_pred) # Display the confusion matrix for center in kmeans.cluster_centers_:
cm_display = ConfusionMatrixDisplay(confusion_matrix=cm) center = center[:2]
cm_display.plot() plt.scatter(center[0], center[1], marker='^', c='red')
plt.show()
plt.xlabel("Petal Length (cm)")
plt.ylabel("Petal Width (cm)")
# Plot for the last two features
#Support Vector Machine plt.subplot(1, 2, 2)
plt.scatter(X[:, 2], X[:, 3], c=pred, cmap=cm.Accent)
import numpy as np plt.grid(True)
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split # Plot cluster centers
from sklearn.svm import SVC for center in kmeans.cluster_centers_:
from sklearn.metrics import confusion_matrix, accuracy_score center = center[2:4]
plt.scatter(center[0], center[1], marker='^', c='red')
dataset = load_digits() # Load the dataset
x_train, x_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.30, plt.xlabel("Sepal Length (cm)")
random_state=4) plt.ylabel("Sepal Width (cm)")
classifier = SVC(kernel="linear") # Initialize and train the SVM classifier plt.show()
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test) # Make predictions
accuracy = accuracy_score(y_test, y_pred) * 100 # Calculate accuracy and confusion matrix
confusion_mat = confusion_matrix(y_test, y_pred)
print("Accuracy for SVM is:", accuracy) # Print the results
print("Confusion Matrix:")
print(confusion_mat)