我是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()方法,但它没有帮助。
提前谢谢。
答案 0 :(得分:0)
您的任务就是跟踪缩放和非缩放数据。
虽然没有详细分析您的代码,但基本的想法是:查看使用缩放/未缩放值的位置并根据需要进行调整!
所需的变化是:
答:
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')))
虽然原始图表显示了别名的直线(精细的阶梯图案),但现在我们看到了不同的东西。我将为感兴趣的读者留下开放的内容(它与缩放有关!)。