I'm new to Python and programming in general. I'm taking a class about Logistic Regression. The code below is correct and plots relatively nice (not so beautiful, but OK):
# ------ LOGISTIC REGRESSION ------ #
# --- Importing the Libraries --- #
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap
# --- Importing the Dataset --- #
path = '/home/bohrz/Desktop/Programação/Machine Learning/Part 3 - ' \
'Classification/Section 14 - Logistic Regression/Social_Network_Ads.csv'
dataset = pd.read_csv(path)
X = dataset.iloc[:, 2:4].values
y = dataset.iloc[:, -1].values
# --- Splitting the Dataset into Training and Test set --- #
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25,
random_state=0)
# --- Feature Scaling --- #
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
# --- Fitting the Logistic Regression Model to the Dataset --- #
classifier = LogisticRegression(random_state=0)
classifier.fit(X_train, y_train)
# --- Predicting the Test set results --- #
y_pred = classifier.predict(X_test)
# --- Making the Confusion Matrix --- #
cm = confusion_matrix(y_test, y_pred)
# --- Visualizing Logistic Regression results --- #
# --- Visualizing the Training set results --- #
X_set_train, y_set_train = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start=X_set_train[:, 0].min(),
stop=X_set_train[:, 0].max(), step=0.01),
np.arange(start=X_set_train[:, 1].min(),
stop=X_set_train[:, 1].max(), step=0.01))
# Building the graph contour based on classification method
Z_train = np.array([X1.ravel(), X2.ravel()]).T
plt.contourf(X1, X2, classifier.predict(Z_train).reshape(X1.shape), alpha=0.75,
cmap=ListedColormap(
('red', 'green')))
# Apply limits when outliers are present
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
# Creating the scatter plot of the Training set results
for i, j in enumerate(np.unique(y_set_train)):
plt.scatter(X_set_train[y_set_train == j, 0], X_set_train[y_set_train == j,
1],
c=ListedColormap(('red', 'green'))(i), label=j)
plt.title('Logistic Regression (Trainning set results)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()
My question is: how do I plot the results with no scale? I tried using invert_transform() method in several places along the code but it didn't help.
Thank you in advance.
Your task is just about keeping track of scaled- and non-scaled data.
While not analyzing your code in detail, the basic idea is just: Look where scaled/unscaled values are used and adjust if needed!
So the needed changes are:
A:
y_pred = classifier.predict(X_test) # YOUR CODE
X_train = sc_X.inverse_transform(X_train) # transform back
X_test = sc_X.inverse_transform(X_test) # """
C:
X1, X2 = np.meshgrid(np.arange(start=X_set_train[:, 0].min(),
stop=X_set_train[:, 0].max(), step=10.), #!!! 0.01 ),
np.arange(start=X_set_train[:, 1].min(),
stop=X_set_train[:, 1].max(), step=0.1)) #!!! 0.01))
B:
Z_train = np.array([X1.ravel(), X2.ravel()]).T
plt.contourf(X1, X2, classifier.predict(sc_X.transform(Z_train)).reshape(X1.shape), # TRANFORM Z
alpha=0.75,
cmap=ListedColormap(
('red', 'green')))
While the original plot displayed an aliased straight line (fine stair pattern), now we see something different. I will leave that open for the interested reader (it's connected to the scaling!).