Method, device, equipment and readable storage medium for generating code statements

By converting the decision tree in the random forest model into compatible code statements, the problem of incompatibility between the random forest model and existing code statements in business deployment is solved, and more efficient model training and artificial intelligence promotion is achieved.

CN111324344BActive Publication Date: 2025-03-28WEBANK (CHINA)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202010128183.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-02-28
Publication Date
2025-03-28
Estimated Expiration
2040-02-28

AI Technical Summary

Technical Problem

In the prior art, the random forest model and code statements are incompatible in business deployment, and it is difficult to be compatible with existing services implemented through code statements such as hive or SQL.

Method used

By determining the conversion strategy based on the model type of the random forest model, converting the decision tree into a string or a string array, and generating corresponding prediction code statements to achieve compatibility between the random forest model and the code statements.

Benefits of technology

It realizes the compatibility of the random forest model and code statements in business deployment, avoids the need to retrain the model or continue to use code statements, improves the training efficiency of the model, and is conducive to the promotion of artificial intelligence.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111324344B_ABST
    Figure CN111324344B_ABST
Patent Text Reader

Abstract

The present invention discloses a method, device, equipment and readable storage medium for generating a code statement, the method comprising the following steps: according to the model type of a random forest model, determining a conversion strategy for converting the random forest model; if the conversion strategy is a single-layer strategy, converting all decision trees in the random forest model into character strings, and generating a first prediction code statement according to each of the character strings; if the conversion strategy is a nested strategy, converting all decision trees in the random forest model into a character string array; according to each of the character string arrays, generating probability prediction codes corresponding to the decision categories of each of the decision trees, and generating each of the probability prediction codes as a second prediction code statement. The present invention converts the random forest model into a code statement, and realizes the compatibility of the random forest model and the code statement in business deployment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of financial technology (Fintech), and in particular to a method, device, equipment and readable storage medium for generating a code statement. Background Art

[0002] With the continuous development of financial technology (Fintech), especially Internet technology finance, more and more technologies (such as artificial intelligence, big data, cloud storage, etc.) are applied in the financial field, but the financial field also puts forward higher requirements for various technologies, such as requiring the prediction function of machine learning models to be used in big data business deployment.

[0003] Currently, with the development of big data technology, more and more data services are running on big data platforms; and in big data business logic, various machine learning models are generally used, such as the prediction function of the random forest model used in actual business deployment. However, the prediction function in a large number of business deployments is currently implemented through code statements such as hive (data warehouse tool) or SQL (Structured Query Language). If the business deployment is set to be implemented through the random forest model, it will be difficult to be compatible with the existing business implemented through code statements such as hive or SQL. Therefore, how to achieve compatibility between the random forest model and code statements in business deployment is a technical problem that needs to be solved urgently. Summary of the invention

[0004] The main purpose of the present invention is to provide a method, device, equipment and readable storage medium for generating code statements, aiming to solve the technical problem of incompatibility between random forest models and code statements in business deployment in the prior art.

[0005] To achieve the above object, the present invention provides a method for generating a code statement, the method comprising the following steps:

[0006] Determining a conversion strategy for converting the random forest model according to a model type of the random forest model;

[0007] If the conversion strategy is a single-layer strategy, all decision trees in the random forest model are converted into character strings, and a first prediction code statement is generated according to each of the character strings;

[0008] If the conversion strategy is a nested strategy, all decision trees in the random forest model are converted into string arrays;

[0009] According to each of the character string arrays, probability prediction codes corresponding to the decision categories of each of the decision trees are generated, and each of the probability prediction codes is generated as a second prediction code statement.

[0010] Optionally, the step of converting all decision trees in the random forest model into string arrays includes:

[0011] Converting the branch nodes in each decision tree into a judgment statement block, and converting the leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree;

[0012] According to the decision categories of the decision trees, the judgment statement blocks and the corresponding result variable groups corresponding to the decision trees are generated into character strings of the decision trees;

[0013] The character strings of each decision tree are added to a character string array for storage, wherein the decision categories corresponding to the array elements in the same position order in each character string array are the same.

[0014] Optionally, the step of generating probability prediction codes corresponding to the decision categories of each decision tree respectively according to each of the character string arrays comprises:

[0015] Reading array elements having the same position order in each of the string arrays;

[0016] Array elements with the same position order are combined to generate probability prediction codes corresponding to the decision categories of each decision tree.

[0017] Optionally, the step of generating each of the probability prediction codes into a second prediction code statement includes:

[0018] Generating each of the probability prediction codes into a probability prediction array, and determining an initial comparison element in the probability prediction array;

[0019] The initial comparison element is added to the preset comparison code to generate a second prediction code statement.

[0020] Optionally, after the step of generating each of the probability prediction codes into a second prediction code statement, the method further comprises:

[0021] When receiving the execution request, controlling the second prediction code statement to execute, and generating each of the probability prediction codes in the probability prediction array into a probability value respectively;

[0022] Based on the cyclic comparison condition in the preset comparison code, compare the probability values ​​to determine the maximum probability value among the probability values;

[0023] Find a decision category corresponding to the maximum probability value, and output the decision category as the running result of the second prediction code statement.

[0024] Optionally, the step of converting all decision trees in the random forest model into character strings and generating a first prediction code statement according to each character string includes:

[0025] Convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable;

[0026] Generate the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree;

[0027] The character strings are concatenated according to a preset concatenation rule to generate a first prediction code statement.

[0028] Optionally, after the step of concatenating the character strings to generate the first prediction code statement, the method further includes:

[0029] receiving a correction instruction for correcting the first prediction code statement, and reading correction information corresponding to the correction instruction;

[0030] According to the correction information, the first prediction code statement is updated and executed.

[0031] Optionally, the step of determining a conversion strategy for converting the random forest model according to the model type of the random forest model includes:

[0032] Identify the model type of the random forest model, and if the model type is a regression model, determine that the conversion strategy for converting the random forest model is a single-layer strategy;

[0033] If the model type is a classification model, it is determined that the conversion strategy for converting the random forest model is a nested strategy.

[0034] Furthermore, to achieve the above-mentioned purpose, the present invention also provides a device for generating a code statement, the device for generating a code statement comprising:

[0035] A determination module, used to determine a conversion strategy for converting the random forest model according to the model type of the random forest model;

[0036] A first generating module, configured to convert all decision trees in the random forest model into character strings if the conversion strategy is a single-layer strategy, and generate a first prediction code statement according to each of the character strings;

[0037] A conversion module, used for converting all decision trees in the random forest model into string arrays if the conversion strategy is a nested strategy;

[0038] The second generating module is used to generate probability prediction codes corresponding to the decision categories of each decision tree according to each character string array, and generate each probability prediction code into a second prediction code statement.

[0039] Furthermore, to achieve the above-mentioned purpose, the present invention also provides a device for generating a code statement, the device for generating a code statement comprising a memory, a processor, and a code statement generation program stored in the memory and executable on the processor, the code statement generation program implementing the steps of the code statement generation method as described above when executed by the processor.

[0040] Furthermore, to achieve the above-mentioned purpose, the present invention also provides a readable storage medium, on which a code statement generation program is stored, and when the code statement generation program is executed by a processor, the steps of the code statement generation method as described above are implemented.

[0041] The method for generating code statements of the present invention is provided with a mechanism for generating code statements by converting a random forest model, and different conversion strategies are provided for different model types of the random forest model, and the conversion strategies include at least a single-layer strategy and a nested strategy. If the conversion strategy for converting the random forest model is determined to be a single-layer strategy according to the model type of the random forest model, the decision tree in the random forest model is first converted into a string, and then each string is generated as a first prediction code statement; if the conversion strategy for converting the random forest model is determined to be a nested strategy, the decision tree in the random forest model is first converted into a string array, and then the probability prediction code corresponding to the decision category of each decision tree is generated according to each string array, and then each probability prediction code is generated as a second prediction code statement. In this way, the random forest model is converted into a code statement, and the compatibility of the random forest model and the code statement in business deployment is realized, and the prediction function realized by the code statement in the business deployment is avoided. Retraining the random forest model or continuing to use the code statement, while retaining the initial prediction function of the business deployment, improves the training efficiency of the random forest model, and is conducive to the promotion of artificial intelligence. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 A schematic diagram of the structure of the device hardware operating environment involved in the embodiment of the device for generating code statements of the present invention;

[0043] Figure 2 It is a flowchart of a first embodiment of a method for generating a code statement of the present invention;

[0044] Figure 3 It is a functional module diagram of a preferred embodiment of the device for generating code statements of the present invention.

[0045] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0046] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.

[0047] The present invention provides a device for generating code statements, referring to Figure 1 , Figure 1 It is a structural schematic diagram of the device hardware operating environment involved in the embodiment of the device for generating code statements of the present invention.

[0048] like Figure 1 As shown, the device for generating the code statement may include: a processor 1001, such as a CPU, a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.

[0049] Those skilled in the art will understand that Figure 1 The hardware structure of the device for generating code statements shown in the figure does not constitute a limitation on the device for generating code statements, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.

[0050] like Figure 1 As shown, the memory 1005 as a readable storage medium may include an operating system, a network communication module, a user interface module, and a code statement generation program. The operating system is a program that manages and controls the code statement generation device and software resources, and supports the operation of the network communication module, the user interface module, the code statement generation program, and other programs or software; the network communication module is used to manage and control the network interface 1004; the user interface module is used to manage and control the user interface 1003.

[0051] exist Figure 1In the hardware structure of the device for generating code statements shown in the figure, the network interface 1004 is mainly used to connect to the backend server and perform data communication with the backend server; the user interface 1003 is mainly used to connect to the client (user end) and perform data communication with the client; the processor 1001 can call the code statement generation program stored in the memory 1005 and perform the following operations:

[0052] Determining a conversion strategy for converting the random forest model according to a model type of the random forest model;

[0053] If the conversion strategy is a single-layer strategy, all decision trees in the random forest model are converted into character strings, and a first prediction code statement is generated according to each of the character strings;

[0054] If the conversion strategy is a nested strategy, all decision trees in the random forest model are converted into string arrays;

[0055] According to each of the character string arrays, probability prediction codes corresponding to the decision categories of each of the decision trees are generated, and each of the probability prediction codes is generated as a second prediction code statement.

[0056] Optionally, the step of converting all decision trees in the random forest model into string arrays includes:

[0057] Converting the branch nodes in each decision tree into a judgment statement block, and converting the leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree;

[0058] According to the decision categories of the decision trees, the judgment statement blocks and the corresponding result variable groups corresponding to the decision trees are generated into character strings of the decision trees;

[0059] The character strings of each decision tree are added to a character string array for storage, wherein the decision categories corresponding to the array elements in the same position order in each character string array are the same.

[0060] Optionally, the step of generating, according to each of the character string arrays, probability prediction codes corresponding to the decision categories of each of the decision trees comprises:

[0061] Reading array elements having the same position order in each of the string arrays;

[0062] Array elements with the same position order are combined to generate probability prediction codes corresponding to the decision categories of each decision tree.

[0063] Optionally, the step of generating each of the probability prediction codes into a second prediction code statement includes:

[0064] Generating each of the probability prediction codes into a probability prediction array, and determining an initial comparison element in the probability prediction array;

[0065] The initial comparison element is added to the preset comparison code to generate a second prediction code statement.

[0066] After the step of generating each of the probability prediction codes into a second prediction code statement, the processor 1001 may call a code statement generation program stored in the memory 1005, and perform the following operations:

[0067] When receiving the execution request, controlling the second prediction code statement to execute, and generating each of the probability prediction codes in the probability prediction array into a probability value respectively;

[0068] Based on the cyclic comparison condition in the preset comparison code, compare the probability values ​​to determine the maximum probability value among the probability values;

[0069] Find a decision category corresponding to the maximum probability value, and output the decision category as the running result of the second prediction code statement.

[0070] Optionally, the step of converting all decision trees in the random forest model into character strings and generating a first prediction code statement according to each character string includes:

[0071] Convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable;

[0072] Generate the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree;

[0073] The character strings are concatenated according to a preset concatenation rule to generate a first prediction code statement.

[0074] Optionally, after the step of concatenating the character strings to generate the first predicted code statement, the processor 1001 may call a code statement generation program stored in the memory 1005 and perform the following operations:

[0075] receiving a correction instruction for correcting the first prediction code statement, and reading correction information corresponding to the correction instruction;

[0076] According to the correction information, the first prediction code statement is updated and executed.

[0077] Optionally, the step of determining a conversion strategy for converting the random forest model according to the model type of the random forest model includes:

[0078] Identify the model type of the random forest model, and if the model type is a regression model, determine that the conversion strategy for converting the random forest model is a single-layer strategy;

[0079] If the model type is a classification model, it is determined that the conversion strategy for converting the random forest model is a nested strategy.

[0080] The specific implementation of the device for generating code statements of the present invention is basically the same as the embodiments of the method for generating code statements described below, and will not be described in detail here.

[0081] The invention also provides a method for generating a code statement.

[0082] Reference Figure 2 , Figure 2 It is a flowchart diagram of the first embodiment of the method for generating code statements of the present invention.

[0083] The embodiment of the present invention provides an embodiment of a method for generating a code statement. It should be noted that although the logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than that shown here. Specifically, the method for generating a code statement in this embodiment includes:

[0084] Step S10, determining a conversion strategy for converting the random forest model according to the model type of the random forest model;

[0085] The method for generating code statements in this embodiment is applied to the server, and is suitable for converting the trained random forest model through the server to generate code statements. Among them, the generated statements can be hive, spark SQL or SQL. The random forest model is an integrated learning method based on decision trees. It establishes a forest in a random way, and the forest is composed of many decision trees. During the training process, each decision tree samples the two dimensions of samples and features to obtain multiple independent decision tree models. Usually, the prediction function of the random forest model is used in actual business deployment. The conversion of the random forest model in this embodiment is essentially to convert the processing logic of the random forest model to realize the prediction function into a code statement for prediction.

[0086] Furthermore, the random forest model includes a random forest regression model and a random forest classification model, and different conversion strategies are set for the two different types of random forest models. Therefore, before converting the random forest model, it is necessary to first determine the model type of the random forest model, determine the conversion strategy by the model type, and then convert it through the conversion strategy. Specifically, according to the model type of the random forest model, the steps of determining the conversion strategy for converting the random forest model include:

[0087] Step S11, identifying the model type of the random forest model, and if the model type is a regression model, determining that the conversion strategy for converting the random forest model is a single-layer strategy;

[0088] Step S12: If the model type is a classification model, determine that the conversion strategy for converting the random forest model is a nested strategy.

[0089] Understandably, there are differences in the logical processing of the random forest regression model and the random forest classification model, so the model type can be determined by identifying the logical processing method of the random forest model. Alternatively, different identifiers are set for the random forest regression model and the random forest classification model in advance, and the model type is determined by identifying the identifier type carried by the random forest model.

[0090] Furthermore, the random forest regression model is referred to as the regression model, and the random forest classification model is referred to as the classification model. If the model type of the random forest model is identified as a regression model, the conversion strategy for converting it is determined to be a single-layer strategy; if the model type is identified as a classification model, the conversion strategy for converting the random forest model is determined to be a nested strategy. The single-layer strategy can use a layer of select function statements to achieve conversion, and the nested strategy can use nested select function statements to achieve conversion.

[0091] Step S20, if the conversion strategy is a single-layer strategy, convert all decision trees in the random forest model into character strings, and generate a first prediction code statement according to each of the character strings;

[0092] Furthermore, if it is determined that the conversion strategy for converting the random forest model is a single-layer strategy, all decision trees in the random forest are converted to obtain strings corresponding to each decision tree. Each string contains multiple statements for implementing the judgment function to characterize the processing logic of each decision tree. Thereafter, each string is combined and processed to generate a first prediction code statement, and the prediction function of the regression model is realized through the first prediction code statement.

[0093] Step S30, if the conversion strategy is a nested strategy, all decision trees in the random forest model are converted into string arrays;

[0094] Furthermore, if it is determined that the conversion strategy for converting the random forest model is a nested strategy, all decision trees in the random forest are converted to obtain a string array corresponding to each decision tree. Each string array contains multiple strings, and the number of strings is consistent with the classification type implemented by the decision tree. For example, if the decision tree implements the classification of customer loan risks, and the classification types include high risk, medium risk and low risk, then the string array generated by its conversion contains three strings, which respectively represent the predicted probabilities of the decision tree for the three types of customer loan risks.

[0095] Step S40, generating probability prediction codes corresponding to the decision categories of each decision tree according to each of the character string arrays, and generating each of the probability prediction codes as a second prediction code statement.

[0096] Furthermore, the classification type implemented by the decision tree is used as the decision category of the decision tree, such as the above-mentioned high risk, medium risk and low risk classification, which are three decision categories. After the character string arrays are obtained through conversion, the probability prediction code representing the decision category of each decision tree can be generated through the character strings in each character string array. The character strings representing the same decision category in each character string array are classified and processed, and the probability prediction code corresponding to each decision category is generated to predict the probability of each decision category and determine the category to which it belongs. Thereafter, the probability prediction codes are combined and processed to generate a second prediction code statement, so as to realize the classification prediction function of the classification model through the second prediction code statement.

[0097] The method for generating code statements of the present invention is provided with a mechanism for generating code statements by converting a random forest model, and different conversion strategies are provided for different model types of the random forest model, and the conversion strategies include at least a single-layer strategy and a nested strategy. If the conversion strategy for converting the random forest model is determined to be a single-layer strategy according to the model type of the random forest model, the decision tree in the random forest model is first converted into a string, and then each string is generated as a first prediction code statement; if the conversion strategy for converting the random forest model is determined to be a nested strategy, the decision tree in the random forest model is first converted into a string array, and then the probability prediction code corresponding to the decision category of each decision tree is generated according to each string array, and then each probability prediction code is generated as a second prediction code statement. In this way, the random forest model is converted into a code statement, and the compatibility of the random forest model and the code statement in business deployment is realized, and the prediction function realized by the code statement in the business deployment is avoided. Retraining the random forest model or continuing to use the code statement, while retaining the initial prediction function of the business deployment, improves the training efficiency of the random forest model, and is conducive to the promotion of artificial intelligence.

[0098] Furthermore, based on the first embodiment of the method for generating code statements of the present invention, a second embodiment of the method for generating code statements of the present invention is proposed.

[0099] The difference between the second embodiment of the method for generating code statements and the first embodiment of the method for generating code statements is that the step of converting all decision trees in the random forest model into a string array includes:

[0100] Step S31, converting the branch nodes in each decision tree into a judgment statement block, and converting the leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree;

[0101] Step S32, generating a character string of each decision tree from a judgment statement block and a corresponding result variable group corresponding to each decision tree according to the decision category of each decision tree;

[0102] Step S33, adding the character strings of each decision tree to a character string array for storage, wherein the decision categories corresponding to the array elements in the same position order in each character string array are the same.

[0103] In the process of converting the classification model in the random forest model, the present embodiment first converts the branch nodes in each decision tree into a judgment statement block; wherein the branch node includes a parent node and a child node, and the judgment statement block can be a casewhen else statement block. Thereafter, according to the decision category of each decision tree, the leaf nodes of each decision tree are converted into a result variable group, and all the leaf nodes of a decision tree form a result variable group, which represents all the classification types of the decision tree. In the actual operation of the decision tree, the branch node represents the judgment classification, and the leaf node represents the probability predicted by the decision tree; after conversion to the result variable group, the judgment statement block is used to judge the classification, and the result variable group is used to store the predicted probability generated by the code operation. That is, the predicted probability obtained after the judgment statement block judges the classification replaces the variables in the result variable group to realize the storage of the generated predicted probability.

[0104] Further, in order to characterize the stored association relationship between the statement block and the result variable group, the judgment statement block formed by each decision tree and the respective result variable group can be combined to generate a string of each decision tree according to the decision category of each decision tree. Among them, a preset code for combining the judgment statement block and the result variable group is pre-set, and the judgment statement block of each decision tree and the corresponding result variable group are added to the preset code respectively, so as to generate the string of each decision tree. For example, decision trees a and b can both realize the probability prediction of two classification types; wherein the judgment statement block formed by a is A, and the elements in the result variable group formed are a1 and a2; the judgment statement block formed by b is B, and the elements in the result variable group formed are b1 and b2; then the judgment statement A and the result variable group composed of a1 and a2 are added to the preset code to form a string of a; at the same time, the judgment statement B and the result variable group composed of b1 and b2 are added to the preset code to form a string of b. In addition, in order to distinguish the strings formed by each decision tree, a preset identifier, such as parentheses, brackets, etc., can be added to the first position of each string.

[0105] Furthermore, the strings generated by each decision tree are added to a string array for storage, and the classification type of each decision tree is represented by the string array of each decision tree. For example, the string array Stree-1[] = {(String1-1), (String1-2)} of a decision tree represents that the strings generated by the decision tree include String-1 and String-1, and have two classification types. In addition, considering that there are many decision trees included in the random forest, each decision tree is trained with different data samples, so that the classification types that can be achieved by each decision tree are different. In order to facilitate the determination of the classification type of each decision tree and the processing of each classification type, the array elements in the same position order in the string array of each decision tree are set to represent the same classification type. For example, between the string arrays Stree-2[] = {(String2-1), (String2-2)} and Stree-1[] = {(String1-1), (String1-2)} generated by the decision tree, the classification types represented by (String2-1) and (String1-1) at the first position in the two string arrays are the same, such as both classifying age. Similarly, the classification types represented by (String2-2) and (String1-2) at the second position in the two string arrays are also the same, such as both classifying gender, etc.

[0106] Furthermore, after each decision tree in the random forest model is converted into a string array, a probability prediction code for judging and classifying each decision category can be generated based on each string array, that is, a probability prediction code corresponding to the decision category of each decision tree. Specifically, the steps of generating a probability prediction code corresponding to the decision category of each decision tree according to each string array include:

[0107] Step S41, reading array elements having the same position order in each of the string arrays;

[0108] Step S42, combining the array elements with the same position order to generate probability prediction codes corresponding to the decision categories of each decision tree.

[0109] Furthermore, because the array elements in the same position order in each string array represent the same classification type, when generating the probability prediction code for determining the classification of each decision category, the array elements with the same position order in each string are read one by one, such as reading the above (String2-1) and (String1-1). After that, the array elements in the same position order read are combined to obtain the probability prediction code of the decision category represented by the position order in the decision tree; after the array elements of the same position data are read and combined to generate the probability prediction code of each decision category, the generation of the probability prediction code of each decision category in the random forest model is completed. Among them, the combination processing is essentially to first connect each array element with the mathematical symbol "+", then count the number of connected array elements, and then perform a ratio processing process between each connected array element and the number of array elements. During the actual operation of the code, the result obtained by the combination processing is the average prediction probability of the probability obtained by running each combination element after mean processing, and the prediction of the classification type represented by the random forest is the average value of the prediction probability of each decision tree. In this way, the probability predicted by the random forest for the decision category is represented by the average value of the predicted probability, which is beneficial to the accuracy of the prediction.

[0110] Furthermore, after obtaining the probability prediction code of each decision tree on each decision category, a second prediction code statement can be generated according to each probability prediction code, so as to realize the classification prediction function of the random forest model through the second prediction code statement. Specifically, the step of generating each probability prediction code into the second prediction code statement includes:

[0111] Step S43, generating each of the probability prediction codes into a probability prediction array, and determining an initial comparison element in the probability prediction array;

[0112] Step S44: adding the initial comparison element to the preset comparison code to generate a second prediction code statement.

[0113] Furthermore, each probability prediction code is added to the random forest array to generate a probability prediction array for predicting each classification category of the random forest model. For example, for the string arrays Stree-1[0, 1···k], Stree-2[0, 1···k]···Stree-n[0, 1···k], where n is the number of decision trees and k is the number of decision categories, the probability prediction codes generated by the array elements in the same position order are (Stree-1[0]+Stree-2[0]+···+Stree-n[0]) / n, (Stree-1[1]+Stree-2[1]+···+Stree-n[1]) / n···(Stree-1[k]+Stree-2[k]+··· +Stree-n[k]) / n; set the random forest array Sforest-all, add the generated probability prediction codes to Sforest-all, and obtain the probability prediction array Sforest-all=select((Stree-1[0]+Stree-2[0]+···+Stree-n[0]) / n,(Stree-1[1]+Stree-2[1]+···+Stree-n[1]) / n···(Stree-1[k]+Stree-2[k]+···+Stree-n[k]) / n)from Stree-all.

[0114] Understandably, each probability prediction code in the probability prediction array represents the possibility of the random forest model predicting each classification type. The larger the probability value generated by the probability prediction code, the greater the possibility of the decision category corresponding to the probability prediction code. Therefore, in order to achieve category prediction, a mechanism for comparing each probability prediction code in the probability prediction array is provided. Specifically, the initial comparison element used to start the comparison in the probability prediction array is first determined. The element identifier of the initial comparison element is set according to the demand. It can be the element identifier of the first element in the probability prediction array or the element identifier of the last element in the probability prediction array. The server determines the initial comparison element in the probability prediction data group by identifying the element identifier.

[0115] Furthermore, a preset comparison code is pre-set, and by adding the set initial comparison element to the preset comparison code, a second prediction code statement for realizing the classification prediction function of the random forest model can be generated.

[0116] Understandably, the second prediction code statement may need to be corrected during the generation process due to the presence of erroneous characters caused by external factors, or it may need to be modified according to the operation scenario during the operation. Therefore, this embodiment is provided with a manual correction mechanism. Specifically, when a correction instruction is received from a correction personnel via a terminal connected to the server communication, the correction information carried therein is read, and the second prediction code statement is corrected by the correction information, so as to ensure that the second prediction code statement is correct and meet the operation requirements of the operation scenario.

[0117] Furthermore, after the step of generating each of the probability prediction codes into a second prediction code statement, the method further includes:

[0118] Step S45, when receiving the execution request, controlling the second prediction code statement to execute, and generating each of the probability prediction codes in the probability prediction array into a probability value;

[0119] Step S46, comparing the probability values ​​based on the cyclic comparison condition in the preset comparison code, and determining the maximum probability value among the probability values;

[0120] Step S47, searching for a decision category corresponding to the maximum probability value, and outputting the decision category as an execution result of the second prediction code statement.

[0121] When receiving a run request, which indicates that the classification prediction function of the classification model can be realized through the second prediction code statement, the second prediction code statement is controlled to run, and the probability prediction codes in the probability prediction array are generated into probability values ​​through running, which represents the probability size of the prediction of each decision category. In addition, a loop comparison condition for comparing the size of each probability value is set in the preset comparison code. After running to generate each probability prediction code into a probability value, each probability value is cyclically compared through the loop comparison condition to determine the maximum probability value with the largest value. The maximum probability value with the largest value represents the maximum possibility of the decision category. Thereafter, the probability prediction code that generates the element value with the largest value is searched, and the decision category represented by the probability prediction code is the decision category with the largest probability. The decision category is output as the running result generated by the running of the second prediction code statement, so as to realize the judgment classification of the random forest model.

[0122] This embodiment converts the classification model in the random forest model into a second prediction code statement, and classification prediction can be performed through the second prediction code statement, thereby achieving compatibility of the classification model and the code statement in the classification prediction function in business deployment; at the same time, through the nested strategy, the intermediate results calculated by each decision tree can be saved in a string array, avoiding multiple calculations for the same decision tree and saving computing resources.

[0123] Furthermore, based on the first or second embodiment of the method for generating a code statement of the present invention, a third embodiment of the method for generating a code statement of the present invention is proposed.

[0124] The difference between the third embodiment of the method for generating a code statement and the first or second embodiment of the method for generating a code statement is that the step of converting all decision trees in the random forest model into character strings and generating a first predicted code statement according to each of the character strings comprises:

[0125] Step S21, converting the branch nodes in each decision tree into a judgment statement block, and converting the leaf nodes in each decision tree into a result variable;

[0126] Step S22, generating the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree;

[0127] Step S23, concatenating the character strings according to a preset concatenation rule to generate a first predicted code statement.

[0128] In the process of converting the regression model in the random forest model, this embodiment first converts the branch nodes in each decision tree into a judgment statement block; wherein the branch node includes a parent node and a child node, and the judgment statement block can be a casewhen else statement block. At the same time, the leaf nodes of each decision tree are converted into a result variable, which represents the prediction results obtained by each decision tree through regression processing. In the actual operation of the decision tree, the branch node represents the comparison judgment, and the leaf node represents the result predicted by the decision tree; after conversion to the result variable, the judgment statement block is used for comparison and judgment, and the result variable is used to represent the prediction result generated by the code operation. That is, the prediction result obtained after the comparison and judgment of the judgment statement block is used to replace the variable of the result variable, indicating the prediction result generated by the regression processing.

[0129] Furthermore, in order to characterize the association between the statement block and the result variable group, the judgment statement blocks formed by each decision tree and their respective result variables are combined to generate a character string of each decision tree. Among them, a preset code for combining the judgment statement block and the result variable is pre-set, and the judgment statement block of each decision tree and the corresponding result variable are added to the preset code respectively, so as to generate the character string of each decision tree. In addition, in order to distinguish the character strings formed by each decision tree, a preset identifier, such as parentheses, brackets, etc., can be added to the first position of each character string.

[0130] Furthermore, a preset concatenation rule for concatenating the character strings of each decision tree is pre-set. After the character strings of each decision tree are generated, the preset concatenation rule is called to concatenate the character strings to obtain the first preset code statement. The concatenation process is essentially to first connect each character string with the mathematical symbol "+", then count the number of connected character strings, and then perform a ratio processing between each connected character string and the number of character strings. For example, for the character strings Stree-1, Stree-2...Stree-n generated by each decision tree, where n is the number of decision trees, the concatenation process results in (Stree-1+Stree-2+...+Stree-n) / n. Thereafter, the standard statement of the first preset code statement specification is supplemented to obtain the first preset code statement select Sforest from dataset.

[0131] It should be noted that during the actual running of the code, the result obtained by the splicing process is the average result of the running results of each string, which is used to characterize that the prediction result of the random forest model after regression processing is the balanced result of the prediction of each decision tree, which is conducive to the accuracy of the prediction.

[0132] Understandably, during the generation process of the first prediction code statement, there may be erroneous characters due to interference from external factors and need to be corrected, or during the operation process, it may need to be modified according to the operation scenario, such as modifying the name of the data table for which the first prediction code statement is executed. Therefore, this embodiment is provided with a manual correction mechanism. Specifically, after the step of concatenating the character strings to generate the first prediction code statement includes:

[0133] Step S24, receiving a correction instruction for correcting the first prediction code statement, and reading correction information corresponding to the correction instruction;

[0134] Step S25: updating and executing the first prediction code statement according to the correction information.

[0135] Furthermore, the server is connected to a terminal for monitoring and correcting the operation of the first prediction code, and the terminal can be a laptop computer, a desktop computer, a smart phone, etc. When there is a need to correct the first prediction code statement, the correction personnel inputs the correction information and sends the correction instruction through the display interface of the terminal. When the server receives the correction instruction for correcting the first prediction code statement, it reads the correction information carried by the correction instruction, and searches for the code line where the code statement that needs to be corrected in the first prediction code statement is located through the position information in the correction information. Thereafter, the code statement that needs to be corrected in the code line is replaced with the correction code statement in the correction information, so as to update the first prediction code statement, and run with the updated first prediction code statement. In this way, while ensuring that the first prediction code statement is correct, the operation requirements of the operation scenario are met.

[0136] This embodiment converts the regression model in the random forest model into a first prediction code statement, and regression prediction can be performed through the first prediction code statement, thereby achieving compatibility of the regression prediction function of the regression model and the code statement in business deployment; at the same time, conversion is performed through a single-layer strategy, which is conducive to improving the operating efficiency of the first prediction code statement obtained after conversion.

[0137] The invention also provides a device for generating code statements.

[0138] Reference Figure 3 , Figure 3 This is a functional module diagram of a first embodiment of a device for generating code statements of the present invention. The device for generating code statements comprises:

[0139] A determination module 10 is used to determine a conversion strategy for converting the random forest model according to the model type of the random forest model;

[0140] A first generating module 20, configured to convert all decision trees in the random forest model into character strings if the conversion strategy is a single-layer strategy, and generate a first prediction code statement according to each of the character strings;

[0141] A conversion module 30, configured to convert all decision trees in the random forest model into a string array if the conversion strategy is a nested strategy;

[0142] The second generating module 40 is used to generate probability prediction codes corresponding to the decision categories of each decision tree according to each character string array, and generate each probability prediction code into a second prediction code statement.

[0143] Furthermore, the conversion module 30 includes:

[0144] A first conversion unit, used to convert branch nodes in each decision tree into a judgment statement block, and convert leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree;

[0145] A first generating unit is used to generate a judgment statement block and a corresponding result variable group corresponding to each decision tree into a character string of each decision tree according to the decision category of each decision tree;

[0146] The first adding unit is used to add the character strings of each decision tree to the character string array for storage, wherein the decision categories corresponding to the array elements located in the same position order in each character string array are the same.

[0147] Furthermore, the second generating module 40 includes:

[0148] A reading unit, used for reading array elements having the same position sequence in each of the string arrays;

[0149] The combining unit is used to combine the array elements with the same position sequence to generate probability prediction codes corresponding to the decision categories of each decision tree.

[0150] Furthermore, the second generating module 40 further includes:

[0151] A first determining unit, configured to generate each of the probability prediction codes into a probability prediction array, and determine an initial comparison element in the probability prediction array;

[0152] The second adding unit is used to add the initial comparison element to the preset comparison code to generate a second prediction code statement.

[0153] Furthermore, the second generating module 40 further includes:

[0154] A control unit, configured to control the execution of the second prediction code statement when receiving an execution request, and to generate each of the probability prediction codes in the probability prediction array into a probability value;

[0155] A comparison unit, configured to compare the probability values ​​based on a cyclic comparison condition in the preset comparison code, and determine a maximum probability value among the probability values;

[0156] A search unit is used to search for a decision category corresponding to the maximum probability value, and output the decision category as an execution result of the second prediction code statement.

[0157] Furthermore, the first generating module 20 includes:

[0158] A second conversion unit, used to convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable;

[0159] A second generating unit, used for generating the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree;

[0160] The concatenation unit is used to concatenate the character strings according to a preset concatenation rule to generate a first prediction code statement.

[0161] Furthermore, the first generating module 20 further includes:

[0162] a receiving unit, configured to receive a correction instruction for correcting the first prediction code statement, and read correction information corresponding to the correction instruction;

[0163] An updating unit is used to update and run the first prediction code statement according to the correction information.

[0164] Furthermore, the determining module 10 further includes:

[0165] an identification unit, configured to identify a model type of the random forest model, and if the model type is a regression model, determine that a conversion strategy for converting the random forest model is a single-layer strategy;

[0166] The second determining unit is used to determine that the conversion strategy for converting the random forest model is a nested strategy if the model type is a classification model.

[0167] The specific implementation of the device for generating code statements of the present invention is basically the same as the embodiments of the method for generating code statements described above, and will not be described in detail herein.

[0168] In addition, an embodiment of the present invention further provides a readable storage medium.

[0169] The readable storage medium stores a code statement generation program, and when the code statement generation program is executed by a processor, the steps of the code statement generation method described above are implemented.

[0170] The readable storage medium of the present invention may be a computer-readable storage medium, and its specific implementation is substantially the same as the above-mentioned embodiments of the method for generating code statements, and will not be described in detail herein.

[0171] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the enlightenment of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims. All equivalent structures or equivalent process changes made using the contents of the specification and drawings of the present invention, or directly or indirectly used in other related technical fields, are protected by the present invention.

Claims

1. A method for generating a code statement, characterized in that: The method for generating the code statement comprises the following steps: Determining a conversion strategy for converting the random forest model according to a model type of the random forest model; If the conversion strategy is a single-layer strategy, all decision trees in the random forest model are converted into character strings, and a first prediction code statement is generated according to each of the character strings; If the conversion strategy is a nested strategy, all decision trees in the random forest model are converted into string arrays; Generate probability prediction codes corresponding to the decision categories of each decision tree according to each character string array, and generate each probability prediction code as a second prediction code statement; The step of converting all decision trees in the random forest model into character strings and generating a first prediction code statement according to each character string includes: Convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable; Generate the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree; According to a preset splicing rule, the character strings are spliced ​​to generate a first prediction code statement; The step of converting all decision trees in the random forest model into string arrays comprises: Converting the branch nodes in each decision tree into a judgment statement block, and converting the leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree; According to the decision categories of the decision trees, the judgment statement blocks and the corresponding result variable groups corresponding to the decision trees are generated into character strings of the decision trees; Add the character strings of each decision tree to a character string array for storage, wherein the array elements in the same position order in each character string array correspond to the same decision category; The step of generating probability prediction codes corresponding to the decision categories of each decision tree according to each of the character string arrays comprises: Reading array elements having the same position order in each of the string arrays; Array elements with the same position order are combined to generate probability prediction codes corresponding to the decision categories of each decision tree.

2. The method for generating a code statement according to claim 1, wherein: The step of generating each of the probability prediction codes into a second prediction code statement comprises: Generating each of the probability prediction codes into a probability prediction array, and determining an initial comparison element in the probability prediction array; The initial comparison element is added to the preset comparison code to generate a second prediction code statement.

3. The method for generating a code statement according to claim 1, wherein: After the step of generating each of the probability prediction codes into a second prediction code statement, the method further comprises: When receiving the execution request, controlling the second prediction code statement to execute, and generating each of the probability prediction codes in the probability prediction array into a probability value respectively; Based on the cyclic comparison condition in the preset comparison code, compare the probability values ​​to determine the maximum probability value among the probability values; Find a decision category corresponding to the maximum probability value, and output the decision category as the running result of the second prediction code statement.

4. The method for generating a code statement according to claim 1, wherein: After the step of concatenating the character strings to generate the first prediction code statement, the method further includes: receiving a correction instruction for correcting the first prediction code statement, and reading correction information corresponding to the correction instruction; According to the correction information, the first prediction code statement is updated and executed.

5. The method for generating a code statement according to any one of claims 1 to 4, characterized in that: The step of determining a conversion strategy for converting the random forest model according to the model type of the random forest model comprises: Identify the model type of the random forest model, and if the model type is a regression model, determine that the conversion strategy for converting the random forest model is a single-layer strategy; If the model type is a classification model, it is determined that the conversion strategy for converting the random forest model is a nested strategy.

6. A device for generating a code statement, characterized in that: The device for generating the code statement comprises: A determination module, used to determine a conversion strategy for converting the random forest model according to the model type of the random forest model; A first generating module, configured to convert all decision trees in the random forest model into character strings if the conversion strategy is a single-layer strategy, and generate a first prediction code statement according to each of the character strings; A conversion module, used for converting all decision trees in the random forest model into string arrays if the conversion strategy is a nested strategy; A second generating module is used to generate probability prediction codes corresponding to the decision categories of each decision tree according to each character string array, and generate each probability prediction code into a second prediction code statement; The first generating module is used to implement: Convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable; Generate the judgment statement blocks and the corresponding result variables corresponding to each decision tree into character strings of each decision tree; According to a preset splicing rule, the character strings are spliced ​​to generate a first prediction code statement; The conversion module comprises: A first conversion unit, used to convert the branch nodes in each decision tree into a judgment statement block, and convert the leaf nodes in each decision tree into a result variable group according to the decision category of each decision tree; A first generating unit is used to generate a judgment statement block and a corresponding result variable group corresponding to each decision tree into a character string of each decision tree according to the decision category of each decision tree; A first adding unit is used to add the character strings of each decision tree to a character string array for storage, wherein the decision categories corresponding to the array elements in the same position order in each character string array are the same; The second generation module comprises: A reading unit is used to read array elements with the same position order in each of the string arrays; a combining unit is used to combine the array elements with the same position order to generate probability prediction codes corresponding to the decision categories of each of the decision trees.

7. A device for generating code statements, characterized in that: The code statement generating device includes a memory, a processor, and a code statement generating program stored in the memory and executable on the processor. When the code statement generating program is executed by the processor, the steps of the code statement generating method as described in any one of claims 1 to 5 are implemented.

8. A readable storage medium, characterized in that: The readable storage medium stores a code statement generation program, and when the code statement generation program is executed by a processor, the steps of the code statement generation method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Decision tree model training method, and method and apparatus for determining data attributes in OCR result

    CN107273883A

  • Text recognition method and device for medical data pictures

    CN107315989A