November 24, 2023
SVM
# Import necessary libraries
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
# Load the dataset
X = np.loadtxt("/home/ise/Downloads/glas.csv", delimiter=",", skiprows=1)
y = X[:, -1]
X = X[:, :-1]
# Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
# Create the SVM model with RBF kernel
svm_model = SVC(kernel="rbf")
# Train the SVM model
svm_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_model.predict(X_test)
# Calculate the accuracy
accuracy = np.mean(y_pred == y_test)
# Print the accuracy
print(f" RBF Accuracy: {accuracy}")#Create the SVM model with linear kernel
svm_model = LinearSVC()
# Train the SVM model
svm_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_model.predict(X_test)
# Calculate the accuracy
accuracy = np.mean(y_pred == y_test)
# Print the accuracy
print(f"Accuracy: {accuracy}")# Create the SVM model with polynomial kernel
svm_model = SVC(kernel="poly", degree=3)
# Train the SVM model
svm_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_model.predict(X_test)
# Calculate the accuracy
accuracy = np.mean(y_pred == y_test)
# Print the accuracy
print(f"Accuracy: {accuracy}")
# Create the SVM model with sigmoid kernel
svm_model = SVC(kernel="sigmoid")
# Train the SVM model
svm_model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = svm_model.predict(X_test)
# Calculate the accuracy
accuracy = np.mean(y_pred == y_test)
# Print the accuracy
print(f"Accuracy: {accuracy}")1)svm using rbf 2)svm using linear 3)svm using polynomial 4) svm using sigmoid