3

my code is pretty simple, but when I try to multiply a 3x2 and 2x1 matrix, I get the following error (which, to me, makes no sense):

ValueError: operands could not be broadcast together with shapes (3,2) (2,1) 

In this program, the first thing I do is randomly generate two points in the domain [-1,1] x [-1,1], and define a line by these points, using the variables slope and y_int. Then, I create N random x values of the form {x_0, x_1, x_2} where x_0 is always 1, and x_1,x_2 are randomly generated numbers in the range [-1,1]. These N values comprise the x_matrix in the code.

The y_matrix is the classification of each of the values x_1,...,x_N. If x_1 is to the right of the random line specified by slope and y_int, then the value of y_1 is +1, and is otherwise -1.

Now, once x_matrix and y_matrix have been specified, I just want to multiply the pseudo-inverse of x_matrix (pinv_x in the code) by y_matrix. This is where the error comes in. I'm at my wit's end and I cannot think of anything that could be wrong.

Any help is greatly appreciated. The code is below:

from numpy import *
import random

N = 2

# Determine target function f(x)                                                                                
x_1 = [random.uniform(-1,1),random.uniform(-1,1)]
x_2 = [random.uniform(-1,1),random.uniform(-1,1)]
slope = (x_1[1] - x_2[1]) / (x_1[0] - x_2[0])
y_int = x_1[1] - (slope * x_1[0])

# Construct training data.                                                                                       
x_matrix = array([1, random.uniform(-1,1), random.uniform(-1,1)])
x_on_line = (x_matrix[1] / slope) - (y_int / slope)
if x_matrix[1] >= x_on_line:
    y_matrix = array([1])
else:
    y_matrix = array([-1])

for i in range(N-1):
    x_val = array([1, random.uniform(-1,1), random.uniform(-1,1)])
    x_matrix = vstack((x_matrix, x_val))
    x_on_line = (x_val[1] / slope) - (y_int / slope)
    if x_val[1] >= x_on_line:
    y_matrix = vstack((y_matrix, array([1])))
    else:
        y_matrix = vstack((y_matrix, array([-1])))

pinv_x = linalg.pinv(x_matrix)
print y_matrix
print pinv_x
w =  pinv_x*y_matrix
4

1 回答 1

5

您使用的是数组,而不是矩阵。要从数组中获取矩阵乘法,您需要使用dot()函数,而不是*. 请参阅此页面。当*数据在数组中时,运算符是逐元素乘法,因此形状必须完全匹配。

于 2012-04-13T16:56:48.243 回答