【发布时间】:2017-10-04 22:56:34
【问题描述】:
我是 Python 和一般编程的新手。我正在上一门关于逻辑回归的课程。下面的代码是正确的,并且绘图相对不错(不是那么漂亮,但还可以):
# ------ 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() 方法,但没有帮助。
提前谢谢你。
【问题讨论】:
-
I tried using invert_transform() method in several places along the code but it didn't help不是很有帮助,并且此代码不可重现,因为我们没有您的数据。也许你想改变它,虽然 invert_transform 在正确的地方应该是正确的(但不确定你在做什么)。 -
“绘制结果”过于模糊,无法给出明确的答案,但无论您做什么,都需要单独存储预先缩放的数据。例如,不要覆盖
X_train和X_test,而是创建X_train_sc和X_test_sc等新变量,然后将其输入到分类器中。 -
@BrendenPetersen 他可以覆盖它们,因为对它们进行逆变换所需的一切都存储在
sc_X中(但是是的;这种覆盖可能会使代码不那么直观)。 -
对不起,我对整个宇宙真的很陌生。我会尝试更具体。我正在尝试绘制除以预测线的两个区域图(这更清楚吗?)我可以用当前代码做到这一点,但我想显示没有比例的结果,因为看到年龄并不好: -3、2 和薪水:-3、3 或类似的东西。这是数据link
-
添加一个我们可以运行或添加数据的最小示例。并且可能描述你失败的尝试?这 2 条建议有哪些不清楚的地方?
标签: python scaling logistic-regression