绘制Logistic回归非缩放值

时间:2017-10-04 22:56:34

标签: python scaling logistic-regression

我是Python和编程的新手。我正在上一节关于Logistic回归的课程。下面的代码是正确的,并且绘制得相对不错(不是那么漂亮,但还可以):

# ------ 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()

我的问题是:如何在没有比例的情况下绘制结果?我尝试在代码的几个地方使用invert_transform()方法,但它没有帮助。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的任务就是跟踪缩放和非缩放数据。

虽然没有详细分析您的代码,但基本的想法是:查看使用缩放/未缩放值的位置并根据需要进行调整!

  • 答:经过培训,我们不再需要缩放的X,所以让我们把所有东西都变回来
  • B:但是那种情节在某些np.mesh上使用了分类器,np.mesh本身是由未缩放的数据创建的,所以我们需要再次使用变换器
  • C:注意:基于网格的方法会创建一个密集的网格,如果边界在保持步长的情况下发生变化,则会因内存消耗而导致PC崩溃
    • 这个实际上可以调整(不确定原始值来自哪里),因为情节会得到微妙的变化

所需的变化是:

答:

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')))

enter image description here

虽然原始图表显示了别名的直线(精细的阶梯图案),但现在我们看到了不同的东西。我将为感兴趣的读者留下开放的内容(它与缩放有关!)。