Search code examples
pythonscikit-learnlinear-regression

Python/Scikit-learn - Linear Regression - Access to Linear Regression Equation


I've built a few different linear regressions, using the same group of predictor variables, as you can see below:

model=LinearRegression()
model.fit(X=predictor_train,y=target_train)
prediction_train=model.predict(predictor_train)
pred=model.predict(main_frame.iloc[-1:,1:])

To create the predictions of the target variable, I suppose that the Scikit algorithm created an equation with those "predictor variables". My question is: How do I access that equation?


Solution

  • You're looking for params = model.coef_. This returns an array with the weight of each model input.

    Note that this is a linear equation, so to get the prediction for yourself, you want to form an equation such that your prediction, y = sum([input[i] * params[i]]), if you had some input array called input. This is the dot product, if you're familiar with linear algebra between the parameter vector and the feature vector.