Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions factor_analyzer/factor_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ def fit(self, X, y=None):
else:
X = X.copy()

n_features = X.shape[1]
if self.n_factors > n_features:
warnings. warn(
"n_factors ({0}) cannot exceed the number of features ({1});"
"the number of factors will be constrained to {1}.".format(
self.n_factors, n_features
)
)

# now check the array, and make sure it
# meets all of our expected criteria
X = check_array(X, ensure_all_finite="allow-nan", estimator=self, copy=True)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_factor_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_array_almost_equal
from pandas.testing import assert_frame_equal
from sklearn.model_selection import GridSearchCV
Expand Down Expand Up @@ -249,3 +250,10 @@ def test_sufficiency(self):
"tests/expected/test01/sufficiency_ml_none_15_test01.csv"
)
assert_frame_equal(df_computed, df_expected)


def test_n_factors_exceeds_n_features_warns():
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 6))
with pytest.warns(UserWarning):
FactorAnalyzer(n_factors=10, rotation=None).fit(X)