我是新来的Python和一般编程。我正在上一堂关于Logistic回归的课。下面的代码是正确的,情节比较不错的(不是很漂亮,但是OK):绘制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()方法,但它没有帮助。
预先感谢您。
''我试着在代码中的几处使用invert_transform()方法,但它没有帮助''不是很有帮助,而且这个代码不可重现,因为我们没有你的数据。也许你想改变这一点,虽然在正确的地方invert_transform应该是正确的(但不知道你在做什么)。 – sascha
“绘制结果”太模糊,无法给出明确的答案,但无论您做什么,都需要分别存储预缩放数据。例如,不要覆盖'X_train'和'X_test',而是创建像'X_train_sc'和'X_test_sc'这样的新变量,然后将其输入到分类器中。 –
@BrendenPetersen他可以覆盖它们,因为逆向变换所需的所有东西都存储在'''sc_X'''中(但这样的覆盖可能会使代码更不直观)。 – sascha