私は一般的にはPythonとプログラミングの初心者です。私はロジスティック回帰についてのクラスを取っています。以下のコードは正しく、比較的素敵(ないとても美しい、しかしOK)プロット:ロジスティック回帰プロット非スケーリングされた値
# ------ 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