I can't get simple matrix operations to work on data, for the life of me I haven't been able to figure out what 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 by 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, this code
reader = csv.reader(open(dataset1, 'r'))
header = reader.next()
X = np.array([[float(col) for col in row] for row in reader])
print(X.shape)
print(X.T.shape)
print(X * X.T)
gives this output:
(4165, 13)
(13, 4165)
Traceback (most recent call last):
File "gen开发者_开发技巧fromtxt.py", line 17, in <module>
print(X * X.T)
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>>
The problem with the second example seems to be that the operator *
performs element-wise multilpication for NumPy arrays. Presumably you would like to perform a matrix multiplication. There are two options to do this:
Use
numpy.matrix
instead ofnumpy.array
-- then multiplication will be matrix multiplication and powers by integer exponents will work as expected.Use
numpy.dot(A, B)
instead ofA*B
-- this will perform matrix multiplication for both arrays and matrices.
If you have any experience with Matlab and/or Octave this page gives a bunch of useful hints: Link
精彩评论