-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.py
82 lines (68 loc) · 2.59 KB
/
polynomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module Name: Polynomial Interpolation
Description: A brief demo of inspecting, handling, imputating Curved Missing Data, i.e. 'NaN', with Polynomial Interpolation.
Credit / Prepared by:
Sun CHUNG, SMIEEE
M.Sc., HKU
License: MIT License
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Sample time series data with missing values
dates = pd.date_range(start='2020-01-01', end='2020-01-20')
sales = [200, np.nan, 210, np.nan, np.nan, 250, 260, np.nan, 280, 290,
285, np.nan, 280, 270, np.nan, np.nan, 320, 325, np.nan, 330]
data = pd.DataFrame({'date': dates, 'sales': sales})
print("Original Data:")
print(data)
# Check for missing values
missing_data = data['sales'].isnull().sum()
total_data = len(data)
missing_percentage = (missing_data / total_data) * 100
print("\nMissing Values in Each Column:")
print(data.isnull().sum())
print(f"Percentage of Missing Data: {missing_percentage:.2f}%")
data['date_formatted'] = data['date'].dt.strftime('%d %b')
# Plot the original data to observe its shape
plt.figure(figsize=(10, 5))
plt.plot(data['date_formatted'], data['sales'], label='Original Sales', marker='o')
plt.title('Original Sales Data with Missing Values')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
# Polynomial Interpolation (Order 2)
data['sales_poly'] = data['sales'].interpolate(method='polynomial', order=2)
print("\nData After Polynomial Interpolation (Order 2):")
print(data[['date', 'sales', 'sales_poly']])
# Plot the original and interpolated data
plt.figure(figsize=(10, 5))
plt.plot(data['date'], data['sales'], label='Original Sales', marker='o')
plt.plot(data['date'], data['sales_poly'], label='Polynomial Interpolation', marker='x')
plt.title('Sales Data with Polynomial Interpolation')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.show()
# Spline Interpolation (Order 3)
data['sales_spline'] = data['sales'].interpolate(method='spline', order=3)
print("\nData After Spline Interpolation (Order 3):")
print(data[['date', 'sales', 'sales_spline']])
# Plot the original and interpolated data
plt.figure(figsize=(10, 5))
plt.plot(data['date'], data['sales'], label='Original Sales', marker='o')
plt.plot(data['date'], data['sales_spline'], label='Spline Interpolation', marker='x')
plt.title('Sales Data with Spline Interpolation')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.xticks(rotation=45)
plt.show()