(request, context)
Execute functions for the sklearn machine learning library.
        :param request: an iterable sequence of RowData
        :param context:
        :return: Refer to comments below as the response depends on the function called
Execute functions for the sklearn machine learning library.
        :param request: an iterable sequence of RowData
        :param context:
        :return: Refer to comments below as the response depends on the function called
Execute functions for the sklearn machine learning library . : param request : an iterable sequence of RowData : param context : : return : Refer to comments below as the response depends on the function called
def _sklearn(request, context):
        """
        Execute functions for the sklearn machine learning library.
        :param request: an iterable sequence of RowData
        :param context:
        :return: Refer to comments below as the response depends on the function called
        """
        # Get a list from the generator object so that it can be iterated over multiple times
        request_list = [request_rows for request_rows in request]
        
        # Get the function id from the header to determine the variant being called
        function = ExtensionService._get_function_id(context)
        
        # Create an instance of the SKLearnForQlik class
        model = SKLearnForQlik(request_list, context)
        
        # Call the function based on the mapping in functions.json
        # The if conditions are grouped based on similar output structure
        if function in (9, 10, 21, 24, 34):    
            if function == 9:
                # Set up the model and save to disk
                response = model.setup()
            elif function == 21:
                # Set up a model with specific metric and dimensionality reduction arguments and save to disk
                response = model.setup(advanced=True)
            elif function == 10:
                # Set feature definitions for an existing model
                response = model.set_features()
            elif function == 24:
                # Set a parameter grid for hyperparameter optimization
                response = model.set_param_grid()
            elif function == 34:
                # Setup the architecture for a Keras model
                response = model.keras_setup()
            
            dtypes = ["str", "str", "str"]
        
        elif function == 11:
            # Return the feature definitions for an existing model
            response = model.get_features()
            dtypes = ["str", "num", "str", "str", "str", "str", "str"]
        
        elif function == 12:
            # Train and Test an existing model, saving the sklearn pipeline for further predictions
            response = model.fit()
            dtypes = ["str", "str", "str", "str", "num"]
        
        elif function in (14, 16, 19, 20, 27, 36):
            if function == 14:
                # Provide predictions in a chart expression based on an existing model
                response = model.predict(load_script=False)
            elif function == 16:
                # Provide predictions probabilities in a chart expression based on an existing model
                response = model.predict(load_script=False, variant="predict_proba")
            elif function == 19:
                # Get a list of models based on a search string
                response = model.list_models()
            elif function == 20:
                # Get a string that can be evaluated to get the features expression for the predict function
                response = model.get_features_expression()
            elif function == 27:
                # Get labels for clustering
                response = model.fit_transform(load_script=False)
            elif function == 36:
                # Get sequence predictions from Keras
                response = model.sequence_predict(load_script=False)
            elif function == 38:
                # Get sequence prediction probabilities from Keras
                response = model.sequence_predict(load_script=False, variant="predict_proba")
            
            dtypes = ["str"]
            
        elif function in (15, 17, 28, 37):
            if function == 15:
                # Provide predictions in the load script based on an existing model
                response = model.predict(load_script=True)    
            elif function == 17:
                # Provide prediction probabilities in the load script based on an existing model
                response = model.predict(load_script=True, variant="predict_proba")
            elif function == 28:
                # Provide labels for clustering
                response = model.fit_transform(load_script=True)
            elif function == 37:
                # Get sequence predictions from Keras
                response = model.sequence_predict(load_script=True)
            elif function == 39:
                # Get sequence prediction probabilities from Keras
                response = model.sequence_predict(load_script=True, variant="predict_proba")

            dtypes = ["str", "str", "str"]
        
        elif function in (18, 22, 42):
            if function == 18:
                response = model.get_metrics()
            elif function == 22:
                response = model.calculate_metrics()
            elif function == 42:
                response = model.calculate_metrics(ordered_data=True)
            
            # Check whether the metrics are for a classifier or regressor and whether they come from cross validation or hold-out testing
            if "accuracy_std" in response.columns:
                estimator_type = "classifier_cv"
            elif "accuracy" in response.columns:
                estimator_type = "classifier"
            elif "r2_score_std" in response.columns:
                estimator_type = "regressor_cv"
            elif "r2_score" in response.columns:
                estimator_type = "regressor"
            
            # We convert values to type SSE.Dual, and group columns into a iterable
            if estimator_type == "classifier_cv":
                dtypes = ["str", "str", "num", "num", "num", "num", "num", "num", "num", "num"]
            elif estimator_type == "classifier":
                dtypes = ["str", "str", "num", "num", "num", "num", "num"]
            elif estimator_type == "regressor_cv":
                dtypes = ["str", "num", "num", "num", "num", "num", "num", "num", "num", "num", "num"]
            elif estimator_type == "regressor":
                dtypes = ["str", "num", "num", "num", "num", "num"]
        
        elif function == 23:
            # Get the confusion matrix for the classifier
            response = model.get_confusion_matrix()
            if response.shape[1] == 4:
                dtypes = ["str", "str", "str", "num"]
            else:
                dtypes = ["str", "num", "num", "num", "num", "num"]
        
        elif function == 25:
            # Get the best parameters based on a grid search cross validation
            response = model.get_best_params()
            dtypes = ["str", "str"]
        
        elif function == 26:
            # Provide results from dimensionality reduction
            response = model.fit_transform(load_script=True)
            dtypes = ["str", "str"]

            for i in range(response.shape[1]-2):
                dtypes.append("num")
        
        elif function == 29:
            # Explain the feature importances for the model
            response = model.explain_importances()
            dtypes = ["str", "str", "num"]
        
        elif function == 35:
            # Provide metrics from the training history of a Keras model
            response = model.get_keras_history()
            dtypes = ["str"]

            for i in range(1, response.shape[1]):
                dtypes.append("num")
        
        # Get the response as SSE.Rows
        response_rows = utils.get_response_rows(response.values.tolist(), dtypes) 

        # Get the number of rows in the request
        num_request_bundles = len(request_list)

        # Get the number of rows in the response
        num_rows = len(response_rows) 

        # Calculate the number of rows to send per bundle
        if num_rows >= num_request_bundles:
            rows_per_bundle = len(response_rows)//len(request_list)
        else:
            rows_per_bundle = num_rows

        # Stream response as BundledRows
        for i in range(0, len(response_rows), rows_per_bundle):
            # Yield Row data as Bundled rows
            yield SSE.BundledRows(rows=response_rows[i : i + rows_per_bundle])
def _sklearn ( request , context ) : # Get a list from the generator object so that it can be iterated over multiple times request_list = [ request_rows for request_rows in request ] # Get the function id from the header to determine the variant being called function = ExtensionService . _get_function_id ( context ) # Create an instance of the SKLearnForQlik class model = SKLearnForQlik ( request_list , context ) # Call the function based on the mapping in functions.json # The if conditions are grouped based on similar output structure if function in ( 9 , 10 , 21 , 24 , 34 ) : if function == 9 : # Set up the model and save to disk response = model . setup ( ) elif function == 21 : # Set up a model with specific metric and dimensionality reduction arguments and save to disk response = model . setup ( advanced = True ) elif function == 10 : # Set feature definitions for an existing model response = model . set_features ( ) elif function == 24 : # Set a parameter grid for hyperparameter optimization response = model . set_param_grid ( ) elif function == 34 : # Setup the architecture for a Keras model response = model . keras_setup ( ) dtypes = [ "str" , "str" , "str" ] elif function == 11 : # Return the feature definitions for an existing model response = model . get_features ( ) dtypes = [ "str" , "num" , "str" , "str" , "str" , "str" , "str" ] elif function == 12 : # Train and Test an existing model, saving the sklearn pipeline for further predictions response = model . fit ( ) dtypes = [ "str" , "str" , "str" , "str" , "num" ] elif function in ( 14 , 16 , 19 , 20 , 27 , 36 ) : if function == 14 : # Provide predictions in a chart expression based on an existing model response = model . predict ( load_script = False ) elif function == 16 : # Provide predictions probabilities in a chart expression based on an existing model response = model . predict ( load_script = False , variant = "predict_proba" ) elif function == 19 : # Get a list of models based on a search string response = model . list_models ( ) elif function == 20 : # Get a string that can be evaluated to get the features expression for the predict function response = model . get_features_expression ( ) elif function == 27 : # Get labels for clustering response = model . fit_transform ( load_script = False ) elif function == 36 : # Get sequence predictions from Keras response = model . sequence_predict ( load_script = False ) elif function == 38 : # Get sequence prediction probabilities from Keras response = model . sequence_predict ( load_script = False , variant = "predict_proba" ) dtypes = [ "str" ] elif function in ( 15 , 17 , 28 , 37 ) : if function == 15 : # Provide predictions in the load script based on an existing model response = model . predict ( load_script = True ) elif function == 17 : # Provide prediction probabilities in the load script based on an existing model response = model . predict ( load_script = True , variant = "predict_proba" ) elif function == 28 : # Provide labels for clustering response = model . fit_transform ( load_script = True ) elif function == 37 : # Get sequence predictions from Keras response = model . sequence_predict ( load_script = True ) elif function == 39 : # Get sequence prediction probabilities from Keras response = model . sequence_predict ( load_script = True , variant = "predict_proba" ) dtypes = [ "str" , "str" , "str" ] elif function in ( 18 , 22 , 42 ) : if function == 18 : response = model . get_metrics ( ) elif function == 22 : response = model . calculate_metrics ( ) elif function == 42 : response = model . calculate_metrics ( ordered_data = True ) # Check whether the metrics are for a classifier or regressor and whether they come from cross validation or hold-out testing if "accuracy_std" in response . columns : estimator_type = "classifier_cv" elif "accuracy" in response . columns : estimator_type = "classifier" elif "r2_score_std" in response . columns : estimator_type = "regressor_cv" elif "r2_score" in response . columns : estimator_type = "regressor" # We convert values to type SSE.Dual, and group columns into a iterable if estimator_type == "classifier_cv" : dtypes = [ "str" , "str" , "num" , "num" , "num" , "num" , "num" , "num" , "num" , "num" ] elif estimator_type == "classifier" : dtypes = [ "str" , "str" , "num" , "num" , "num" , "num" , "num" ] elif estimator_type == "regressor_cv" : dtypes = [ "str" , "num" , "num" , "num" , "num" , "num" , "num" , "num" , "num" , "num" , "num" ] elif estimator_type == "regressor" : dtypes = [ "str" , "num" , "num" , "num" , "num" , "num" ] elif function == 23 : # Get the confusion matrix for the classifier response = model . get_confusion_matrix ( ) if response . shape [ 1 ] == 4 : dtypes = [ "str" , "str" , "str" , "num" ] else : dtypes = [ "str" , "num" , "num" , "num" , "num" , "num" ] elif function == 25 : # Get the best parameters based on a grid search cross validation response = model . get_best_params ( ) dtypes = [ "str" , "str" ] elif function == 26 : # Provide results from dimensionality reduction response = model . fit_transform ( load_script = True ) dtypes = [ "str" , "str" ] for i in range ( response . shape [ 1 ] - 2 ) : dtypes . append ( "num" ) elif function == 29 : # Explain the feature importances for the model response = model . explain_importances ( ) dtypes = [ "str" , "str" , "num" ] elif function == 35 : # Provide metrics from the training history of a Keras model response = model . get_keras_history ( ) dtypes = [ "str" ] for i in range ( 1 , response . shape [ 1 ] ) : dtypes . append ( "num" ) # Get the response as SSE.Rows response_rows = utils . get_response_rows ( response . values . tolist ( ) , dtypes ) # Get the number of rows in the request num_request_bundles = len ( request_list ) # Get the number of rows in the response num_rows = len ( response_rows ) # Calculate the number of rows to send per bundle if num_rows >= num_request_bundles : rows_per_bundle = len ( response_rows ) // len ( request_list ) else : rows_per_bundle = num_rows # Stream response as BundledRows for i in range ( 0 , len ( response_rows ) , rows_per_bundle ) : # Yield Row data as Bundled rows yield SSE . BundledRows ( rows = response_rows [ i : i + rows_per_bundle ] )