Features और Targets बनाइए
सबसे अच्छा पोर्टफोलियो चुनने के लिए जब हम मशीन लर्निंग का उपयोग करते हैं, तो हमें features और targets जनरेट करने होते हैं. हमारे features पिछले अभ्यास में ही बने थे — prices के exponentially weighted moving averages. हमारे targets वे बेहतरीन पोर्टफोलियो होंगे जो हमें सबसे उच्च Sharpe ratio से मिले थे.
हम pandas की .iterrows() मेथड का उपयोग करके ewma_monthly DataFrame के index, value पेयर्स लेंगे. लूप में ewma_monthly के current value को हम अपने features मानेंगे. फिर हम best Sharpe ratio के index (max_sharpe_idxs से) का उपयोग करके हर महीने के लिए सबसे अच्छे portfolio_weights निकालेंगे और उसे target सेट करेंगे.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Finance के लिए Machine Learning
अभ्यास निर्देश
- लूप में
ewma_monthlyके साथ.iterrows()मेथड का उपयोग करकेindex, valueपर iterate करें. - लूप में मौजूद
dateऔरbest_idxका उपयोग करकेportfolio_weightsको इंडेक्स करें, ताकि best Sharpe ratio के आधार पर ideal portfolio weights मिलें. - Features में
ewmaको append करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
targets, features = [], []
# Create features from price history and targets as ideal portfolio
for date, ewma in ewma_monthly.____:
# Get the index of the best sharpe ratio
best_idx = max_sharpe_idxs[date]
targets.append(portfolio_weights[____][____])
features.append(____) # add ewma to features
targets = np.array(targets)
features = np.array(features)
print(targets[-5:])