p-values from ridge regression in python

2024/10/1 5:39:31

I'm using ridge regression (ridgeCV). And I've imported it from: from sklearn.linear_model import LinearRegression, RidgeCV, LarsCV, Ridge, Lasso, LassoCV

How do I extract the p-values? I checked but ridge has no object called summary.

I couldn't find any page which discusses this for python (found one for R).

alphas = np.linspace(.00001, 2, 1)
rr_scaled = RidgeCV(alphas = alphas, cv =5, normalize = True)
rr_scaled.fit(X_train, Y_train)
Answer

You can use the regressors package to output p values using:

from regressors import stats    
stats.coef_pval(rr_scaled, X_train, Y_train)

You can also print out a regression summary (containing std errors, t values, p values, R^2) using:

stats.summary(rr_scaled, X_train, Y_train)

Example:

df = pd.DataFrame({'y':np.random.randn(10), 'x1':np.random.randn(10), 'x2':np.random.randn(10)})
#           y        x1        x2
# 0 -0.228546  0.133703  0.624039
# 1 -1.005794  1.064283  1.527229
# 2 -2.180160 -1.485611 -0.471199
# 3 -0.683695 -0.213433 -0.692055
# 4 -0.451981 -0.133173  0.995683
# 5 -0.166878 -0.384913  0.255065
# 6  0.816602 -0.380910  0.381321
# 7 -0.408240  1.116328  1.163418
# 8 -0.899570 -1.055483 -0.470597
# 9  0.926600 -1.497506 -0.523385
X_train = df[['x1','x2']]
Y_train = df.yalphas = np.linspace(.00001, 2, 1)
rr_scaled = RidgeCV(alphas = alphas, cv =5, normalize = True)
rr_scaled.fit(X_train, Y_train)

Calling stats.coef_pval:

stats.coef_pval(rr_scaled, X_train, Y_train)
# array([0.17324576, 0.77225007, 0.74614808])

Now, calling stats.summary:

stats.summary(rr_scaled, X_train, Y_train)
# Residuals:
# Min      1Q  Median      3Q     Max
# -1.3347 -0.2368  0.0038  0.3636  1.7804# Coefficients:
#             Estimate  Std. Error  t value   p value
# _intercept -0.522607    0.353333  -1.4791  0.173246
# x1         -0.143694    0.481720  -0.2983  0.772250
# x2          0.192431    0.576419   0.3338  0.746148
# ---
# R-squared:  0.00822,    Adjusted R-squared:  -0.27515
# F-statistic: 0.03 on 2 features
https://en.xdnf.cn/q/70991.html

Related Q&A

AutoTokenizer.from_pretrained fails to load locally saved pretrained tokenizer (PyTorch)

I am new to PyTorch and recently, I have been trying to work with Transformers. I am using pretrained tokenizers provided by HuggingFace.I am successful in downloading and running them. But if I try to…

How to scroll down in an instagram pop-up frame with Selenium

I have a python script using selenium to go to a given Instagram profile and iterate over the users followers. On the instagram website when one clicks to see the list of followers, a pop-up opens with…

Get starred messages from GMail using IMAP4 and python

I found many dummy info about working with IMAP, but I didnt understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, but how should I work with …

python and php bcrypt

I was using Laravel to register the users. It uses bcrypt like so:$2y$10$kb9T4WXdz5aKLSZX1OkpMOx.3ogUn9QX8GRZ93rd99i7VLKmeoXXXI am currently making another script that will authenticate users from anot…

Python socket library thinks socket is open when its not

Im working with a bit of Python that looks like this:HOST = 127.0.0.1 PORT = 43434 single = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:single.bind((HOST, PORT)) except socket.error as e:# Pr…

object of type _csv.reader has no len(), csv data not recognized

The following is a self-contained example. Change the "folder_name" to run it. This answers : reader type = _csv.reader list(reader) = [] _csv.reader has no len()I have tried many things but …

Lookup country for GPS coordinates without Internet access

I need to find out in what country given GPS coordinates are, on a device that has no Internet access (e.g. this, but without the easy on-line solution). Having no experience with GIS, I guess Id need …

how to get spyders python recognize external packages on MacOS X?

I have spyderlib installed on my MacOS X (10.6.8) using the official dmg file. In parallel, I have installed packages using both pip and homebrew from the terminal (i.e. opencv, gdal...). As Spyder is …

textcat - architecture extra fields not permitted

Ive been trying to practise what Ive learned from this tutorial:(https://realpython.com/sentiment-analysis-python/) using PyCharm. And this line: textcat.add_label("pos")generated a warning: …

cv2.rectangle() calls overloaded method, although I give other parameter

cv2.rectangle has two ways of calling:img = cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] ) img = cv.rectangle( img, rec, color[, thickness[, lineType[, shift]]]source:h…