I have problem with my Python code, which I execute in Spyder. I'm somewhat of a coding noob, so for practicing purpose I wrote a classification code using KNN. The code works well until the point where I want to visualise the results. The problem starts when I run the
plt.contourf(...)
function.
What happens is that all the previously created variables disappear, the current line in the console goes to [1] and the square for canceling a running process becomes red. And I need to restart the Kernel..
I tried to isolate the problem and I found a weird behavior. For instance if I execute only a part of the plt.contourf(...)
in the console, e.g. only:
np.array([X1.ravel(), X2.ravel()])
I get the expected result. However, if I execute
np.array([X1.ravel(), X2.ravel()])
a second time I get again the same breakdown that I get when I execute the entire code. Might this problem be related to memory usage/storage? Can anyone help me out please?
Here is the full code:
#KNN
#Import libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#Import data
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:,0:2].values
y = dataset.iloc[:,-1].values
#Splitting data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size = 0.25,
random_state = 0)
#Feature scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#Training the KNN model
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors = 5,
p = 2,
metric = 'minkowski')
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
acc = accuracy_score(y_test, y_pred)
# Visualising the Training set results
from matplotlib.colors import ListedColormap
X_set, y_set = sc.inverse_transform(X_train), y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 10, stop = X_set[:, 0].max() + 10, step = 0.25),
np.arange(start = X_set[:, 1].min() - 1000, stop = X_set[:, 1].max() + 1000, step = 0.25))
plt.contourf(X1, X2, classifier.predict(sc.transform(np.array([X1.ravel(), X2.ravel()]).T)).reshape(X1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Logistic Regression (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()