user1072337
user1072337

Reputation: 12945

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.

Upvotes: 2

Views: 1699

Answers (3)

Carlos Pimentel
Carlos Pimentel

Reputation: 489

For your code to work please see below tested code and the line you should change!

#TODO: Transform log_samples using the PCA fit above

pca_samples = pca.fit_transform(log_samples)

The above code works very well.

Upvotes: 0

GoingMyWay
GoingMyWay

Reputation: 17478

pca.fit(X[, y]) just fit the model with X, and return the self, that is pca itself.

Since that you want to get the transformed data with

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

So, you should call pca.fit_transform()

fit_transform(X[, y]) Fit the model with X and apply the dimensionality reduction on X.

See the docs of pca, and fit_transform

Upvotes: 3

tdelaney
tdelaney

Reputation: 77407

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.

Upvotes: 0

Related Questions