搭建模型线上打分服务

在几台不同机器分别部署相同服务,指定IP和端口,让运维做负载均衡。

更新时,需几台机器依序关闭、修改、重启。

1. 启动服务

start_service.sh

nohup /data/zz/anaconda3/bin/python algorithms_predict.py >> ../logs/algorithms_predict.log 2>&1 &

2.关闭服务

stop_service.sh

#/bin/bash
PID=$(ps -ef|grep algorithms_predict | grep -v grep | awk '{print $2}')
if [ -z $PID ]; then
	  echo "process provider not exist" >> ../logs/algorithms_predict.log
	  exit
else
	  echo "process id: $PID" >> ../logs/algorithms_predict.log
	  kill -9 ${PID}
	  echo "process algorithms_predict killed" >> ../logs/algorithms_predict.log
fi

3. 打分脚本

algorithms_predict.py

from flask import Flask, request, jsonify, render_template
import lightgbm as lgb
import numpy as np


app = Flask(__name__)
lx_strangers_model = lgb.Booster(model_file='../../models/strangers_model.txt')
lx_1d_is_agree_model = lgb.Booster(model_file='../../models/lx_1d_is_agree_v9.txt')

@app.route('/lx_1d_is_agree/predict',methods=['POST', 'GET'])
def lx_1d_is_agree_predict():
    data = request.get_json(force=True)
    features = np.array(data['features']).reshape(1, -1)
    threshold = data['threshold']
    score = lx_1d_is_agree_model.predict(features)[0]

    output = {}
    output['uid'] = data['uid']
    output['uid_f'] = data['uid_f']
    output['score'] = score
    output['is_agree'] = 0
    if score >= threshold:
        output['is_agree'] = 1
    return jsonify(output)


@app.route('/lx_strangers/predict',methods=['POST', 'GET'])
def lx_strangers_predict():
    data = request.get_json(force=True)
    features = np.array(data['features']).reshape(1, -1)
    threshold = data['threshold']
    features_ = features.copy()
    features_[0, -1] = 0
    score0 = lx_strangers_model.predict(features_)[0]
    features_[0, -1] = 1
    score1 = lx_strangers_model.predict(features_)[0]

    output = {}
    output['uid'] = data['uid']
    output['score0'] = score0
    output['score1'] = score1
    output['is_cancel'] = 0
    if features[0, -1] == 1:
        output['is_cancel'] = 1
    elif score1 > score0 and score0 < threshold and score1 >= threshold:
        output['is_cancel'] = 1
    return jsonify(output)


def start_server():
    app.run(host='0.0.0.0', port=5005, threaded=True)

if __name__ == '__main__':
    start_server()

风控模型搭建的方法和步骤可从设计维度、审核流程等方面了解。 在设计维度上,风控模型采用多维度和多角度的设计方式。以线上借贷用户资质审核模型为例,审核流程通常是机器审核和人工确认相结合。机器模型审核大致分为三个阶段,先对用户提交的数据进行处理,以对用户资质进行审核,最终得出评分卡分数。具体操作上,风控模型一般会过滤高危地区的黑名单,避免与可能存在金融欺诈的人群有业务往来;接着通过系统设置的评分规则对用户个人信息进行评定;最后结合其他输入资料微调分数,得到最终评分卡分数。并且,很多金融公司不管是针对高评分用户还是存疑情况,都需要人工进行最后的确认,其风控模型的计算策略和机制属于公司绝密信息,规则只有核心员工知晓[^4]。 从上线部署角度来看,风控模型线下开发好后,需进行上线部署,此环节关系到模型的稳定性和可控性,需要规范化操作。同时,还需关注上线后的数据校验和监控等问题,在介绍上线方式时会分适用场景、部署步骤和细节以及方法的优缺点等方面进行阐述[^1]。 ```python # 以下是一个简单的模拟风控模型评分计算示例 def risk_score_calculation(user_info): # 假设过滤高危地区黑名单 if user_info['region'] in ['high_risk_region1', 'high_risk_region2']: return 0 # 假设系统设置的评分规则 base_score = user_info['credit_history'] * 0.3 + user_info['income'] * 0.5 + user_info['age'] * 0.2 # 假设其他输入资料微调分数 adjustment = 5 if user_info['has_asset'] else -5 final_score = base_score + adjustment return final_score # 示例用户信息 user_info = { 'region': 'normal_region', 'credit_history': 7, 'income': 8000, 'age': 30, 'has_asset': True } score = risk_score_calculation(user_info) print(f"用户的风控评分是: {score}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值