How to define X as the columns values?

Viewed 42

I am performing PCA analysis and getting the number of components equal to the number of samples, not the number of features. (Data does not have labels)

The data was given in excel as rows: features, columns: samples

I transposed the data and then defined X as the columns (features)

When performing PCA I understand that the # of PC is equal to the number of features but I am not getting this.

Very new with Python, excuse my ignorance with these basics.

df = pd.read_excel(excel_file)

# STEP 1

# Transposing the data
data = df.T
print('\nData Shape (row, columns):', data.shape)

# STEP 2
# Data preprocessing

# Separating out the features
X = data.iloc[:, 0:4001]
print('\nX shape:', X.shape)
print('X size:', X.size)

# Standardizing the features
X_scaled = StandardScaler().fit_transform(X)

# Dimension reduction
pca = PCA()
X_pca = pca.fit_transform(X_scaled)
print('\nNumber of principal components:', pca.n_components_)

OUTPUT:

Data Shape (row, columns): (120, 4001)

X shape: (120, 4001) X size: 480120

Number of principal components: 120

Thank you for your help!!!

1 Answers

So, there was nothing wrong with my code. The logic behind this is that number of principal components cannot exceed the number of samples. You cannot project 120 samples to a hyperplane of more than 120 dimensions.

Related