مشاركة عبر


إنشاء مكون نموذج Python

توضح هذه المقالة مكونا في مصمم التعلم الآلي Azure.

تعرف على كيفية استخدام مكون Create Python Model لإنشاء نموذج غير مدرب من برنامج Python النصي. يمكنك إسناد النموذج إلى أي متعلم مضمن في حزمة Python في بيئة مصمم التعلم الآلي من Azure.

بعد إنشاء النموذج، يمكنك استخدام Train Model لتدريب النموذج على مجموعة بيانات، مثل أي متعلم آخر في التعلم الآلي من Azure. يمكن تمرير النموذج المدرب إلى Score Model لإجراء تنبؤات. يمكنك بعد ذلك حفظ النموذج المدرب ونشر سير عمل تسجيل النقاط كخدمة ويب.

تحذير

حاليا، لا يمكن توصيل هذا المكون بمكون Tune Model Hyperparameters أو تمرير النتائج المسجلة لنموذج Python إلى Evaluate Model. إذا كنت بحاجة إلى ضبط المعلمات الفائقة أو تقييم نموذج، يمكنك كتابة برنامج نصي Python مخصص باستخدام مكون Execute Python Script .

تكوين المكون

يتطلب استخدام هذا المكون معرفة متوسطة أو خبيرة ب Python. يدعم المكون استخدام أي متعلم مضمن في حزم Python المثبتة بالفعل في التعلم الآلي من Azure. راجع قائمة حزمة Python المثبتة مسبقا في Execute Python Script.

ملاحظة

يرجى توخي الحذر عند كتابة البرنامج النصي الخاص بك والتأكد من عدم وجود خطأ في بناء الجملة، مثل استخدام كائن غير معلن أو مكون غير مستورد.

ملاحظة

انتبه أيضا إلى قائمة المكونات المثبتة مسبقا في Execute Python Script. استيراد المكونات المثبتة مسبقا فقط. يرجى عدم تثبيت حزم إضافية مثل "pip install xgboost" في هذا البرنامج النصي، وإلا سيتم رفع الأخطاء عند قراءة النماذج في مكونات الدفق لأسفل.

توضح هذه المقالة كيفية استخدام إنشاء نموذج Python باستخدام مسار بسيط. فيما يلي رسم تخطيطي للبنية الأساسية لبرنامج ربط العمليات التجارية:

رسم تخطيطي لإنشاء نموذج Python

  1. حدد Create Python Model، وقم بتحرير البرنامج النصي لتنفيذ عملية النمذجة أو إدارة البيانات. يمكنك إسناد النموذج إلى أي متعلم مضمن في حزمة Python في بيئة التعلم الآلي من Azure.

ملاحظة

يرجى إيلاء اهتمام إضافي للتعليقات في نموذج التعليمات البرمجية للبرنامج النصي والتأكد من أن البرنامج النصي الخاص بك يتبع المتطلبات بدقة، بما في ذلك اسم الفئة والأساليب بالإضافة إلى توقيع الأسلوب. سيؤدي الانتهاك إلى استثناءات. يدعم إنشاء نموذج Python فقط إنشاء نموذج يستند إلى sklearn ليتم تدريبه باستخدام Train Model.

يستخدم نموذج التعليمات البرمجية التالي لمصنف Naive Bayes من فئتين حزمة sklearn الشائعة:


# The script MUST define a class named Azure Machine LearningModel.
# This class MUST at least define the following three methods:
    # __init__: in which self.model must be assigned,
    # train: which trains self.model, the two input arguments must be pandas DataFrame,
    # predict: which generates prediction result, the input argument and the prediction result MUST be pandas DataFrame.
# The signatures (method names and argument names) of all these methods MUST be exactly the same as the following example.

# Please do not install extra packages such as "pip install xgboost" in this script,
# otherwise errors will be raised when reading models in down-stream components.

import pandas as pd
from sklearn.naive_bayes import GaussianNB


class AzureMLModel:
    def __init__(self):
        self.model = GaussianNB()
        self.feature_column_names = list()

    def train(self, df_train, df_label):
        # self.feature_column_names records the column names used for training.
        # It is recommended to set this attribute before training so that the
        # feature columns used in predict and train methods have the same names.
        self.feature_column_names = df_train.columns.tolist()
        self.model.fit(df_train, df_label)

    def predict(self, df):
        # The feature columns used for prediction MUST have the same names as the ones for training.
        # The name of score column ("Scored Labels" in this case) MUST be different from any other columns in input data.
        return pd.DataFrame(
            {'Scored Labels': self.model.predict(df[self.feature_column_names]), 
             'probabilities': self.model.predict_proba(df[self.feature_column_names])[:, 1]}
        )


  1. قم بتوصيل مكون Create Python Model الذي أنشأته للتو لتدريب النموذجوS score Model.

  2. إذا كنت بحاجة إلى تقييم النموذج، أضف مكون Execute Python Script وقم بتحرير البرنامج النصي Python.

    البرنامج النصي التالي هو نموذج التعليمات البرمجية للتقييم:

    
    
    # The script MUST contain a function named azureml_main
    # which is the entry point for this component.
    
    # imports up here can be used to 
    import pandas as pd
    
    # The entry point function MUST have two input arguments:
    #   Param<dataframe1>: a pandas.DataFrame
    #   Param<dataframe2>: a pandas.DataFrame
    def azureml_main(dataframe1 = None, dataframe2 = None):
    
        from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score, roc_curve
        import pandas as pd
        import numpy as np
    
        scores = dataframe1.ix[:, ("income", "Scored Labels", "probabilities")]
        ytrue = np.array([0 if val == '<=50K' else 1 for val in scores["income"]])
        ypred = np.array([0 if val == '<=50K' else 1 for val in scores["Scored Labels"]])    
        probabilities = scores["probabilities"]
    
        accuracy, precision, recall, auc = \
        accuracy_score(ytrue, ypred),\
        precision_score(ytrue, ypred),\
        recall_score(ytrue, ypred),\
        roc_auc_score(ytrue, probabilities)
    
        metrics = pd.DataFrame();
        metrics["Metric"] = ["Accuracy", "Precision", "Recall", "AUC"];
        metrics["Value"] = [accuracy, precision, recall, auc]
    
        return metrics,
    
    

الخطوات التالية

راجع مجموعة المكونات المتوفرة للتعلم الآلي من Azure.