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

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

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

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

تحذير

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

اضبط المكون

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

ملاحظة

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

ملاحظة

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

توضح المقالة كيفية استخدام Create Python Model مع مسار معالجة بسيط. فيما يلي رسم تخطيطي لمسار المعالجة:

رسم تخطيطي لـ Create Python Model

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

ملاحظة

يرجى الانتباه أكثر إلى التعليقات في نموذج التعليمات البرمجية للبرنامج النصي والتأكد من أن البرنامج النصي الخاص بك يتبع بدقة المتطلبات، بما في ذلك اسم الفئة والأساليب بالإضافة إلى توقيع الأسلوب. الانتهاك سيؤدي إلى استثناءات. Create Python Model يدعم فقط إنشاء نموذج يستند إلى 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 الذي قمت بإنشائه للتو لـ Train Modelو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.