थोड़ा चारों ओर खोज के बाद, मैं कुछ भी है कि वास्तव में अजगर के लिए एक विकल्प के रूप में ets
होनहार लगता नहीं मिली है। हालांकि कुछ प्रयास हैं: StatsModels और pycast's Forecasting methods, जो आप जांच सकते हैं कि वे आपकी आवश्यकताओं के अनुरूप हैं या नहीं।
एक विकल्प जिसे आप लापता कार्यान्वयन के कामकाज के लिए उपयोग कर सकते हैं, subprocess मॉड्यूल का उपयोग करके पाइथन से आर स्क्रिप्ट चलाने के लिए है। इस here को कैसे करें इस पर एक बहुत अच्छा लेख है।
आदेश बाद में करने के लिए
: (उदा। my_forecast.R
)
- आप एक अनुसंधान स्क्रिप्ट बनाने के लिए की जरूरत है, जो calculate जाएगा और (
ets
का प्रयोग करके) प्रिंट पूर्वानुमान एक पर फ़ाइल, या stdout
पर (cat()
कमांड के साथ), स्क्रिप्ट रन के बाद उनका उपयोग करने के लिए।इस प्रकार, import subprocess
# You need to define the command that will run the Rscript from the subprocess
command = 'Rscript'
path2script = 'path/to/my_forecast.R'
cmd = [command, path2script]
# Option 1: If your script prints to a file
subprocess.run(cmd)
f = open('path/to/created/file', 'r')
(...Do stuff from here...)
# Option 2: If your script prints to stdout
forecasts = subprocess.check_output(cmd, universal_newlines=True)
(...Do stuff from here...)
आप भी अपने cmd
है, जो आदेश पंक्ति तर्क के रूप में अपने Rscript द्वारा उपयोग किया जाएगा करने के लिए तर्क जोड़ सकते हैं::
आप इस प्रकार एक अजगर स्क्रिप्ट से आर स्क्रिप्ट चला सकते हैं
args = [arg0, arg1, ...]
cmd = [command, path2script] + args
Then pass cmd to the subprocess
संपादित करें:
मुझे होल्ट-विंटर पूर्वानुमान पर लेखों की एक अनुकरणीय श्रृंखला मिली: part1, part2 और part3।
प्रारंभिक प्रवृत्ति:
def initial_trend(series, slen):
sum = 0.0
for i in range(slen):
sum += float(series[i+slen] - series[i])/slen
return sum/slen
# >>> initial_trend(series, 12)
# -0.7847222222222222
प्रारंभिक मौसमी घटक:
उन लेखों में विश्लेषण को समझने के लिए आसान इसके अलावा,
ग्रेगरी Trubetskoy (लेखक) कोड वह विकसित प्रदान की गई है
def initial_seasonal_components(series, slen):
seasonals = {}
season_averages = []
n_seasons = int(len(series)/slen)
# compute season averages
for j in range(n_seasons):
season_averages.append(sum(series[slen*j:slen*j+slen])/float(slen))
# compute initial values
for i in range(slen):
sum_of_vals_over_avg = 0.0
for j in range(n_seasons):
sum_of_vals_over_avg += series[slen*j+i]-season_averages[j]
seasonals[i] = sum_of_vals_over_avg/n_seasons
return seasonals
# >>> initial_seasonal_components(series, 12)
# {0: -7.4305555555555545, 1: -15.097222222222221, 2: -7.263888888888888,
# 3: -5.097222222222222, 4: 3.402777777777778, 5: 8.069444444444445,
# 6: 16.569444444444446, 7: 9.736111111111112, 8: -0.7638888888888887,
# 9: 1.902777777777778, 10: -3.263888888888889, 11: -0.7638888888888887}
अंततः एल्गोरिदम:
def triple_exponential_smoothing(series, slen, alpha, beta, gamma, n_preds):
result = []
seasonals = initial_seasonal_components(series, slen)
for i in range(len(series)+n_preds):
if i == 0: # initial values
smooth = series[0]
trend = initial_trend(series, slen)
result.append(series[0])
continue
if i >= len(series): # we are forecasting
m = i - len(series) + 1
result.append((smooth + m*trend) + seasonals[i%slen])
else:
val = series[i]
last_smooth, smooth = smooth, alpha*(val-seasonals[i%slen]) + (1-alpha)*(smooth+trend)
trend = beta * (smooth-last_smooth) + (1-beta)*trend
seasonals[i%slen] = gamma*(val-smooth) + (1-gamma)*seasonals[i%slen]
result.append(smooth+trend+seasonals[i%slen])
return result
# # forecast 24 points (i.e. two seasons)
# >>> triple_exponential_smoothing(series, 12, 0.716, 0.029, 0.993, 24)
# [30, 20.34449316666667, 28.410051892109554, 30.438122252647577, 39.466817731253066, ...
आप एक फ़ाइल, पूर्व में उन जगह कर सकते हैं: निम्नलिखित संरचना के साथ एक फ़ोल्डर के अंदर holtwinters.py
:
forecast_folder
|
└── __init__.py
|
└── holtwinters.py
यहाँ से, यह एक अजगर मॉड्यूल है जो आप हर परियोजना संरचना के अंदर जगह कर सकते हैं आप चाहते हैं कि इसे उस परियोजना के अंदर कहीं भी इस्तेमाल करें, बस इसे आयात करके।
यहां आर पैकेज द्वारा किए गए दृष्टिकोण पर कुछ संदर्भ दिए गए हैं: https://www.otexts.org/fpp/7/7 http://robjhyndman.com/talks/ABS1.pdf। अब तक मुझे एक पायथन पैकेज नहीं मिला है जो पूर्ण राज्य अंतरिक्ष ढांचे को लागू करता है। – Zach
क्या आप सिर्फ 'rpy2' पैकेज के माध्यम से पाइथन से आर टूल्स का उपयोग कर सकते हैं? –
@mfripp हां, मैं अजगर से आर को कॉल कर सकता हूं, लेकिन अगर मैं कर सकता हूं तो मैं सीधे पाइथन का उपयोग करना पसंद करूंगा! – Zach