/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package dm; import java.text.AttributedCharacterIterator.Attribute; import dm.TEST_DM; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import weka.core.matrix.*; import static java.lang.Math.*; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author student */ public class Houses { static int recCount; static int atrrCount; public static void main(String[] args) throws Exception { TEST_DM houses = new TEST_DM("C:/Documents and Settings/Kasia/Pulpit/semestr 7/analiza danych/houses.arff"); atrrCount = houses.dane.numAttributes(); recCount = houses.dane.numInstances(); houses.dane.setClassIndex(0); //zapisac w formacie xml lub w podobnym analiza(houses); } private static void normalizuj(double[][] Z) { double[] sr = new double[Z[0].length]; double[] var = new double[Z[0].length]; for (int i = 0; i < Z.length; i++) { for (int j = 0; j < Z[0].length; j++) { sr[j] = sr[j] + Z[i][j]; } } for (int i = 0; i < Z[0].length; i++) { sr[i] = sr[i] / Z.length; } for (int i = 0; i < Z.length; i++) { for (int j = 0; j < Z[0].length; j++) { Z[i][j] = Z[i][j] - sr[j]; var[j] = var[j] + (Z[i][j]) * (Z[i][j]); } } for (int i = 0; i < Z[0].length; i++) { var[i] = Math.sqrt(var[i] / Z.length); } for (int i = 0; i < Z.length; i++) { for (int j = 0; j < Z[0].length; j++) { Z[i][j] = Z[i][j] / var[j]; } } } public static void analiza(TEST_DM houses) throws IOException { double[][] Z = new double[recCount][atrrCount - 1]; double[] sr = new double[atrrCount - 1]; double[] var = new double[atrrCount - 1]; for (int i = 0; i < recCount; i++) { for (int j = 1; j < atrrCount; j++) { Z[i][j - 1] = houses.element(i, j); } } normalizuj(Z); Matrix zM = new Matrix(Z); //System.out.println(new Matrix (Z)); Matrix cov = zM.transpose().times(zM).times(1. / recCount); System.out.println(cov); EigenvalueDecomposition w?asne = cov.eig(); double[] w = w?asne.getRealEigenvalues(); double suma = 0; System.out.println("wartosci w?asne:"); for (int i = 0; i < w.length; i++) { // System.out.println(w[i]); suma = suma + w[i]; System.out.printf("%d:_%5.3f=%4.1f proc\n", i, w[i], w[i] / atrrCount * 100); } System.out.println("suma warto?ci w?asnych wynosi: "); System.out.println(suma); Matrix v = w?asne.getV(); System.out.println(v); for (int i = 0; i < atrrCount - 1; i++) { System.out.printf("%7.3f %7.3f %7.3f %7.3f\n", v.get(i, 7), v.get(i, 6), v.get(i, 5), v.get(i, 4)); } System.out.println(v.transpose().times(v)); Matrix zMv = zM.times(v); double[][] wlasne = new double[4][8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 4; j++) { if (abs(v.get(i, 7 - j)) > 0.4) { wlasne[j][i] = v.get(i, 7 - j); } } } Matrix wektory = new Matrix(wlasne); System.out.println(wektory); double[][] X = new double[recCount][8]; for (int i = 0; i < recCount; i++) { for (int j = 1; j < atrrCount; j++) { X[i][j - 1] = houses.element(i, j); } } Matrix dane = new Matrix(X); Matrix wektoryDane = (wektory.times(dane.transpose())).transpose(); //System.out.println(wektoryDane); double[][] zet = new double[recCount][5]; for (int i = 0; i < recCount; i++) { for (int j = 0; j < 4; j++) { zet[i][0] = 1; zet[i][j + 1] = wektoryDane.get(i, j); } } Matrix M = new Matrix(zet); //System.out.println(M); double[][] igrek = new double[recCount][1]; for (int i = 0; i < recCount; i++) { igrek[i][0] = houses.element(i, 0); } Matrix Y = new Matrix(igrek); System.out.println(Y); Matrix MTM = (M.transpose().times(M)).inverse(); System.out.println(MTM); Matrix A = (MTM.times(M.transpose())).times(Y); System.out.println(A); //Wykres wykres= new Wykres(w); //wykres.show(); // System.out.println(zM.times(v)); // PrintWriter pw = new PrintWriter(new FileWriter("houses.csv")); // for (int i = 0; i < recCount; i++) { // pw.printf("%d;%5.3f;%5.3f\n", i, zMv.get(i, 2), zMv.get(i, 3)); // // } // pw.close(); } }
aivliska