Skip to content

Commit d8df124

Browse files
committed
feat: new article
1 parent 5e96260 commit d8df124

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ keywords:
2020
- Nonlinear Models
2121
- Data Smoothing
2222
- Statistical Modeling
23+
- python
24+
- bash
25+
- go
2326
seo_description: Splines are flexible mathematical tools used for smoothing and modeling complex data patterns. Learn what they are, how they work, and their practical applications in regression, data smoothing, and machine learning.
2427
seo_title: What Are Splines? A Deep Dive into Their Uses in Data Analysis
2528
seo_type: article
@@ -29,6 +32,9 @@ tags:
2932
- Regression
3033
- Data Smoothing
3134
- Nonlinear Models
35+
- python
36+
- bash
37+
- go
3238
title: 'Understanding Splines: What They Are and How They Are Used in Data Analysis'
3339
---
3440

@@ -167,3 +173,227 @@ Splines are a versatile and powerful tool for modeling nonlinear relationships,
167173
From **cubic splines** for smooth curve fitting to **B-splines** for handling noise, and **natural splines** to avoid overfitting, splines give you the ability to model complex data without the limitations of traditional polynomial regression. Whether you’re a statistician, data scientist, or machine learning engineer, understanding how to use splines can enhance your ability to model and interpret data with **greater precision**.
168174

169175
If you're dealing with **nonlinear patterns** in data, consider giving splines a try. With their balance of flexibility and smoothness, they just might be the tool you need to uncover the true relationship hiding in your data.
176+
177+
## Appendix: Python Code for Splines
178+
179+
Below is an example of how to use splines in Python with the `scipy` and `statsmodels` libraries. The code demonstrates fitting a spline to data, plotting the result, and using spline regression to model nonlinear relationships.
180+
181+
### Fitting a Cubic Spline with `scipy`
182+
183+
```python
184+
import numpy as np
185+
import matplotlib.pyplot as plt
186+
from scipy.interpolate import CubicSpline
187+
188+
# Generate example data
189+
x = np.linspace(0, 10, 10)
190+
y = np.sin(x) + 0.1 * np.random.randn(10) # Adding some noise
191+
192+
# Fit a cubic spline
193+
cs = CubicSpline(x, y)
194+
195+
# Generate finer points for smooth plotting
196+
x_fine = np.linspace(0, 10, 100)
197+
y_fine = cs(x_fine)
198+
199+
# Plot the original data and the fitted spline
200+
plt.scatter(x, y, label='Data', color='red')
201+
plt.plot(x_fine, y_fine, label='Cubic Spline', color='blue')
202+
plt.title('Cubic Spline Fit')
203+
plt.legend()
204+
plt.show()
205+
```
206+
207+
### B-Spline Fitting with `scipy`
208+
209+
```python
210+
from scipy.interpolate import splrep, splev
211+
212+
# Example data
213+
x = np.linspace(0, 10, 10)
214+
y = np.sin(x) + 0.1 * np.random.randn(10)
215+
216+
# Fit B-spline (degree 3)
217+
tck = splrep(x, y, k=3)
218+
219+
# Evaluate the spline at finer points
220+
x_fine = np.linspace(0, 10, 100)
221+
y_fine = splev(x_fine, tck)
222+
223+
# Plot the result
224+
plt.scatter(x, y, label='Data', color='red')
225+
plt.plot(x_fine, y_fine, label='B-Spline', color='green')
226+
plt.title('B-Spline Fit')
227+
plt.legend()
228+
plt.show()
229+
```
230+
231+
### Spline Regression with `statsmodels`
232+
233+
```python
234+
import statsmodels.api as sm
235+
from patsy import dmatrix
236+
237+
# Generate synthetic data for regression
238+
np.random.seed(123)
239+
x = np.linspace(0, 10, 100)
240+
y = np.sin(x) + np.random.normal(scale=0.3, size=100)
241+
242+
# Create a cubic spline basis for regression
243+
transformed_x = dmatrix("bs(x, df=6, degree=3, include_intercept=True)", {"x": x})
244+
245+
# Fit the spline regression model
246+
model = sm.OLS(y, transformed_x).fit()
247+
248+
# Generate predicted values
249+
y_pred = model.predict(transformed_x)
250+
251+
# Plot original data and spline regression fit
252+
plt.scatter(x, y, facecolor='none', edgecolor='b', label='Data')
253+
plt.plot(x, y_pred, color='red', label='Spline Regression Fit')
254+
plt.title('Spline Regression with statsmodels')
255+
plt.legend()
256+
plt.show()
257+
```
258+
259+
### Natural Cubic Spline with `patsy`
260+
261+
```python
262+
# Using Natural Cubic Spline in statsmodels via patsy
263+
264+
# Create a natural spline basis for regression
265+
transformed_x_ns = dmatrix("cr(x, df=4)", {"x": x}, return_type='dataframe')
266+
267+
# Fit the natural spline regression model
268+
model_ns = sm.OLS(y, transformed_x_ns).fit()
269+
270+
# Generate predicted values
271+
y_pred_ns = model_ns.predict(transformed_x_ns)
272+
273+
# Plot the data and natural spline regression fit
274+
plt.scatter(x, y, facecolor='none', edgecolor='b', label='Data')
275+
plt.plot(x, y_pred_ns, color='orange', label='Natural Cubic Spline Fit')
276+
plt.title('Natural Cubic Spline Regression')
277+
plt.legend()
278+
plt.show()
279+
```
280+
281+
## Appendix: Go Code for Splines
282+
283+
In Go, there is no built-in support for splines, but we can use third-party packages like `gonum` to implement spline interpolation and regression. Below is an example of how to use splines in Go with the `gonum` package.
284+
285+
### Installing Required Libraries
286+
287+
You need to install `gonum` for numerical computing:
288+
289+
```bash
290+
go get gonum.org/v1/gonum
291+
```
292+
293+
### Cubic Spline Interpolation with `gonum`
294+
295+
```go
296+
package main
297+
298+
import (
299+
"fmt"
300+
"gonum.org/v1/gonum/floats"
301+
"gonum.org/v1/gonum/interp"
302+
"gonum.org/v1/plot"
303+
"gonum.org/v1/plot/plotter"
304+
"gonum.org/v1/plot/vg"
305+
"math"
306+
)
307+
308+
func main() {
309+
// Example data points
310+
x := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
311+
y := make([]float64, len(x))
312+
for i, v := range x {
313+
y[i] = math.Sin(v) + 0.1*randFloat64() // Adding noise
314+
}
315+
316+
// Fit cubic spline
317+
spline := interp.Cubic{}
318+
spline.Fit(x, y)
319+
320+
// Generate smoother points
321+
xFine := linspace(0, 10, 100)
322+
yFine := make([]float64, len(xFine))
323+
for i, v := range xFine {
324+
yFine[i] = spline.Predict(v)
325+
}
326+
327+
// Plot the result
328+
plotCubicSpline(x, y, xFine, yFine)
329+
}
330+
331+
// Function to generate random noise
332+
func randFloat64() float64 {
333+
return (2*math.RandFloat64() - 1) * 0.1
334+
}
335+
336+
// linspace generates 'n' evenly spaced points between 'start' and 'end'
337+
func linspace(start, end float64, n int) []float64 {
338+
result := make([]float64, n)
339+
floats.Span(result, start, end)
340+
return result
341+
}
342+
343+
// plotCubicSpline plots the original data and the fitted cubic spline
344+
func plotCubicSpline(x, y, xFine, yFine []float64) {
345+
p, _ := plot.New()
346+
p.Title.Text = "Cubic Spline Interpolation"
347+
p.X.Label.Text = "X"
348+
p.Y.Label.Text = "Y"
349+
350+
// Plot original data
351+
dataPoints := make(plotter.XYs, len(x))
352+
for i := range x {
353+
dataPoints[i].X = x[i]
354+
dataPoints[i].Y = y[i]
355+
}
356+
scatter, _ := plotter.NewScatter(dataPoints)
357+
scatter.GlyphStyle.Shape = draw.CircleGlyph{}
358+
scatter.GlyphStyle.Radius = vg.Points(3)
359+
360+
// Plot cubic spline interpolation
361+
splineLine := make(plotter.XYs, len(xFine))
362+
for i := range xFine {
363+
splineLine[i].X = xFine[i]
364+
splineLine[i].Y = yFine[i]
365+
}
366+
line, _ := plotter.NewLine(splineLine)
367+
368+
// Add plots to plot
369+
p.Add(scatter, line)
370+
p.Save(6*vg.Inch, 6*vg.Inch, "cubic_spline.png")
371+
}
372+
```
373+
374+
### B-Spline Fitting in Go (Manual Implementation)
375+
376+
Go doesn’t have direct support for B-splines in `gonum`, so you might have to implement it manually or find a library that does. Below is a simple example that demonstrates cubic interpolation using `gonum`'s interpolation package.
377+
378+
```go
379+
package main
380+
381+
import (
382+
"fmt"
383+
"gonum.org/v1/gonum/interp"
384+
)
385+
386+
func main() {
387+
x := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
388+
y := []float64{0, 0.84, 0.91, 0.14, -0.75, -1, -0.75, 0.14, 0.91, 0.84, 0}
389+
390+
// Create a cubic spline interpolator
391+
spline := interp.Cubic{}
392+
spline.Fit(x, y)
393+
394+
// Evaluate the spline at a new point
395+
xEval := 6.5
396+
yEval := spline.Predict(xEval)
397+
fmt.Printf("Spline evaluation at x = %v: y = %v\n", xEval, yEval)
398+
}
399+
```

0 commit comments

Comments
 (0)