This is my code but when I run it, I am not getting the correct shape. I need it to return a numpy array of the shape (4,100).
To get an idea of what I'm doing, I am fitting a polynomial LinearRegression model on the training data for the specified degrees then generating predictions for the polynomial's values by transposing the 100 row, single column output into a single row, 100 column array.
np.random.seed(0)
C = 15
n = 60
x = np.linspace(0, 20, n) # x is drawn from a fixed range
y = x ** 3 / 20 - x ** 2 - x C * np.random.randn(n)
x = x.reshape(-1, 1) # convert x and y from simple array to a 1-column matrix for input to sklearn regression
y = y.reshape(-1, 1)
# Create the training and testing sets and their targets
X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
def model():
degs = (1, 3, 7, 11)
#Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it
#contains a single sample.
def poly_y(i):
poly = PolynomialFeatures(degree = i)
x_poly = poly.fit_transform(X_train.reshape(-1,1))
linreg = LinearRegression().fit(x_poly, y_train)
#x_orig = np.linspace(0, 20, 100)
y_pred = linreg.predict(poly.fit_transform(np.linspace(0, 20, 100).reshape(-1,1)))
y_pred = y_pred.T
return(y_pred.reshape(-1,1))
ans = poly_y(1)
for i in degs:
temp = poly_y(i)
ans = np.vstack([ans, temp])
return ans
model()
CodePudding user response:
Combining the comments to your question, with a brief explanation:
You're currently doing
ans = poly_y(1)
for i in degs:
temp = poly_y(i)
ans = np.vstack([ans, temp])
You set ans to the result for a degree of one, then loop through all degrees and stack those to ans. But, all degrees include 1, so you get degree 1 twice, and end up with a 500 by 1 array. Thus, you can remove the first line. Then, you have this loop where you repeatedly stack to ans, which can be done in one go, using a list comprehension (e.g., with [poly_y(deg) for deg in degs]). Stacking that results in a 400 by 1 array, which is not what you want. You could reshape that, or you could use hstack. The latter returns a 100 by 4 array; to get a 4 by 100 array, just transpose that.
So the final solution would be to replace the above four lines with
ans = np.hstack([poly_y(deg) for deg in degs]).T
(and if you want to get more fancy, replace those lines and the return ans line with
return np.hstack([poly_y(deg) for deg in degs]).T
)

