1

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()
Ruli
  • 2,592
  • 12
  • 30
  • 40
ikarus
  • 11
  • 2
  • I just tried to run the code with Jupyter and I get the Error: The kernel appears to have died. It will restart automatically. – ikarus Dec 18 '20 at 14:58
  • Anyone? I tried to run the code in google colab, it works without problems. But I'd like to understand what the issue with spyder/jupyter is – ikarus Jan 07 '21 at 11:04

1 Answers1

1

I am taking the course from the udemy also, when I split all the code line by line, z = sc.transform(np.array([x1.ravel(),x2.ravel()]).T) when run will consumed more than 50% of the ram by store in ram, so the moment I load classifier.predict(z) then Google Codelab is crash. plt.contourf(x,y,z) is taking 3 dimension for the KNN lesson you learn.

It will be better to test in R program only, since plotting the chart 3D chart will overload the Google codelab.

Kin Siang
  • 2,644
  • 2
  • 4
  • 8
  • 1
    Hi Kin, I managed to make the plot in Python as well. But I had to adjust the resolution of the grid in order to save some RAM. Increasing the stepsize in np.meshgrid() helped – ikarus May 12 '21 at 10:27
  • Hi ikarus, it is nice to hear that, starting from Logistic regression, the chart plot start to perform quite bad in G codelab, where R start to show where it is better than the python in plotting chart. – Kin Siang May 12 '21 at 12:29