github.com/matrixorigin/matrixone@v1.2.0/pkg/udf/pythonservice/demo/credit/detection.py (about)

     1  # coding = utf-8
     2  # -*- coding:utf-8 -*-
     3  import decimal
     4  import os
     5  from typing import List
     6  
     7  import joblib
     8  import numpy as np
     9  
    10  model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'model_with_scaler')
    11  
    12  
    13  def detect(featuresList: List[List[int]], amountList: List[decimal.Decimal]) -> List[bool]:
    14      model_with_scaler = joblib.load(model_path)
    15  
    16      columns_features = np.array(featuresList)
    17      column_amount = np.array(amountList, dtype='float').reshape(-1, 1)
    18      column_amount = model_with_scaler['amount_scaler'].transform(column_amount)
    19      data = np.concatenate((columns_features, column_amount), axis=1)
    20      predictions = model_with_scaler['model'].predict(data)
    21      return [pred == 1 for pred in predictions.tolist()]
    22  
    23  
    24  detect.vector = True
    25  
    26  if __name__ == '__main__':
    27      ret = detect(
    28          [
    29              [-1.39598357648026, 1.07394393065875, 0.213508735697434, -1.00551307370986, 1.187300373936,
    30               -1.07111854041464, 0.753203638755905, 0.30971495602621, -0.983991867539784, -1.70071141514904,
    31               0.823426013946253, 0.755747784301563, 0.57208601947088, -0.493305378300063, -0.470447091684077,
    32               0.748161458746014, -0.0446257458090844, 1.15426779428755, -0.580978900127479, -0.04190738904369,
    33               0.313836892986802, 0.629705007512215, -0.702875042416293, -0.378664198371815, 1.17715716992152,
    34               -0.0670218758532906, -0.108995491056784, -0.0807276510778096],
    35              [-1.99458047217904, -1.63531939810522, 1.00669502032082, -1.43318273761075, 3.25919383016879,
    36               4.00823861971185, -1.66001967781548, 1.09946561236069, 1.91968533357403, -0.518543946411732,
    37               -1.05471184392788, 0.446539472296582, -0.468234843104227, -1.40433618221015, -1.8103263742068,
    38               -0.277012303056273, -0.290667580529318, -0.608743967003329, -0.218456639889371, -0.677311451910878,
    39               -0.258971403019176, 0.0809710102853903, -0.579907932919128, 0.830053981653551, -0.462078172323048,
    40               0.437226392974709, -0.170400933244463, 0.0175635565102101],
    41              [-3.0435406239976, -3.15730712090228, 1.08846277997285, 2.2886436183814, 1.35980512966107,
    42               -1.06482252298131, 0.325574266158614, -0.0677936531906277, -0.270952836226548, -0.838586564582682,
    43               -0.414575448285725, -0.503140859566824, 0.676501544635863, -1.69202893305906, 2.00063483909015,
    44               0.666779695901966, 0.599717413841732, 1.72532100745514, 0.283344830149495, 2.10233879259444,
    45               0.661695924845707, 0.435477208966341, 1.37596574254306, -0.293803152734021, 0.279798031841214,
    46               -0.145361714815161, -0.252773122530705, 0.0357642251788156],
    47              [-2.30334956758553, 1.759247460267, -0.359744743330052, 2.33024305053917, -0.821628328375422,
    48               -0.0757875706194599, 0.562319782266954, -0.399146578487216, -0.238253367661746, -1.52541162656194,
    49               2.03291215755072, -6.56012429505962, 0.0229373234890961, -1.47010153611197, -0.698826068579047,
    50               -2.28219382856251, -4.78183085597533, -2.61566494476124, -1.33444106667307, -0.430021867171611,
    51               -0.294166317554753, -0.932391057274991, 0.172726295799422, -0.0873295379700724, -0.156114264651172,
    52               -0.542627889040196, 0.0395659889264757, -0.153028796529788],
    53          ],
    54          [
    55              decimal.Decimal('2.35'),
    56              decimal.Decimal('17.6'),
    57              decimal.Decimal('529'),
    58              decimal.Decimal('239.93'),
    59          ]
    60      )
    61  
    62      print(ret)