my code run with no problems. what I was trying to do is add some noise to my dataset which I couldn't do .
so how can I do that ?
I tried many approach to do that : -creating a noise function -adding directly some noise to my dataset But all of them didn't work.
All I am asking about is how can i do that to my code below ?
This is the code for testing and ploting ,it works just fine But what I am trying is to add noise to my data set , and that what i couldn't do .
import numpy as np
from SVM_M_M import SVM
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# Testing
if __name__ == "__main__":
X, y = datasets.make_blobs(
n_samples=250, n_features=2, centers=2, cluster_std=1.05, random_state=1
)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, shuffle=True, random_state=1)
clf = SVM(n_iters=1000)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
def accuracy(y_true, y_pred):
accuracy = np.sum(y_true==y_pred) / len(y_true)
return accuracy
print("SVM Accuracy: ", accuracy(y_test, predictions))
# plot results
def get_hyperplane(x, w, b, offset):
return (-w[0] * x - b + offset) / w[1]
fig, ax = plt.subplots(1, 1, figsize=(10,6))
plt.set_cmap('PiYG')
plt.scatter(X_train[:, 0], X_train[:, 1], marker='o', c=y_train, s=100, alpha=0.75)
plt.scatter(X_test[:, 0], X_test[:, 1], marker="x", c=y_test, s=100, alpha=0.75)
Y = np.random.normal(2 * X + 2, 20)
x0_1 = np.amin(X_train[:, 0])
x0_2 = np.amax(X_train[:, 0])
x1_1 = get_hyperplane(x0_1, clf.w, clf.b, 0)
x1_2 = get_hyperplane(x0_2, clf.w, clf.b, 0)
x1_1_m = get_hyperplane(x0_1, clf.w, clf.b, -1)
x1_2_m = get_hyperplane(x0_2, clf.w, clf.b, -1)
x1_1_p = get_hyperplane(x0_1, clf.w, clf.b, 1)
x1_2_p = get_hyperplane(x0_2, clf.w, clf.b, 1)
ax.plot([x0_1, x0_2], [x1_1, x1_2], "-", c='k', lw=1, alpha=0.9)
ax.plot([x0_1, x0_2], [x1_1_m, x1_2_m], "--", c='grey', lw=1, alpha=0.8)
ax.plot([x0_1, x0_2], [x1_1_p, x1_2_p], "--", c='grey', lw=1, alpha=0.8)
x1_min = np.amin(X[:, 1])
x1_max = np.amax(X[:, 1])
ax.set_ylim([x1_min - 3, x1_max + 3])
for spine in ['top','right']:
ax.spines[spine].set_visible(False)
plt.show()