diff --git a/factor_analyzer/factor_analyzer.py b/factor_analyzer/factor_analyzer.py index e1f6f67..524ea5e 100644 --- a/factor_analyzer/factor_analyzer.py +++ b/factor_analyzer/factor_analyzer.py @@ -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) diff --git a/tests/test_factor_analyzer.py b/tests/test_factor_analyzer.py index e8306a8..bbf212e 100644 --- a/tests/test_factor_analyzer.py +++ b/tests/test_factor_analyzer.py @@ -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 @@ -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)