|
For example, how can we add a new feature expressed as a ratio between two other features, by using the Khiops Python API? |
Replies: 3 comments
|
Custom features can be added by using the Khiops dictionary API, which is included in the Khiops Core API. To answer to the original example question, a ratio-like feature can be defined by using the Dictionary CreditInformation
{
Categorical CustomerId ;
Categorical CreditId ;
Numerical AccountBalance ;
Numerical CreditLine ;
Numerical CreditUtilizationRate = Divide(AccountBalance, CreditLine) ;
};Such a dictionary can be constructed via the # Perform the necessary imports
from khiops import core as kh
# Build empty dictionary domain object
credit_information_dict_domain = kh.DictionaryDomain()
# Build empty dictionary object; specify its name
credit_information_dict = kh.Dictionary()
credit_information_dict.name = "CreditInformation"
# Build customer ID variable; specify its name and type
customer_id = kh.Variable()
customer_id.name = "CustomerId"
customer_id.type = "Categorical"
# Build credit ID variable; specify its name and type
credit_id = kh.Variable()
credit_id.name = "CreditId"
credit_id.type = "Categorical"
# Build account balance variable; specify its name and type
account_balance = kh.Variable()
account_balance.name = "AccountBalance"
account_balance.type = "Numerical"
# Build credit line variable; specify its name and type
credit_line = kh.Variable()
credit_line.name = "CreditLine"
credit_line.type = "Numerical"
# Built credit utilization rate variable; specify its name and type
credit_utilization_rate = kh.Variable()
credit_utilization_rate.name = "CreditUtilizationRate"
credit_utilization_rate.type = "Numerical"
# Add variable construction rule
credit_utilization_rate.rule = "Divide(AccountBalance, CreditLine)"
# Add variables to the credit information dictionary
for variable in [
customer_id, credit_id, account_balance, credit_line, credit_utilization_rate
]:
credit_information_dict.add_variable(variable)
# Add credit information dictionary to the dictionary_domain
credit_information_dict_domain.add_dictionary(credit_information_dict)
# Write the credit information dictionary domain to a file on disk
credit_information_dict_domain.export_khiops_dictionary_file(
"/path/to/credit_information.kdic"
)Thus, a Khiops dictionary file with the |
|
This response is a good answer and can cover part of my scenario where I need to customize the algorithm. For multiple accounts, there are two algorithms for calculating the credit utilization rate. The first is to calculate the credit utilization rate for each account and then take the average. The second is to first aggregate the credit limits and loan balances, then divide the total loan balance by the total credit limit to obtain the overall credit utilization rate. The solution provided above calculates the first method. How can I customize the calculation for the second method?
|
|
These types of aggregations can be modeled, in Khiops, via a
Thus, by using the Khiops dictionary
The resulting Khiops dictionary (kdic) file is: These dictionaries can be constructed via the This dictionary can be loaded into Khiops and used with data as follows:
Using the Khiops dictionary for training and deploying the model on these same data, the outputs obtained contain the computed fields, for both
|
Custom features can be added by using the Khiops dictionary API, which is included in the Khiops Core API.
For an introduction to the concept of Khiops dictionaries, we can refer to https://khiops.org/api-docs/ on the official Khiops web site. For starting using and defining Khiops dictionaries, we can start from https://khiops.org/tutorials/kdic_intro/.
To answer to the original example question, a ratio-like feature can be defined by using the
Dividerule, as in the following example, where the credit utilization rate of each customer is computed as the ratio between the account balance and the credit line,in the
CreditInformationdictionary file: