Пример #1
0
    def explain(self):
        answers = {}
        for pred_col in self.evaluations:
            answers[pred_col] = []

            prediction_row = {col: self.data[col][self.row_index] for col in list(self.data.keys())}

            evaluation = self.evaluations[pred_col][self.row_index]
            clusters = evaluation.explain(self.transaction_output.transaction.lmd['column_stats'][pred_col])

            for cluster in clusters:
                pct_confidence = round(cluster['confidence'] * 100)
                predicted_value = cluster['value']

                if self.col_stats[pred_col]['data_type'] in (DATA_TYPES.NUMERIC, DATA_TYPES.DATE):
                    value_range = [cluster['buckets'][0],cluster['buckets'][-1]]

                    range_pretty_start = value_range[0]
                    if range_pretty_start > 1000:
                        range_pretty_start = round(range_pretty_start)
                    if range_pretty_start > 100:
                        range_pretty_start = round(range_pretty_start,2)
                    if range_pretty_start > 10:
                        range_pretty_start = round(range_pretty_start,1)
                    elif range_pretty_start > pow(10,-3):
                        range_pretty_start = round(range_pretty_start,6)

                    range_end_start = value_range[-1]
                    if range_end_start > 1000:
                        range_end_start = round(range_end_start)
                    if range_end_start > 100:
                        range_end_start = round(range_end_start,2)
                    if range_end_start > 10:
                        range_end_start = round(range_end_start,1)
                    elif range_end_start > pow(10,-3):
                        range_end_start = round(range_end_start,6)

                    explaination = explain_prediction(self.transaction_output.transaction.lmd, prediction_row, cluster['confidence'], pred_col)

                    answers[pred_col].append({
                        'value': predicted_value,
                        'range': value_range,
                        'confidence': cluster['confidence'],
                        'explaination': explaination,
                        'simple': f'We are {pct_confidence}% confident the value of "{pred_col}" lies between {range_pretty_start} and {range_end_start}'
                    })
                else:
                    explaination = explain_prediction(self.transaction_output.transaction.lmd, prediction_row, cluster['confidence'], pred_col)
                    answers[pred_col].append({
                        'value': predicted_value,
                        'confidence': cluster['middle_confidence'],
                        'explaination': explaination,
                        'simple': f'We are {pct_confidence}% confident the value of "{pred_col}" is {predicted_value}'
                    })

            answers[pred_col] = sorted(answers[pred_col], key=lambda x: x['confidence'], reverse=True)

        return answers
Пример #2
0
 def explain(self):
     prediction_row = {
         col: self.transaction_output.data[col][self.row_index]
         for col in list(self.transaction_output.data.keys())
     }
     #self.transaction_output.data.iloc[self.row_index]
     return explain_prediction(self.transaction_output.transaction.lmd,
                               prediction_row)
Пример #3
0
    def explain(self):
        answers = {}
        for pred_col in self.predict_columns:
            answers[pred_col] = []

            prediction_row = {
                col: self.data[col][self.row_index]
                for col in list(self.data.keys())
            }

            clusters = [{
                'value': prediction_row[pred_col],
                'confidence': prediction_row[f'{pred_col}_confidence']
            }]

            for cluster in clusters:
                pct_confidence = round(cluster['confidence'] * 100)
                predicted_value = cluster['value']

                if f'{pred_col}_model_confidence' in prediction_row:
                    new_conf = round(
                        (prediction_row[f'{pred_col}_model_confidence'] * 3 +
                         cluster['confidence'] * 1) / 4, 4)
                else:
                    new_conf = cluster['confidence']

                explanation = explain_prediction(
                    self.transaction_output.transaction.lmd, prediction_row,
                    cluster['confidence'], pred_col)
                answers[pred_col].append({
                    'value':
                    predicted_value,
                    'confidence':
                    new_conf,
                    'explanation':
                    explanation,
                    'explaination':
                    explanation,
                    'simple':
                    f'We are {pct_confidence}% confident the value of "{pred_col}" is {predicted_value}'
                })

                if self.transaction_output.input_confidence is not None:
                    for i in range(len(answers[pred_col])):
                        answers[pred_col][i]['confidence_influence_scores'] = {
                            'confidence_variation_score': [],
                            'column_names': []
                        }
                        for c in self.transaction_output.input_confidence:
                            answers[pred_col][i][
                                'confidence_influence_scores'][
                                    'confidence_variation_score'].append(
                                        self.transaction_output.
                                        input_confidence[c])
                            answers[pred_col][i][
                                'confidence_influence_scores'][
                                    'column_names'].append(str(c))

                model_result = {'value': prediction_row[f'model_{pred_col}']}

                if f'{pred_col}_model_confidence' in prediction_row:
                    model_result['confidence'] = prediction_row[
                        f'{pred_col}_model_confidence']

                answers[pred_col][-1]['model_result'] = model_result

            answers[pred_col] = sorted(answers[pred_col],
                                       key=lambda x: x['confidence'],
                                       reverse=True)

        return answers
    def explain(self):
        answers = {}
        for pred_col in self.evaluations:
            answers[pred_col] = []

            prediction_row = {
                col: self.data[col][self.row_index]
                for col in list(self.data.keys())
            }

            evaluation = self.evaluations[pred_col][self.row_index]
            clusters = evaluation.explain(
                self.transaction_output.transaction.lmd['column_stats']
                [pred_col])

            for cluster in clusters:
                pct_confidence = round(cluster['confidence'] * 100)
                predicted_value = cluster['value']

                if f'{pred_col}_model_confidence' in prediction_row:
                    new_conf = round(
                        (prediction_row[f'{pred_col}_model_confidence'] * 3 +
                         cluster['confidence'] * 1) / 4, 4)
                else:
                    new_conf = cluster['confidence']

                if self.col_stats[pred_col]['data_type'] in (
                        DATA_TYPES.NUMERIC, DATA_TYPES.DATE):
                    value_range = [
                        cluster['buckets'][0], cluster['buckets'][-1]
                    ]

                    range_pretty_start = value_range[0]
                    if range_pretty_start > 1000:
                        range_pretty_start = round(range_pretty_start)
                    if range_pretty_start > 100:
                        range_pretty_start = round(range_pretty_start, 2)
                    if range_pretty_start > 10:
                        range_pretty_start = round(range_pretty_start, 1)
                    elif range_pretty_start > pow(10, -3):
                        range_pretty_start = round(range_pretty_start, 6)

                    range_end_start = value_range[-1]
                    if range_end_start > 1000:
                        range_end_start = round(range_end_start)
                    if range_end_start > 100:
                        range_end_start = round(range_end_start, 2)
                    if range_end_start > 10:
                        range_end_start = round(range_end_start, 1)
                    elif range_end_start > pow(10, -3):
                        range_end_start = round(range_end_start, 6)

                    explanation = explain_prediction(
                        self.transaction_output.transaction.lmd,
                        prediction_row, cluster['confidence'], pred_col)

                    answers[pred_col].append({
                        'value':
                        predicted_value,
                        'range':
                        value_range,
                        'confidence':
                        new_conf,
                        'explanation':
                        explanation,
                        'explaination':
                        explanation,
                        'simple':
                        f'We are {pct_confidence}% confident the value of "{pred_col}" lies between {range_pretty_start} and {range_end_start}'
                    })
                else:
                    explanation = explain_prediction(
                        self.transaction_output.transaction.lmd,
                        prediction_row, cluster['confidence'], pred_col)
                    answers[pred_col].append({
                        'value':
                        predicted_value,
                        'confidence':
                        new_conf,
                        'explanation':
                        explanation,
                        'explaination':
                        explanation,
                        'simple':
                        f'We are {pct_confidence}% confident the value of "{pred_col}" is {predicted_value}'
                    })

                if self.transaction_output.input_confidence is not None:
                    for i in range(len(answers[pred_col])):
                        answers[pred_col][i]['confidence_influence_scores'] = {
                            'confidence_variation_score': [],
                            'column_names': []
                        }
                        for c in self.transaction_output.input_confidence:
                            answers[pred_col][i][
                                'confidence_influence_scores'][
                                    'confidence_variation_score'].append(
                                        self.transaction_output.
                                        input_confidence[c])
                            answers[pred_col][i][
                                'confidence_influence_scores'][
                                    'column_names'].append(str(c))

                model_result = {'value': prediction_row[f'model_{pred_col}']}

                if f'{pred_col}_model_confidence' in prediction_row:
                    model_result['confidence'] = prediction_row[
                        f'{pred_col}_model_confidence']

                answers[pred_col][-1]['model_result'] = model_result

            answers[pred_col] = sorted(answers[pred_col],
                                       key=lambda x: x['confidence'],
                                       reverse=True)

        return answers