代码之家  ›  专栏  ›  技术社区  ›  Miguel

绘制逻辑回归非标度值

  •  0
  • Miguel  · 技术社区  · 7 年前

    # ------ 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\u transform()方法,但没有任何帮助。

    提前谢谢你。

    1 回复  |  直到 7 年前
        1
  •  0
  •   sascha    7 年前

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

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

    • A: 训练后,我们不再需要缩放X,所以让我们将所有内容转换回原来的状态
    • C: 小心:基于网格的方法是创建密集网格,如果边界在保持步长的同时发生变化,您的电脑将因内存消耗而崩溃
      • 这一个实际上可以调整(不确定原始值来自哪里),因为情节会发生微妙的变化

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

    enter image description here

    虽然原始绘图显示了一条锯齿直线(精细的楼梯图案),但现在我们看到了一些不同的东西。我将把它留给感兴趣的读者(它与缩放有关!)。

    推荐文章