I'm trying to do PCA on a sparse matrix, but I am encountering an error:
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
Here is my code:
import sys
import csv
from sklearn.decomposition import PCA
data_sentiment = []
y = []
data2 = []
csv.field_size_limit(sys.maxint)
with open('/Users/jasondou/Google Drive/data/competition_1/speech_vectors.csv') as infile:
reader = csv.reader(infile, delimiter=',', quotechar='|')
n = 0
for row in reader:
# sample = row.split(',')
n += 1
if n%1000 == 0:
print n
data_sentiment.append(row[:25000])
pca = PCA(n_components=3)
pca.fit(data_sentiment)
PCA(copy=True, n_components=3, whiten=False)
print(pca.explained_variance_ratio_)
y = pca.transform(data_sentiment)
The input data is speech_vector.csv, which a 2740 * 50000 matrix found available here
Here is the full error traceback:
Traceback (most recent call last):
File "test.py", line 45, in <module>
y = pca.transform(data_sentiment)
File "/Users/jasondou/anaconda/lib/python2.7/site-packages/sklearn/decomposition/pca.py", line 397, in transform
X = X - self.mean_
TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'numpy.ndarray'
I do not quite understand what self.mean_ refers to here.
pca.fitpca.fit()orpca.transform()); I don't see any subtraction operations that might have raised this error directly in this top-level code.PCAis imported.data_sentimentlooks like. Could you please add a few rows fromdata_sentimentto your question. Also, please edit your question to contain the full traceback for the error message you are seeing.