1
我试图找到csv file.csv文件的单个提取列中包含大约45000个实例和17个属性的standard deviation(σ=√[(Σ(x-MEAN))2÷n]用';'进行操作。 为了找到标准偏差,它需要在每个迭代的while循环中使用Xi取代平均值。所以我认为MEAN需要循环迭代才能找到标准偏差。但我不知道如何做到这一点,或者有什么办法可以做到这一点。我在这里卡住了。那么我就用新习惯代替老习惯了。然后写入(生成)新的csv文件。在csv文件中找到标准偏差
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.io.*;
import static java.lang.Math.sqrt;
public class Main {
public static void main(String[] args) throws IOException {
String filename = "ly.csv";
File file = new File(filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("bank-full_updated.csv"));
}
catch (IOException e) {
}
try {
double Tuple,avg;
double temp;
Tuple = 0;
double stddev=0;
Scanner inputStream = new Scanner(file);
inputStream.next();
while (inputStream.hasNext()) {
String data1 = inputStream.next();
String[] values = data1.split(";");
double Xi = Double.parseDouble(values[1]);
//now finding standard deviation
temp1 += (Xi-MEAN);
// temp2=(temp1*temp1);
// temp3=(temp2/count);
// standard deviation=Math.sqrt(temp3);
Xi=standard deviation * Xi
//now replace new Xi to original values1
values[1] = String.valueOf(Xi);
// iterate through the values and build a string out of them for write a new file
StringBuilder sb = new StringBuilder();
String newData = sb.toString();
for (int i = 0; i < values.length; i++) {
sb.append(values[i]);
if (i < values.length - 1) {
sb.append(";");
}
}
// get the new string
System.out.println(sb.toString());
writer.write(sb.toString()+"\n");
}
writer.close();
inputStream.close();
}
catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是一个相对简单的数学问题,而不是一个规划问题。先用数学(你真正理解的方式)来计算数学。 (我很确定你提到的那个页面上的公式是错误的,顺便说一句)。 – 2015-02-07 04:31:30