4.3 KiB
4.3 KiB
In [ ]:
#!pip install mlxtendIn [ ]:
import pandas as pd
import numpy as npIn [ ]:
df = pd.read_csv('datasets-1/sms_spam_svm.csv')In [ ]:
y = df.iloc[:, 0].values
y = np.where(y == 'spam', -1, 1)
x = df.iloc[:, [1, 2]].valuesIn [ ]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
x, y, test_size=0.3, random_state=0)In [ ]:
from sklearn.svm import SVC
# linear, poly, rbf, sigmoid
svm = SVC(kernel='rbf', gamma='auto', random_state=1)
svm.fit(X_train, y_train)
y_pred = svm.predict(X_test)In [ ]:
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt
X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X_combined, y_combined, clf=svm)
plt.xlabel('suspect words')
plt.ylabel('spam or ham')
plt.legend(loc='upper right')
plt.tight_layout()
plt.show()In [ ]:
from sklearn.metrics import accuracy_score
print('Misclassified samples: %d' % (y_test != y_pred).sum())
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))In [ ]:
from sklearn.metrics import classification_report , confusion_matrix
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
print("\nClassification Report:")
print(classification_report(y_test, y_pred))In [ ]:
In [ ]:
In [ ]: