英文:
calculating percentage of data in one standard deviation in Java?
问题
以下是您提供的代码的翻译部分:
// 计算已存在于目录中的文件中的数据的最大值、最小值、平均值、标准差以及处于一个标准差范围内的数据百分比,使用Java编写。如下所示,已经找到了最大值、最小值、平均值和标准差。如何在下面的代码中获取处于一个标准差范围内的数据百分比?
int count = 1;
int mean = 0;
int sum = 0;
int max = Integer.MIN_VALUE;
int mini = Integer.MAX_VALUE;
double std_deviation = 0;
double squaresum = 0;
double computationalsum = 0;
int percentage;
long lines = 0;
Scanner file = null;
int number = 0;
file_name = JOptionPane.showInputDialog("输入数据文件名:");
try {
file = new Scanner(new FileInputStream(file_name));
} catch (FileNotFoundException e) {
System.out.println("文件未找到");
}
max = file.nextInt();
mini = max;
while (file.hasNextInt()) {
number = file.nextInt();
squaresum += Math.pow(number, 2);
computationalsum += number;
if (number > max) {
max = number;
} else if (number < mini) {
mini = number;
}
sum += number;
count += 1;
}
file.close();
mean = sum / count;
// 在此处使用标准差的公式
double sumofsquares = squaresum - ((Math.pow(computationalsum, 2) / (count - 1)));
double ssquared = sumofsquares / (count - 1);
double otherstdev = Math.sqrt(ssquared);
double minrange = mean - otherstdev;
double maxrange = mean + otherstdev;
// 计算处于一个标准差范围内的数据百分比
int withinOneDeviation = 0;
file = new Scanner(new FileInputStream(file_name));
while (file.hasNextInt()) {
int num = file.nextInt();
double deviation = (num - mean) / otherstdev;
if (deviation > -1 && deviation < 1) {
withinOneDeviation++;
}
}
file.close();
double percentageWithinOneDeviation = (double) withinOneDeviation / count * 100.0;
System.out.println(count);
System.out.println(mini);
System.out.println(max);
System.out.println(mean);
System.out.println(otherstdev);
System.out.println(percentageWithinOneDeviation);
请注意,翻译仅涵盖了您提供的代码部分,没有包含其他内容。
英文:
I want to calculate the max, min, mean, standard deviation and the percentage of data within one standard deviation from a file already existing in my directory using Java. As you can see below, I have found max, min, mean and stand deviation. How can I get the percentages of data within one standard deviation in the following code?
int count = 1;
int mean = 0;
int sum = 0;
int max = Integer.MIN_VALUE;
int mini = Integer.MAX_VALUE;
double std_deviation = 0;
double squaresum=0;
double computationalsum=0;
int percentage;
long lines = 0;
Scanner file = null;
int number=0;
file_name = JOptionPane.showInputDialog("Enter the data file name:");
try {
file = new Scanner (new FileInputStream (file_name));
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
max = file.nextInt();
mini = max;
while (file.hasNextInt()) {
number = file.nextInt();
squaresum += Math.pow(number, 2);
computationalsum += number;
if (number > max) {
max = number;
}
else if (number < mini)
{
mini = number;
}
sum += number;
count +=1;
}
file.close();
mean = sum/count;
// use formula of standard deviation here
double sumofsquares = squaresum - ((Math.pow(computationalsum, 2)/(count-1)));
double ssquared = sumofsquares/(count-1);
double otherstdev = Math.sqrt(ssquared);
double minrange = mean - otherstdev;
double maxrange = mean + otherstdev;
// calculate the percentage of mean in one stedv
// while(scanner.hasNextInt()) { int = scanner.nextint() - mean / standard deviation; if(int > -1 && int < 1) { then: this number is within 1 standard deviation (ie oneDeviation++;}.......THEN you need to
// take that counter and divide it by the total number of numbers in the file
// }
// for (int i =0; i=> min)
System.out.println(count);
System.out.println(mini);
System.out.println(max);
System.out.println(mean);
System.out.println(otherstdev);
// System.out.println(newcount);
}
else
{
JOptionPane.showMessageDialog(null, "Exiting");
done = true;
}
}
while(!done);
}
}
答案1
得分: 1
简短回答,通过计数。
例如,您有以下数据集:
数据值 频率
10 17
11 7
12 23
13 4
14 6
15 12
16 21
使用计算器,您已经发现平均值约为13.06,标准偏差为2.24。这意味着一个标准偏差的范围是从13.06+2.24到13.06-2.24,即从15.3到10.8。您只需计算有多少数据点落在这两条线之间。
因此,您可以将数字存储在一个数组中,然后在最后可以遍历数组并检查区间。
英文:
Short answer, by counting.
For example you have the following dataset:
Data value Frequency
10 17
11 7
12 23
13 4
14 6
15 12
16 21
Using a calculator, you have found that the mean is about 13.06 and the standard deviation 2.24. That means that the range of one standard deviation is from 13.06+2.24 to 13.06-2.24 or 15.3 to 10.8. You can simply count how many data points fall between these two lines.
So you must store the numbers in an array for example. And at the end you can iterate over the array and check the interval.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论