Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Map Feature
def map_feature(x1, x2):
'''
Maps the two input features to quadratic features.
Returns a new feature array with more features, comprising of
X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc...
Inputs X1, X2 must be the same size
'''
x1.shape = (x1.size, 1)
x2.shape = (x2.size, 1)
degree = 6
out = ones(shape=(x1[:, 0].size, 1))
m, n = out.shape
for i in range(1, degree + 1):
for j in range(i + 1):
r = (x1 ** (i - j)) * (x2 ** j)
out = append(out, r, axis=1)
return out
#load the dataset
data = loadtxt('ex2data2.txt', delimiter=',')
X = data[:, 0:2]
y = data[:, 2]
pos = where(y == 1)
neg = where(y == 0)
scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
xlabel('Microchip Test 1')
ylabel('Microchip Test 2')
legend(['y = 1', 'y = 0'])
#show()
m, n = X.shape
y.shape = (m, 1)
it = map_feature(X[:, 0], X[:, 1])

Hi, your code serves as a perfect learning material!
I don't get the point of setting y.shape=(m,1). Isn't y already of that size?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment