0

在我的 Churn_Modelling.csv 文件上应用一些 LDA 时,一切都很顺利,直到我的 X_train 返回 (8000, 1) 除了 (8000, 2) 如预期的那样:

lda = LDA(n_components = 2)

X_train = lda.fit_transform(X_train, y_train)

X_train 预先进行了“热编码”和“特征缩放”,如下所示:

# LDA

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:, 3:13].values
y = dataset.iloc[:, 13].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X_1 = LabelEncoder()
X[:, 1] = labelencoder_X_1.fit_transform(X[:, 1])
labelencoder_X_2 = LabelEncoder()
X[:, 2] = labelencoder_X_2.fit_transform(X[:, 2])
onehotencoder = OneHotEncoder(categorical_features = [1])
X = onehotencoder.fit_transform(X).toarray()
X = X[:, 1:]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Applying LDA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
lda = LDA(n_components = 2)
X_train = lda.fit_transform(X_train, y_train)
X_test = lda.transform(X_test)

在对其他 .csv 文件执行相同操作时,我没有遇到任何问题……您知道为什么吗?

非常感谢您的帮助!

4

1 回答 1

2

我想我有答案,但如果可能的话,我希望得到确认:-)

我希望使用变换获得的最大列数。是 n-1 因此,在我的情况下,2 个类(True,False)最多产生 1 列(n-1)。

我对吗 ?再次感谢你。

于 2018-04-11T14:38:25.290 回答