Skip to content

Commit

Permalink
fix famd on unseen values
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxHalford committed Nov 17, 2024
1 parent e8af795 commit 26eb962
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion prince/famd.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def row_coordinates(self, X):
prop = X_cat.sum() / X_cat.sum().sum() * 2
X_cat = X_cat.sub(X_cat.mean(axis="rows")).div(prop**0.5, axis="columns")

Z = pd.concat([X_num, X_cat], axis=1)
Z = pd.concat([X_num, X_cat.sparse.to_dense()], axis=1).fillna(0.0)

return super().row_coordinates(Z)

Expand Down
30 changes: 30 additions & 0 deletions tests/test_famd.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,33 @@ def test_col_contrib(self):
F = load_df_from_R("famd$var$contrib")
P = self.famd.column_contributions_
np.testing.assert_allclose(F, P * 100)


def test_issue_169():
"""
https://github.com/MaxHalford/prince/issues/169
>>> import pandas as pd
>>> from prince import FAMD
>>> df = pd.DataFrame({'var1':['c', 'a', 'b','c'], 'var2':['x','y','y','z'],'var2': [0.,10.,30.4,0.]})
>>> famd = FAMD(n_components=2, random_state=42)
>>> famd = famd.fit(df[:3])
>>> famd.transform(df[0:3])
component 0 1
0 -1.303760 -0.658334
1 -0.335621 0.981047
2 1.639381 -0.322713
>>> famd.transform(df[0:2])
component 0 1
0 -1.000920 -0.669274
1 -0.092001 0.669274
>>> famd.transform(df[3:])
component 0 1
3 -0.869173 -1.215925e-16
"""
6 changes: 3 additions & 3 deletions tests/test_mca.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import io
import tempfile

import numpy as np
Expand Down Expand Up @@ -194,13 +193,14 @@ def test_type_doesnt_matter():
np.testing.assert_allclose(outputs[i], outputs[i + 1])


issue_161_data = '''
issue_161_data = """
,category,userid,location,applicationname,browser\n
0,Portal Login,a@b.com,"San Jose, CA, United States",A,Chrome\n
1,Application Access,b@b.com,"San Jose, CA, United States",B,Other\n
2,Application Access,a@b.com,"San Jose, CA, United States",C,Other\n
3,Portal Login,c@b.com,"San Diego, CA, United States",A,Chrome\n
'''
"""


def test_issue_161():
"""
Expand Down

0 comments on commit 26eb962

Please sign in to comment.