क्रॉस-वैलिडेटेड implicit ALS मॉडल चलाना
अब जब हमारे पास कई ALS मॉडल हैं, जिनमें अलग-अलग हाइपरपैरामीटर वैल्यूज़ हैं, तो हम उन्हें msd डेटासेट के ट्रेनिंग हिस्से पर cross validation से ट्रेन कर सकते हैं, फिर उन्हें टेस्ट सेट पर चलाकर पहले चर्चा की गई ROEM फंक्शन से हर मॉडल के प्रदर्शन का मूल्यांकन कर सकते हैं। दुर्भाग्य से, यह प्रक्रिया इस अभ्यास के लिए बहुत समय लेगी, इसलिए इसे अलग से चलाया गया है। लेकिन संदर्भ के लिए आप अपने model_list का मूल्यांकन निम्न लूप से कर सकते हैं (यहाँ हम msd डेटासेट का उपयोग कर रहे हैं):
# Split the data into training and test sets
(training, test) = msd.randomSplit([0.8, 0.2])
#Building 5 folds within the training set.
train1, train2, train3, train4, train5 = training.randomSplit([0.2, 0.2, 0.2, 0.2, 0.2], seed = 1)
fold1 = train2.union(train3).union(train4).union(train5)
fold2 = train3.union(train4).union(train5).union(train1)
fold3 = train4.union(train5).union(train1).union(train2)
fold4 = train5.union(train1).union(train2).union(train3)
fold5 = train1.union(train2).union(train3).union(train4)
foldlist = [(fold1, train1), (fold2, train2), (fold3, train3), (fold4, train4), (fold5, train5)]
# Empty list to fill with ROEMs from each model
ROEMS = []
# Loops through all models and all folds
for model in model_list:
for ft_pair in foldlist:
# Fits model to fold within training data
fitted_model = model.fit(ft_pair[0])
# Generates predictions using fitted_model on respective CV test data
predictions = fitted_model.transform(ft_pair[1])
# Generates and prints a ROEM metric CV test data
r = ROEM(predictions)
print ("ROEM: ", r)
# Fits model to all of training data and generates preds for test data
v_fitted_model = model.fit(training)
v_predictions = v_fitted_model.transform(test)
v_ROEM = ROEM(v_predictions)
# Adds validation ROEM to ROEM list
ROEMS.append(v_ROEM)
print ("Validation ROEM: ", v_ROEM)
कदम-दर-कदम समझाने के लिए, 192 मॉडलों के टेस्ट प्रेडिक्शंस पहले से जेनरेट कर दिए गए हैं और उनका ROEM निकाल लिया गया है। ये वैल्यूज़ दी गई ROEMS लिस्ट में उपलब्ध हैं। क्योंकि लिस्ट Pyspark के लिए विशिष्ट नहीं है और numpy लिस्ट्स के साथ बहुत अच्छा काम करता है, इसलिए हम यहाँ numpy का उपयोग करेंगे। नीचे दिए गए निर्देशों का पालन करें ताकि आप सबसे अच्छा ROEM और वह मॉडल ढूँढ सकें जिसने इसे प्रदान किया।
यह अभ्यास पाठ्यक्रम का हिस्सा है
PySpark के साथ Recommendation Engines बनाना
अभ्यास निर्देश
numpyइम्पोर्ट करें.- दी गई
ROEMSलिस्ट से सबसे छोटा ROEMnumpy.argmin()से निकालें।.argmin()मेथड दी गई लिस्ट में सबसे कम वैल्यू का index लौटाएगा। परिणाम कोiकहें औरiप्रिंट करें. - लिस्ट slicing का उपयोग करके
ROEMSलिस्ट में indexiपर मौजूद वैल्यू निकालें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import numpy
import numpy
# Find the index of the smallest ROEM
i = numpy.____(____)
print("Index of smallest ROEM:", ____)
# Find ith element of ROEMS
print("Smallest ROEM: ", ____[____])