Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Is it possible to extract the remaining basis function from the algorithm for further calculation? #209

Open
kwchau opened this issue Dec 23, 2020 · 1 comment

Comments

@kwchau
Copy link

kwchau commented Dec 23, 2020

As stated in the title, is there a way to get the remaining basis after fitting as a list for further manipulation in Python? The basis_ attribute is an internal class that cannot be used while summary returns a string that is hard to extract information from. It would be great to have a list with the variables, left or right sided, and the cutoff points for each basis function in a list for further computations.

@kevin-dietz
Copy link

@kwchau It is possible but you have to do some manipulation of basis_. Something like the below code may get at what you are looking for. Wrote the snippet from memory so it may not get you all the way there but should get you pretty close.

varnames = []
pruned = []
coefs = []
i = 0

for bf in your_model.basis_:
    varnames.append(str(bf))
    pruned.append(bf.is_pruned())

    if bf.is_pruned() is False:
        coefs.append(your_model.coef_[0, i])
        i = i+1

    #Zero fill pruned coefs just to put something
    if bf.is_pruned() is not False:
        coefs.append(0)

#Construct Dataframe
summ = pd.DataFrame({'Variable' : varnames, 'Pruned': pruned, 'Coefficient': coefs})

#Remove Pruned Variables
summ = summ.loc[summ['Pruned'] == False]  

#Clean up variable name by removing h() 
summ['Variable'].replace("h" + r"\(",'', regex = True, inplace = True).replace(r"\)", '', regex = True, inplace = True)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants