问题
So, I can get sklearn.linear_model.LinearRegression to process my data - at least to run the script without raising any exceptions or warnings. The only issue is, that I am not trying to plot the results with matplotlib, but instead I want to see the estimators and diagnostic statistics for the model.
How can I get a model summary such as the slope and intercept (B0,B1), R squared adjusted, etc to display in the console or populate into a variable instead of plotting this?
This is a generic copy of the script I ran:
import numpy as p
import pandas as pn
from sklearn import datasets, linear_model
z = pn.DataFrame(
{'a' : [1,2,3,4,5,6,7,8,9],
'b' : [9,8,7,6,5,4,3,2,1]
})
a2 = z['a'].values.reshape(9,1)
b2 = z['b'].values.reshape(9,1)
reg = linear_model.LinearRegression(fit_intercept=True)
reg.fit(a2,b2)
# print(reg.get_params(deep=True)) I tried this and it didn't print out the #information I wanted
# print(reg) # I tried this too
This ran without errors, but no output other than this appeared in the console:
{'n_jobs': 1, 'fit_intercept': True, 'copy_X': True, 'normalize': False} LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
Thanks for any info on how to get this to print a summary of the model.
回答1:
sklearn
's API is designed around fitting training data and then generating predictions on test data without exposing much if any information about how the model is fit. While you can sometimes find the estimated parameters of the model by accessing the fitted model object's coef_
attribute, you won't find much in the way of parameter description functionality. This is because there may be no way to provide this information in a uniform way. The API is designed to let you treat a linear regression the same a random forest.
Since you are interested in a linear model, you can get the information you're looking for, including confidence intervals, goodness-of-fit statistics, and the like from the statsmodels library. See their OLS example: http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html for details.
来源:https://stackoverflow.com/questions/43575652/scikit-learn-sklearn-linear-model-linearregression-view-the-results-of-the-mode