「java算标准差」数据标准差怎么计算
今天给各位分享java算标准差的知识,其中也会对数据标准差怎么计算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
在Java中怎么求方差和标准差
Java求方差和标准差:
public class GetAverageandStandardDevition {
private int[] array = new int[10];
private int num = 10;
public int getRandomDigit() {
return (int) (Math.random() * 1000);
}
public void getTargetDigit() {
for (int i = 0; i num; i++) {
array[i] = getRandomDigit();
System.out.println(array[i]);
}
}
//方差
public double getAverage(){
int sum = 0;
for(int i = 0;i num;i++){
sum += array[i];
}
return (double)(sum / num);
}
//标准差
public double getStandardDevition(){
double sum = 0;
for(int i = 0;i num;i++){
sum += Math.sqrt(((double)array[i] -getAverage()) * (array[i] -getAverage()));
}
return (sum / (num - 1));
}
public static void main(String[] args) {
GetAverageandStandardDevition gcs = new GetAverageandStandardDevition();
gcs.getTargetDigit();
System.out.println(gcs.getAverage() + " " + gcs.getStandardDevition());
}
如何用java程序导入表格数据,并且求出每列的标准差,求代码。越详细越好。
jar包下载地址:
下载完把rar改为jar,参考别人的,呵呵
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadExcel {
public static void main(String[] args) throws Exception {
File file = new File("src/test.xls");
String[][] result = getData(file, 0);
getStd(result);
}
public static double[] getStd(String[][] result) {
int rowLength = result.length;
int colLength = result[0].length;
int[][] marx = new int[rowLength][colLength - 1];
// 将字符串数组转换为double数组,注:字符串本身要为数字哦
for (int i = 0; i rowLength; i++) {
for (int j = 0; j colLength - 1; j++) {
marx[i][j] = new Integer(result[i][j]);
}
}
double[] sum = new double[marx[0].length];
double[] avg = new double[marx[0].length];
double[] std = new double[marx[0].length];
double temp = 0;
for (int j = 0; j marx[0].length; j++) {
for (int i = 0; i marx.length; i++) {
sum[j] += marx[i][j];
avg[j] = sum[j] / marx.length;
temp = marx[i][j] - avg[j];
temp = Math.pow(temp, 2) / marx.length;
std[j] = Math.pow(temp, 0.5);
}
}
System.out.println("表格数据:"+rowLength+" 行, "+(colLength-1)+" 列");
System.out.println("计算结果为");
System.out.println("sum = ");
printArray(sum);
System.out.println("\navg = ");
printArray(avg);
System.out.println("\nstd = ");
printArray(std);
return std;
}
/**
* 输出
*/
public static void printArray(double[] a) {
for (double i : a) {
System.out.print(i + " ");
}
}
/**
*
* 读取Excel的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行
*
* @param file
* 读取数据的源Excel
* @param ignoreRows
* 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1
* @return 读出的Excel中数据的内容
* @throws FileNotFoundException
* @throws IOException
*/
public static String[][] getData(File file, int ignoreRows)
throws FileNotFoundException, IOException {
ListString[] result = new ArrayListString[]();
int rowSize = 0;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(
file));
// 打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFCell cell = null;
for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex++) {
HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex = st.getLastRowNum(); rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
if (row == null) {
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize rowSize) {
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
boolean hasValue = false;
for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex++) {
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) {
// 注意:一定要设成这个,否则可能会出现乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
if (columnIndex == 0 value.trim().equals("")) {
break;
}
values[columnIndex] = rightTrim(value);
hasValue = true;
}
if (hasValue) {
result.add(values);
}
}
}
in.close();
String[][] returnArray = new String[result.size()][rowSize];
for (int i = 0; i returnArray.length; i++) {
returnArray[i] = (String[]) result.get(i);
}
return returnArray;
}
/**
*
* 去掉字符串右边的空格
*
* @param str
* 要处理的字符串
* @return 处理后的字符串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i = 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
}
计算数字的标准差的java编程是什么?
获取平均值,然后才可以获取标准差
public class GetAverageandStandardDevition {
private int[] array = new int[10];
private int num = 10;
public int getRandomDigit() {
return (int) (Math.random() * 1000);
}
public void getTargetDigit() {
for (int i = 0; i num; i++) {
array[i] = getRandomDigit();
System.out.println(array[i]);
}
}
//获取平均值
public double getAverage(){
int sum = 0;
for(int i = 0;i num;i++){
sum += array[i];
}
return (double)(sum / num);
}
//标准差
public double getStandardDevition(){
double sum = 0;
for(int i = 0;i num;i++){
sum += Math.sqrt(((double)array[i] -getAverage()) * (array[i] -getAverage()));
}
return (sum / (num - 1));
}
public static void main(String[] args) {
GetAverageandStandardDevition gcs = new GetAverageandStandardDevition();
gcs.getTargetDigit();
System.out.println(gcs.getAverage() + " " + gcs.getStandardDevition());
}
拓展
Java编程
Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
用JAVA计算学生成绩及标准差
不知道你什么意思 但是给你个思路写一个学生bean 里面是对你所说的那些数据的get和set方法然后写一个类 里面一个静态方法或者普通方法返回是一个集合是对你指定路径的一个文本内容的提取然后在提取的时候用写好的bean进行封装装入集合当中返回出来 在输出 整个过程就是这样的希望能帮助到你
关于java算标准差和数据标准差怎么计算的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。