(x, deg)
return Z
Compute monomial features of `x' up to degree `deg'.
Compute monomial features of `x' up to degree `deg'.
Compute monomial features of x up to degree deg .
def monomials(x, deg):
    """Compute monomial features of `x' up to degree `deg'."""
    x = np.atleast_2d(np.copy(x))
    # 1-D features (x, y)
    Z = x
    if deg >= 2:
        # 2-D features (x^2, x * y, y^2)
        temp = np.empty([len(x), 3])
        temp[:, 0] = x[:, 0] ** 2
        temp[:, 1] = x[:, 0] * x[:, 1]
        temp[:, 2] = x[:, 1] ** 2
        Z = np.hstack((Z, temp))
    if deg >= 3:
        # 3-D features (x^3, x^2 * y, x * y^2, y^3)
        temp = np.empty([len(x), 4])
        temp[:, 0] = x[:, 0] ** 3
        temp[:, 1] = (x[:, 0] ** 2) * x[:, 1]
        temp[:, 2] = x[:, 0] * (x[:, 1] ** 2)
        temp[:, 3] = x[:, 1] ** 3
        Z = np.hstack((Z, temp))
    if deg >= 4:
        # 4-D features (x^4, x^3 * y, x^2 * y^2, x * y^3, y^4)
        temp = np.empty([len(x), 5])
        temp[:, 0] = x[:, 0] ** 4
        temp[:, 1] = (x[:, 0] ** 3) * x[:, 1]
        temp[:, 2] = (x[:, 0] ** 2) * (x[:, 1] ** 2)
        temp[:, 3] = x[:, 0] * (x[:, 1] ** 3)
        temp[:, 4] = x[:, 1] ** 4
        Z = np.hstack((Z, temp))
    return Z
def monomials ( x , deg ) : x = np . atleast_2d ( np . copy ( x ) ) # 1-D features (x, y) Z = x if deg >= 2 : # 2-D features (x^2, x * y, y^2) temp = np . empty ( [ len ( x ) , 3 ] ) temp [ : , 0 ] = x [ : , 0 ] ** 2 temp [ : , 1 ] = x [ : , 0 ] * x [ : , 1 ] temp [ : , 2 ] = x [ : , 1 ] ** 2 Z = np . hstack ( ( Z , temp ) ) if deg >= 3 : # 3-D features (x^3, x^2 * y, x * y^2, y^3) temp = np . empty ( [ len ( x ) , 4 ] ) temp [ : , 0 ] = x [ : , 0 ] ** 3 temp [ : , 1 ] = ( x [ : , 0 ] ** 2 ) * x [ : , 1 ] temp [ : , 2 ] = x [ : , 0 ] * ( x [ : , 1 ] ** 2 ) temp [ : , 3 ] = x [ : , 1 ] ** 3 Z = np . hstack ( ( Z , temp ) ) if deg >= 4 : # 4-D features (x^4, x^3 * y, x^2 * y^2, x * y^3, y^4) temp = np . empty ( [ len ( x ) , 5 ] ) temp [ : , 0 ] = x [ : , 0 ] ** 4 temp [ : , 1 ] = ( x [ : , 0 ] ** 3 ) * x [ : , 1 ] temp [ : , 2 ] = ( x [ : , 0 ] ** 2 ) * ( x [ : , 1 ] ** 2 ) temp [ : , 3 ] = x [ : , 0 ] * ( x [ : , 1 ] ** 3 ) temp [ : , 4 ] = x [ : , 1 ] ** 4 Z = np . hstack ( ( Z , temp ) ) return Z