I am working on a problem where I have to predict a vector y from a scalar x. I am currently using linear regression to create a baseline model. But it does not seem to handle the multi-dimesional output.
I am using the following the code:
from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(x_train, y_train)
In this case, x_train is a column vector of shape (1,m) and y_train is a vector of vectors of shape (m,).
The error message produced can be seen here.
I think it has to do something with multi-output parameter. Is there any way to work around this?
CodePudding user response:
I am not sure about the data that you are using but try reshaping your "x_train" by using x_train.reshape(-1,1) and then fitting this reshaped data to your linear regression.
CodePudding user response:
Not sure what your underlying data look like, but the error message bool object has no attribute 'any'
, suggests something wrong with the way your y
object is formatted. Here's a minimal working example:
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
N = 100
x = np.random.choice(range(3), size=N)
df = pd.DataFrame({
'x': x,
'y1': 3 * x np.random.normal(size=N),
'y2': -0.2 * x np.random.normal(scale=0.2, size=N),
'y3': -x 12 np.random.normal(scale=0.3, size=N)})
print(df.head())
X = df.pop('x').to_numpy().reshape(-1, 1)
reg = LinearRegression().fit(X, df)
preds = reg.predict(X)
print(preds[:5])
print('coefs:', reg.coef_)
print('intercepts:', reg.intercept_)