Skip to content

Instantly share code, notes, and snippets.

@supereng
Forked from marcelcaraciolo/mapfeature.py
Created March 11, 2017 22:55
Show Gist options
  • Save supereng/5b0cb042a3c731e6dbd8cb6812c0ca60 to your computer and use it in GitHub Desktop.
Save supereng/5b0cb042a3c731e6dbd8cb6812c0ca60 to your computer and use it in GitHub Desktop.
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment