numpy - csv to matrix in scipy -
i can't simple matrix operations work on data, life of me haven't been able figure out i'm doing incorrectly:
data = np.genfromtxt(dataset1, names=true, delimiter=",", dtype=float) x = np.matrix(data) print(x.t*x) traceback (most recent call last): file "genfromtxt.py", line 11, in <module> print(x.t*x) file "/usr/lib/pymodules/python2.6/numpy/matrixlib/defmatrix.py", line 319, in __mul__ return n.dot(self, asmatrix(other)) typeerror: can't multiply sequence non-int of type 'tuple'
print(data) gives:
[ (3.0, 32.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.5606799999999996, 9.0) (4.0, 43.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.7203099999999996, 16.0) (5.0, 40.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 9.0, 0.0, 5.9964500000000003, 25.0) ..., (5.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2146100000000004, 25.0) (6.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.2915700000000001, 36.0) (7.0, 50.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 12.0, 0.0, 6.3716100000000004, 49.0)]
edit:
further, code
reader = csv.reader(open(dataset1, 'r')) header = reader.next() x = np.array([[float(col) col in row] row in reader]) print(x.shape) print(x.t.shape) print(x * x.t)
gives output:
(4165, 13) (13, 4165) traceback (most recent call last): file "genfromtxt.py", line 17, in <module> print(x * x.t) valueerror: shape mismatch: objects cannot broadcast single shape >>>
the problem second example seems operator *
performs element-wise multilpication numpy arrays. presumably perform matrix multiplication. there 2 options this:
use
numpy.matrix
instead ofnumpy.array
-- multiplication matrix multiplication , powers integer exponents work expected.use
numpy.dot(a, b)
instead ofa*b
-- perform matrix multiplication both arrays , matrices.
Comments
Post a Comment