-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
62 lines (48 loc) · 2.02 KB
/
plot.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
"""
[email protected] 2024-04-08
"""
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
# Read the data from the file
data = pd.read_csv('dates-chron.txt', sep=' ', header=None, names=['Date', 'CumulativeCount'], parse_dates=['Date'])
# Define the reference date
reference_date = pd.Timestamp('2000-01-01')
# Renormalize dates to be the number of days since 2000-01-01
data['Days_Since_2000'] = (data['Date'] - reference_date).dt.days + 1
# Use only the second half of the data for fitting
halfway_point = len(data) // 2
fit_data = data.iloc[halfway_point:]
# Prepare data for linear regression in the log-log space
X = np.log(fit_data['Days_Since_2000'].values.reshape(-1, 1))
y = np.log(fit_data['CumulativeCount'].values)
# Perform linear regression
model = LinearRegression()
model.fit(X, y)
# Get the exponent (slope) and the constant (intercept) of the power-law fit
exponent = model.coef_[0]
constant = np.exp(model.intercept_)
# Create an array of ordinal dates for prediction
ordinal_dates = data['Days_Since_2000'].values.reshape(-1, 1)
# Predict using the model to get the fit line in the transformed space
log_y_fit = model.predict(np.log(ordinal_dates))
# Inverse the log to get the actual values
y_fit = np.exp(log_y_fit)
# Plot the actual data
plt.figure(figsize=(10, 6))
plt.plot(data['Date'], data['CumulativeCount'], '.-', label='Cumulative Count')
plt.scatter(data.iloc[3]['Date'], data.iloc[2]['CumulativeCount'], color='red', marker='x', s=100, label='Vaswani et al. (2017)')
# Plot the fit line with actual dates and counts
nu=round(exponent,2)
plt.plot(data['Date'], y_fit, color='orange', label=f'Power-law Fit ($y \sim t^{{{nu}}}$)')
# Set the labels and title
plt.xlabel('Year')
plt.ylabel('Cumulative Count')
plt.title('...is all you need arXiv papers (cumulative) power law')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Output the parameters of the power-law fit
print(f"Power-law fit parameters: a={constant:.2f}, b={exponent:.2f}")