I have a matrix with 2 columns (matrix "X" with two features - feature0 and feature1) and variable number of rows. For each sample (row in matrix) I want to compute an expanded row such that each row would be [feature0, feature1, feature0^2, feature1^2, feature0*feature1, 1].
I have written function below which does the job.
def expand(X):
X_expanded = np.zeros((X.shape[0], 6))
for i in range(X_expanded.shape[0]):
for j in range(X_expanded.shape[1]):
if j <= 1:
X_expanded[i, j] = X[i, j]
elif j == 2:
X_expanded[i, j] = X[i, 0]*X[i, 0]
elif j == 3:
X_expanded[i, j] = X[i, 1]*X[i, 1]
elif j == 4:
X_expanded[i, j] = X[i, 0]*X[i, 1]
elif j == 5:
X_expanded[i, j] = 1
return X_expanded
The questions I have, is there a more efficient or "better way" of performing this calculation? Seems cumbersome to me so would welcome any advice. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire