gridsearch.java
来自「Weka」· Java 代码 · 共 2,258 行 · 第 1/5 页
JAVA
2,258 行
m_MaxX = maxX; m_StepX = stepX; m_LabelX = labelX; m_MinY = minY; m_MaxY = maxY; m_StepY = stepY; m_LabelY = labelY; m_Height = (int) StrictMath.round((m_MaxY - m_MinY) / m_StepY) + 1; m_Width = (int) StrictMath.round((m_MaxX - m_MinX) / m_StepX) + 1; // is min < max? if (m_MinX >= m_MaxX) throw new IllegalArgumentException("XMin must be smaller than XMax!"); if (m_MinY >= m_MaxY) throw new IllegalArgumentException("YMin must be smaller than YMax!"); // steps positive? if (m_StepX <= 0) throw new IllegalArgumentException("XStep must be a positive number!"); if (m_StepY <= 0) throw new IllegalArgumentException("YStep must be a positive number!"); // check borders if (!Utils.eq(m_MinX + (m_Width-1)*m_StepX, m_MaxX)) throw new IllegalArgumentException( "X axis doesn't match! Provided max: " + m_MaxX + ", calculated max via min and step size: " + (m_MinX + (m_Width-1)*m_StepX)); if (!Utils.eq(m_MinY + (m_Height-1)*m_StepY, m_MaxY)) throw new IllegalArgumentException( "Y axis doesn't match! Provided max: " + m_MaxY + ", calculated max via min and step size: " + (m_MinY + (m_Height-1)*m_StepY)); } /** * Tests itself against the provided grid object * * @param o the grid object to compare against * @return if the two grids have the same setup */ public boolean equals(Object o) { boolean result; Grid g; g = (Grid) o; result = (width() == g.width()) && (height() == g.height()) && (getMinX() == g.getMinX()) && (getMinY() == g.getMinY()) && (getStepX() == g.getStepX()) && (getStepY() == g.getStepY()) && getLabelX().equals(g.getLabelX()) && getLabelY().equals(g.getLabelY()); return result; } /** * returns the left border * * @return the left border */ public double getMinX() { return m_MinX; } /** * returns the right border * * @return the right border */ public double getMaxX() { return m_MaxX; } /** * returns the step size on the X axis * * @return the step size */ public double getStepX() { return m_StepX; } /** * returns the label for the X axis * * @return the label */ public String getLabelX() { return m_LabelX; } /** * returns the bottom border * * @return the bottom border */ public double getMinY() { return m_MinY; } /** * returns the top border * * @return the top border */ public double getMaxY() { return m_MaxY; } /** * returns the step size on the Y axis * * @return the step size */ public double getStepY() { return m_StepY; } /** * returns the label for the Y axis * * @return the label */ public String getLabelY() { return m_LabelY; } /** * returns the number of points in the grid on the Y axis (incl. borders) * * @return the number of points in the grid on the Y axis */ public int height() { return m_Height; } /** * returns the number of points in the grid on the X axis (incl. borders) * * @return the number of points in the grid on the X axis */ public int width() { return m_Width; } /** * returns the values at the given point in the grid * * @param x the x-th point on the X axis * @param y the y-th point on the Y axis * @return the value pair at the given position */ public PointDouble getValues(int x, int y) { if (x >= width()) throw new IllegalArgumentException("Index out of scope on X axis (" + x + " >= " + width() + ")!"); if (y >= height()) throw new IllegalArgumentException("Index out of scope on Y axis (" + y + " >= " + height() + ")!"); return new PointDouble(m_MinX + m_StepX*x, m_MinY + m_StepY*y); } /** * returns the closest index pair for the given value pair in the grid. * * @param values the values to get the indices for * @return the closest indices in the grid */ public PointInt getLocation(PointDouble values) { PointInt result; int x; int y; double distance; double currDistance; int i; // determine x x = 0; distance = m_StepX; for (i = 0; i < width(); i++) { currDistance = StrictMath.abs(values.getX() - getValues(i, 0).getX()); if (Utils.sm(currDistance, distance)) { distance = currDistance; x = i; } } // determine y y = 0; distance = m_StepY; for (i = 0; i < height(); i++) { currDistance = StrictMath.abs(values.getY() - getValues(0, i).getY()); if (Utils.sm(currDistance, distance)) { distance = currDistance; y = i; } } result = new PointInt(x, y); return result; } /** * checks whether the given values are on the border of the grid * * @param values the values to check * @return true if the the values are on the border */ public boolean isOnBorder(PointDouble values) { return isOnBorder(getLocation(values)); } /** * checks whether the given location is on the border of the grid * * @param location the location to check * @return true if the the location is on the border */ public boolean isOnBorder(PointInt location) { if (location.getX() == 0) return true; else if (location.getX() == width() - 1) return true; if (location.getY() == 0) return true; else if (location.getY() == height() - 1) return true; else return false; } /** * returns a subgrid with the same step sizes, but different borders * * @param top the top index * @param left the left index * @param bottom the bottom index * @param right the right index * @return the Sub-Grid */ public Grid subgrid(int top, int left, int bottom, int right) { return new Grid( getValues(left, top).getX(), getValues(right, top).getX(), getStepX(), getLabelX(), getValues(left, bottom).getY(), getValues(left, top).getY(), getStepY(), getLabelY()); } /** * returns an extended grid that encompasses the given point (won't be on * the border of the grid). * * @param values the point that the grid should contain * @return the extended grid */ public Grid extend(PointDouble values) { double minX; double maxX; double minY; double maxY; double distance; Grid result; // left if (Utils.smOrEq(values.getX(), getMinX())) { distance = getMinX() - values.getX(); // exactly on grid point? if (Utils.eq(distance, 0)) minX = getMinX() - getStepX() * (StrictMath.round(distance / getStepX()) + 1); else minX = getMinX() - getStepX() * (StrictMath.round(distance / getStepX())); } else { minX = getMinX(); } // right if (Utils.grOrEq(values.getX(), getMaxX())) { distance = values.getX() - getMaxX(); // exactly on grid point? if (Utils.eq(distance, 0)) maxX = getMaxX() + getStepX() * (StrictMath.round(distance / getStepX()) + 1); else maxX = getMaxX() + getStepX() * (StrictMath.round(distance / getStepX())); } else { maxX = getMaxX(); } // bottom if (Utils.smOrEq(values.getY(), getMinY())) { distance = getMinY() - values.getY(); // exactly on grid point? if (Utils.eq(distance, 0)) minY = getMinY() - getStepY() * (StrictMath.round(distance / getStepY()) + 1); else minY = getMinY() - getStepY() * (StrictMath.round(distance / getStepY())); } else { minY = getMinY(); } // top if (Utils.grOrEq(values.getY(), getMaxY())) { distance = values.getY() - getMaxY(); // exactly on grid point? if (Utils.eq(distance, 0)) maxY = getMaxY() + getStepY() * (StrictMath.round(distance / getStepY()) + 1); else maxY = getMaxY() + getStepY() * (StrictMath.round(distance / getStepY())); } else { maxY = getMaxY(); } result = new Grid(minX, maxX, getStepX(), getLabelX(), minY, maxY, getStepY(), getLabelY()); // did the grid really extend? if (equals(result)) throw new IllegalStateException("Grid extension failed!"); return result; } /** * returns an Enumeration over all pairs in the given row * * @param y the row to retrieve * @return an Enumeration over all pairs * @see #getValues(int, int) */ public Enumeration<PointDouble> row(int y) { Vector result; int i; result = new Vector(); for (i = 0; i < width(); i++) result.add(getValues(i, y)); return result.elements(); } /** * returns an Enumeration over all pairs in the given column * * @param x the column to retrieve * @return an Enumeration over all pairs * @see #getValues(int, int) */ public Enumeration<PointDouble> column(int x) { Vector result; int i; result = new Vector(); for (i = 0; i < height(); i++) result.add(getValues(x, i)); return result.elements(); } /** * returns a string representation of the grid * * @return a string representation */ public String toString() { String result; result = "X: " + m_MinX + " - " + m_MaxX + ", Step " + m_StepX; if (m_LabelX.length() != 0) result += " (" + m_LabelX + ")"; result += "\n"; result += "Y: " + m_MinY + " - " + m_MaxY + ", Step " + m_StepY; if (m_LabelY.length() != 0) result += " (" + m_LabelY + ")"; result += "\n"; result += "Dimensions (Rows x Columns): " + height() + " x " + width(); return result; } } /** * A helper class for storing the performance of a values-pair. * Can be sorted with the PerformanceComparator class. * * @see PerformanceComparator */ protected class Performance implements Serializable { /** for serialization */ private static final long serialVersionUID = -4374706475277588755L; /** the value pair the classifier was built with */ protected PointDouble m_Values; /** the Correlation coefficient */ protected double m_CC; /** the Root mean squared error */ protected double m_RMSE; /** the Root relative squared error */ protected double m_RRSE; /** the Mean absolute error */ protected double m_MAE; /** the Relative absolute error */ protected double m_RAE; /** the Accuracy */ protected double m_ACC; /** * initializes the performance container * * @param values the values-pair * @param evaluation the evaluation to extract the performance * measures from * @throws Exception if retrieving of measures fails */ public Performance(PointDouble values, Evaluation evaluation) throws Exception { super(); m_Values = values; m_RMSE = evaluation.rootMeanSquaredError(); m_RRSE = evaluation.rootRelativeSquaredError(); m_MAE = evaluation.meanAbsoluteError(); m_RAE = evaluation.relativeAbsoluteError(); try { m_CC = evaluation.correlationCoefficient(); } catch (Exception e) { m_CC = Double.NaN; } try { m_ACC = evaluation.pctCorrect(); } catch (Exception e) { m_ACC = Double.NaN; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?