⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functioncharacteristicagenerator.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	for (int x=attributeIndexStart; x <= attributeIndexEnd; x++)
	    y[x - attributeIndexStart] = data.get(getExampleTable().getAttribute(x));

	// ----- compute global maximum (Max), first turning point (WP1), and second turning point (WP2) -----
	// ----- of the value series 'y' and write result into 'functionCharacteristica'                 -----
	computeFunctionCharacteristica (y, functionCharacteristica);

	// ----- return the resulting, newly generated six x- and y-values -----
	for (int i = 0; i < functionCharacteristica.length; i++) {
	    if (resultAttributes[i] != null)
		data.set(resultAttributes[i], functionCharacteristica[i]);
	}
    }


    /* --------- Private Methods ---------- */

    /** Computes the function characteristica global maximum (Max.x, Max.y),
     *  first turning point (WP1.x, WP1.y), and second turning point (WP2.x, WP2.y)
     *  of the given value series <tt>y</tt> (e.g. time series or function value series, where
     *  is always assumed to start with 0 and be incremented by 1 for each y-value) and
     *  writes these values into <tt>functionCharacteristica</tt>. <br>
     *  How this version works:
     *  <ol><li>Find global maximum (Max.x, Max.y);</li>
     *      <li>Find WP1 in range 0 .. Max.x-1</li>
     *      <li>Find WP2 in range Max.x+1 .. x_max</li>
     *  </ol>
     */
    private void  computeFunctionCharacteristica (double[] y, double[] functionCharacteristica) {
	int  maxX;    // index (position) of global maximum

	// find attributes values of Max.x, Max.Y (i.e. set variables in 'functionCharacteristica'):
	findGlobalMaximum (y, functionCharacteristica);
	maxX = ((int)(functionCharacteristica[MAX_X]+0.5));

        // find attributes values of WP1.x, WP1.y (i.e. set variables in 'functionCharacteristica'):
	findWP1 (y, 0, maxX-1, functionCharacteristica);

        // find attributes values of WP2.x, WP2.y (i.e. set variables in 'functionCharacteristica'):
	findWP2 (y, maxX+1, y.length-1, functionCharacteristica);
    }


    /** finds the function characteristic global maximum (Max.x, Max.y)
     *  of the given value series <tt>y</tt> and writes it into <tt>functionCharacteristica[MAX_X]</tt>
     *  and <tt>functionCharacteristica[MAX_Y]</tt>.
     *  This method is called by the method <code>computeFunctionCharacteristica(...)</code>.
     */
    private void  findGlobalMaximum (double[] y, double[] functionCharacteristica) {
	// int       x;     // x-value of time series (= old attribute index)
	// double[]  y;     // y-values of time series (= old attribute values)
	int     maxX;       // x-value of the global maximum
	double  maxY;       // y-value of the global maximum

	maxX = 0;   maxY = y[0];
	for (int x = 0;  x < y.length;  x++) {
	    if (y[x] > maxY) {
		maxX = x;  maxY = y[x];
	    }
	}
	if (maxX == 0) {
	    LogService.logMessage("FunctionCharacteristicaGenerator.findGlobalMaximum(): "
				  +"maximum illegaly is first value of the value series", LogService.WARNING);

	    /* try {
		java.io.PrintWriter out = 
		    new java.io.PrintWriter(new java.io.FileWriter("error.ex"));
		for (int i = 0; i < y.length; i++) out.println(y[i]);
		out.close();
	    } catch (Exception e) { e.printStackTrace(); }*/

	    // Problem: generator has to search WP1 left of maximum.
	} else if (maxX == y.length - 1) {
	    LogService.logMessage("FunctionCharacteristicaGenerator.findGlobalMaximum(): "
				  +"maximum illegaly is last value of the value series", LogService.WARNING);
	    // Problem: generator has to search WP2 right of maximum.
	}
	functionCharacteristica[MAX_X] = maxX;
	functionCharacteristica[MAX_Y] = maxY;
    }


    /** finds the function characteristic first turning point (WP1.x, WP1.y)
     *  of the given value series <tt>y</tt> and writes it into <tt>functionCharacteristica[WP1_X]</tt>
     *  and <tt>functionCharacteristica[WP1_Y]</tt>.
     *  This method is called by the method <code>computeFunctionCharacteristica(...)</code>.<br><br>
     *  [Wendepunkt 1. Art = von Links- in Rechtskr&uuml;mmung] <br>
     *  Wendepunkte nicht als L&ouml;sung analytischer Constraints (2. Ableitung = 0, 3. Ableitung != 0), die
     *  selbst bei kleinen Datenfehlern oder leichtem Rauschen sehr schwierig korrekt zu bestimmen ist,
     *  sondern als L&ouml;sung eines Optimierungsproblems, das robuster und leichter l&ouml;sbar ist.
     */
    private void  findWP1 (double[] y, int startIndex, int stopIndex, double[] functionCharacteristica) {
	int       x;                  // x-value of time series (= old attribute index)
	// double[]  y;               // y-values of time series (= old attribute values)
	double[]  slope_difference;   // differences of the slopes of all consecutive pairs of slopes in
	                              //   the time series, where each slope is computed from two consecutive
	                              //   points in the time series
	double    sum_of_all_slopes;  // sum of all slopes in the interval from startIndex to stopIndex
	double    sum_of_left_side;   // sum of the slopes left of current time series point
	double    d;                  // tmp variable (current slope difference, current optimization term value)
	double    d_max;              // maximum optimization term value found so far
	int       wp1X;               // x-value of the first turning point (WP1.x)
	double    wp1Y;               // y-value of the first turning point (WP1.y)

	// WP1 := point P, maximizing the following term:
	//        sum{i=startIndex..P}{slope(x_i)-slope(x_i-1)} - sum{i=P..stopIndex}{slope(x_i)-slope(x_i-1)}

        // check parameters
	if ((startIndex < 0) || (startIndex >= stopIndex-2) || (stopIndex > y.length-1)) {
	    LogService.logMessage("FunctionCharacteristicaGenerator.findWP1(...): "
				  +"illegal index combination (startIndex="+startIndex+", stopIndex="
				  +stopIndex+", y.length="+y.length+")", LogService.WARNING);
	    return;
	}
	// compute slope differences:  slope      := slope from y[x] to y[x+1]
	//                             last_slope := slope from y[x-1] to y[x]
	//                             slope_differenc := slope - last_slope = (y[x+1] - y[x]) - (y[x] - y[x-1])
	//                                                                   = y[x+1] - y[x-1]
	slope_difference = new double[stopIndex-startIndex-1];
	sum_of_all_slopes = 0.0;
	for (x = startIndex+1;  x < stopIndex;  x++) {
	    d = y[x+1] - y[x-1];
	    slope_difference[x-startIndex-1] = d;
	    sum_of_all_slopes += d;
	}
	// find point with maximal term value (= WP1)
	wp1X = startIndex+1;   wp1Y = y[startIndex+1];
	sum_of_left_side = 0.0;
	d_max = -sum_of_all_slopes;
	for (x = startIndex+1;  x < stopIndex-1;  x++) {
	    sum_of_left_side = slope_difference[x-startIndex-1];
	    d = sum_of_left_side - (sum_of_all_slopes - sum_of_left_side);
	    if (d > d_max) {
		d_max = d;
		wp1X = x;   wp1Y = y[x];
	    }
	}
	functionCharacteristica[WP1_X] = wp1X;
	functionCharacteristica[WP1_Y] = wp1Y;
    }


    /** finds the function characteristic second turning point (WP2.x, WP2.y)
     *  of the given value series <tt>y</tt> and writes it into <tt>functionCharacteristica[WP1_X]</tt>
     *  and <tt>functionCharacteristica[WP1_Y]</tt>.
     *  This method is called by the method <code>computeFunctionCharacteristica(...)</code>.<br><br>
     *  [Wendepunkt 2. Art = von Rechts- in Linkskr&uuml;mmung] <br>
     *  Wendepunkte nicht als L&ouml;sung analytischer Constraints (2. Ableitung = 0, 3. Ableitung != 0), die
     *  selbst bei kleinen Datenfehlern oder leichtem Rauschen sehr schwierig korrekt zu bestimmen ist,
     *  sondern als L&ouml;sung eines Optimierungsproblems, das robuster und leichter l&ouml;sbar ist.
     */
    private void  findWP2 (double[] y, int startIndex, int stopIndex, double[] functionCharacteristica) {
	int       x;                  // x-value of time series (= old attribute index)
	// double[]  y;               // y-values of time series (= old attribute values)
	double[]  slope_difference;   // differences of the slopes of all consecutive pairs of slopes in
	                              //   the time series, where each slope is computed from two consecutive
	                              //   points in the time series
	double    sum_of_all_slopes;  // sum of all slopes in the interval from startIndex to stopIndex
	double    sum_of_left_side;   // sum of the slopes left of current time series point
	double    d;                  // tmp variable (current slope difference, current optimization term value)
	double    d_min;              // minimum optimization term value found so far
	int       wp2X;               // x-value of the second turning point (WP2.x)
	double    wp2Y;               // y-value of the second turning point (WP2.y)

	// WP2 := point P, minimizing the following term:
	//        sum{i=startIndex..P}{slope(x_i)-slope(x_i-1)} - sum{i=P..stopIndex}{slope(x_i)-slope(x_i-1)}

        // check parameters
	if ((startIndex < 0) || (startIndex >= stopIndex-2) || (stopIndex > y.length-1)) {
	    LogService.logMessage("FunctionCharacteristicaGenerator.findWP2(...): "
				  +"illegal index combination (startIndex="+startIndex+", stopIndex="
				  +stopIndex+", y.length="+y.length+")", LogService.WARNING);
	    return;
	}
	// compute slope differences:  slope      := slope from y[x] to y[x+1]
	//                             last_slope := slope from y[x-1] to y[x]
	//                             slope_differenc := slope - last_slope = (y[x+1] - y[x]) - (y[x] - y[x-1])
	//                                                                   = y[x+1] - y[x-1]
	slope_difference = new double[stopIndex-startIndex-1];
	sum_of_all_slopes = 0.0;
	for (x = startIndex+1;  x < stopIndex;  x++) {
	    d = y[x+1] - y[x-1];
	    slope_difference[x-startIndex-1] = d;
	    sum_of_all_slopes += d;
	}
	// find point with minimal term value (= WP2)
	wp2X = startIndex+1;   wp2Y = y[startIndex+1];
	sum_of_left_side = 0.0;
	d_min = -sum_of_all_slopes;
	for (x = startIndex+1;  x < stopIndex-1;  x++) {
	    sum_of_left_side = slope_difference[x-startIndex-1];
	    d = sum_of_left_side - (sum_of_all_slopes - sum_of_left_side);
	    if (d < d_min) {
		d_min = d;
		wp2X = x;   wp2Y = y[x];
	    }
	}
	functionCharacteristica[WP2_X] = wp2X;
	functionCharacteristica[WP2_Y] = wp2Y;
    }

    public String toString() {
	String s = "Function characteristica generator";
	if (argumentsSet() && (getExampleTable() != null))
	    s += " (#" + getArgument(0).getIndex() + " - #" + 
		getExampleTable().getBlockEndIndex(getArgument(0).getIndex()) + ")";
	return s;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -