LDA(n_components = 2)+ fit_transform返回1-dim矩阵而不是2-dim

时间:2018-04-11 14:28:01

标签: python python-3.x scikit-learn lda churn

在我的Churn_Modelling.csv文件中应用一些LDA时,eveything一直顺利,直到我的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文件上做同样的事情我没有麻烦......你知道为什么吗?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

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

我希望使用transform获得的最大列数。是n-1所以,在我的例子中,2个类(True,False)最多产生1列(n-1)。

我是对的吗?再次感谢你。