kettle 通过java实现正态分布密度函数调用

42 篇文章 6 订阅

kettle版本

kettle 5.4

需求描述

excel中可以通过正态分布函数NORMDIST生成给定值的正态分布值,如下所示:
在这里插入图片描述
本文讲解如何通过kettle实现excel的NORMDIST函数。

实现效果

在这里插入图片描述

实现方式

1、生成记录

模拟数据输入
在这里插入图片描述

2、Java代码

使用注意:
如果直接将计算的double值输出,kettle默认会截断为小数点后1位,所以这里输出为字符串,保留小数点后5位。
在这里插入图片描述
详细代码:

import org.apache.commons.math3.distribution.NormalDistribution;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{
	// Let's look up parameters only once for performance reason.
	//
	if (first) {
	  first=false;
	}

	// First, get a row from the default input hop
	//
	Object[] r = getRow();

	// If the row object is null, we are done processing.
	//
	if (r == null) {
	  setOutputDone();
	  return false;
	}

	// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
	// enough to handle any new fields you are creating in this step.
	//
	Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());

	double x_in = get(Fields.In, "x_in").getNumber(r);
	double mean_in = get(Fields.In, "mean_in").getNumber(r);
	double standard_dev_in = get(Fields.In, "standard_dev_in").getNumber(r);

	// Set the value in the output field
	//
	Number nd = getNd(x_in, mean_in, standard_dev_in) ;
	logBasic("nd:"+nd);
//	get(Fields.Out, "nd").setValue(outputRow, nd);
	get(Fields.Out, "nd").setValue(outputRow, String.format("%.5f",nd));

	// putRow will send the row on to the default output hop.
	//
	putRow(data.outputRowMeta, outputRow);

	return true;
}

/**
 * 计算正态分布值
 * 
 * <pre>
 * 方法同excel NORMDIST 函数
 * </pre>
 * 
 * @param x
 *            需要计算其分布的数值
 * @param mean
 *            分布的算术平均值
 * @param standard_dev
 *            标准偏差
 * @return 正态分布值
 */
public static double getNd(double x, double mean, double standard_dev) {
	return new NormalDistribution(mean, standard_dev).density(x);
}

3、写日志

内容保留为空即可,默认输出全部变量
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值