如何将代码从 R 翻译为 python?

问题描述 投票:0回答:1

我需要将代码从 R 翻译为 python。 你能帮我么? 我附上代码

功能一: 使用验证数据集找到截止值

    pred = xgboost::predict(modelo, dval)
    predM = data.frame(t(matrix(pred, nrow = nClasses)))

    if (metodo_precision == 'KDP') {
      # we return the list of breakpoints
      cutoffs_list = lapply(as.list(precisiones_posibles), function(precision) {
        RECIBOS.FIND_CUTOFFS(nombre_proc_log = nombre_proc_log,
                             nombre_tabla_logs = nombre_tabla_logs,
                             nombre_secuencia = nombre_secuencia,
                             required_precision = precision,
                             predM = predM, df.val = df.val,
                             var_obj = var_obj)
      })
      # We convert to dataframe to save in a table
      cutoffs_df = as.data.frame(t(as.data.frame(x = cutoffs_list, stringsAsFactors = FALSE)),
                                 stringsAsFactors = FALSE)
      names(cutoffs_df) = c("PROB_C", "PROB_D", "PROB_R")
      cutoffs_df$PREC = precisiones_posibles*100
      rownames(cutoffs_df) = cutoffs_df$PREC
      cutoffs_df = cutoffs_df[, c("PREC", "PROB_C", "PROB_D", "PROB_R")]
      # We save the results in the table tabla_prec_prob
      ore.drop(tabla_prec_prob)
      ore.create(x = cutoffs_df, table = tabla_prec_prob)
    } else {
      # We create the enriched validation table that, in addition to the probabilities, contains the precisions
      crear_tabla_val = RECIBOS.CREAR_VALIDACION(nombre_proc_log = nombre_proc_log,
                                                 nombre_tabla_logs = nombre_tabla_logs,
                                                 nombre_secuencia = nombre_secuencia,
                                                 tabla_enriquecida_validacion = tabla_enriquecida_validacion,
                                                 campos_pk_incidencia = campos_pk_incidencia,
                                                 var_obj = var_obj,
                                                 probabilidades = predM,
                                                 datos_validacion = df.val)
# We return the breakpoints as a data.frame
      ore.sync(schema = esquema_recibos, table = tabla_prec_prob, use.keys = FALSE)
      cutoffs = ore.pull(ore.get(schema = esquema_recibos, name = tabla_prec_prob))
      cutoffs_list = as.data.frame(sapply(cutoffs, function(x) as.numeric(as.character(x))),
                                   stringAsFactors = FALSE)
    }
    # We return the results
    return(list(modelo, cutoffs_list))

功能 2:我们创建包含精度及其最小概率的表

tabla_precisiones = RECIBOS.PREC2PROB(nombre_proc_log = nombre_proc_log,
                                            nombre_tabla_logs = nombre_tabla_logs,
                                            nombre_secuencia = nombre_secuencia,
                                            var_obj = var_obj,
                                            campos_pk_incidencia = campos_pk_incidencia,
                                            tabla_enriquecida_validacion = tabla_enriquecida_validacion,
                                            precisiones_posibles = precisiones_posibles,
                                            tabla_prec_prob = tabla_prec_prob)

 # We return the breakpoints as a data.frame
      ore.sync(schema = esquema_recibos, table = tabla_prec_prob, use.keys = FALSE)
      cutoffs = ore.pull(ore.get(schema = esquema_recibos, name = tabla_prec_prob))
      cutoffs_list = as.data.frame(sapply(cutoffs, function(x) as.numeric(as.character(x))),
                                   stringAsFactors = FALSE)
    }
    # We return the results
    return(list(modelo, cutoffs_list))

非常感谢您的帮助

python r translate code-translation
1个回答
-1
投票

只要对目标语言有一点了解(在这种情况下是Python),您就可以充分利用chatgpt。

© www.soinside.com 2019 - 2024. All rights reserved.