Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

pandas - How to do Bspline in python

I'm trying to plot a Bspline with my regression data, but it is not working. The graph shows the scatter plot but without the interpolated spline:

#library##
import numpy as np
from numpy.polynomial.polynomial import polyfit
from sklearn.preprocessing import PolynomialFeatures 
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_squared_error
import scipy.interpolate as interpolate
from scipy.interpolate import BSpline
##select x,y ##
select2 = df.groupby('Customer_Age')['Customer_Gender'].value_counts().rename('count').reset_index()
x = pd.DataFrame(select2['Customer_Age']).values.reshape(-1, 1)
y = select2['count'].values.reshape(-1, 1)
## polinomal ##
pre_process = PolynomialFeatures(degree=2)
x_poly = pre_process.fit_transform(x)
##predict#
pr_model = LinearRegression()
pr_model.fit(x_poly, y)
y_pred = pr_model.predict(x_poly)
##B-spline##
t, c, k = interpolate.splrep(x, y, s=0, k=4)
N = 100
xmin, xmax = x.min(), x.max()
xx = np.linspace(xmin, xmax, N)
spline = interpolate.BSpline(t, c, k, extrapolate=False)
##plot##
plt.plot(x, y, 'bo', label='Original points')
plt.plot(xx, spline(xx), 'r', label='BSpline')
plt.grid()
plt.legend(loc='best')
plt.show()

My approach results in these outputs for t, c, k:

    t: [17.  17.  17.  17.  17.  18.  18.5 19.  19.5 20.  20.5 21.  21.5 22.
     22.5 23.  23.5 24.  24.5 25.  25.5 26.  26.5 27.  27.5 28.  28.5 29.
     29.5 30.  30.5 31.  31.5 32.  32.5 33.  33.5 34.  34.5 35.  35.5 36.
     36.5 37.  37.5 38.  38.5 39.  39.5 40.  40.5 41.  41.5 42.  42.5 43.
     43.5 44.  44.5 45.  45.5 46.  46.5 47.  47.5 48.  48.5 49.  49.5 50.
     50.5 51.  51.5 52.  52.5 53.  53.5 54.  54.5 55.  55.5 56.  56.5 57.
     57.5 58.  58.5 59.  59.5 60.  60.5 61.  61.5 62.  62.5 63.  63.5 64.
     64.5 65.  65.5 66.  66.5 67.  67.5 68.  68.5 69.  69.5 70.  70.5 71.
     71.5 72.  72.5 73.  73.5 74.  74.5 75.  75.5 76.  76.5 77.5 78.  78.5
     79.5 80.5 81.  81.5 83.  84.  84.5 85.5 87.  87.  87.  87.  87. ]
    c: [nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan nan
     nan nan nan nan nan nan nan  6.  0.  0.  0.  0.  0.]
    k: 4

enter image description here

This is the structure of my data for x and y:

Customer_Age    Customer_Gender count
0   17  M   846
1   17  F   460
2   18  M   970
3   18  F   790
4   19  M   1188
question from:https://stackoverflow.com/questions/65857720/how-to-do-bspline-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...