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