Search code examples
pythonscikit-learnsklearn-pandas

TypeError: unsupported operand type(s) for *: 'PCA' and 'float'


EDIT:

Here is the head of the data csv:

    Fresh   Milk    Grocery Frozen  Detergents_Paper    Delicatessen
0   12669   9656    7561    214 2674    1338
1   7057    9810    9568    1762    3293    1776
2   6353    8808    7684    2405    3516    7844
3   13265   1196    4221    6404    507 1788
4   22615   5410    7198    3915    1777    5185

Error I'm seeing:

TypeError: unsupported operand type(s) for *: 'PCA' and 'float'

Code:

from sklearn.decomposition import PCA

log_data = np.log(data)

# TODO: Apply PCA to the good data with the same number of dimensions as features
pca = PCA(n_components=4)

# TODO: Apply a PCA transformation to the sample log-data
pca_samples = pca.fit(log_data)

# Generate PCA results plot
pca_results = rs.pca_results(good_data, pca)

display(pd.DataFrame(np.round(pca_samples, 4), columns = pca_results.index.values))

It's complaining about the last line

data is from a csv that has been shown to work fine.


Solution

  • PCA.fit() tansforms the model in-place and returns self so that you can chain other model operations. So, after

    pca_samples = pca.fit(log_data)
    

    pca_samples is just another reference to pca.