简体   繁体   English

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

[英]LDA(n_components = 2) + fit_transform return 1-dim matrix instead of 2-dim

While applying some LDA on my Churn_Modelling.csv file, eveything goes well until the point where my X_train return (8000, 1) except of (8000, 2) as expected : 在我的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 is before-hand "hot-encoded" and "feature scaled" as followed : 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)

While doing the same on an other .csv file I have no troubles... do you have any idea why ? 在其他.csv文件上执行相同操作时,我没有遇到任何麻烦...您知道为什么吗?

Thank you very very much for your help ! 非常感谢您的帮助!

I think I have the answer but I would prefer to have confirmation if possible :-) 我想我有答案,但如果可能的话,我希望得到确认:-)

The maximal number of columns I can hope to obtain using transform. 我希望使用transform可以获得的最大列数。 is n-1 so, in my case, 2 classes (True, False) yields maximally 1 column (n-1). 是n-1,因此,在我的情况下,2个类(真,假)最多产生1列(n-1)。

Am I right ? 我对吗 ? Thank you again. 再次感谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM