// Stopping criteria boolean isDone(KMeansModel model, double[][] newCenters, double[][] oldCenters) { if (!isRunning()) return true; // Stopped/cancelled // Stopped for running out iterations if (model._output._iterations >= _parms._max_iterations) return true; // Compute average change in standardized cluster centers if (oldCenters == null) return false; // No prior iteration, not stopping double average_change = 0; for (int clu = 0; clu < _parms._k; clu++) average_change += hex.genmodel.GenModel.KMeans_distance( oldCenters[clu], newCenters[clu], _isCats, null, null); average_change /= _parms._k; // Average change per cluster model._output._avg_centroids_chg = ArrayUtils.copyAndFillOf( model._output._avg_centroids_chg, model._output._avg_centroids_chg.length + 1, average_change); model._output._training_time_ms = ArrayUtils.copyAndFillOf( model._output._training_time_ms, model._output._training_time_ms.length + 1, System.currentTimeMillis()); return average_change < TOLERANCE; }
private static void handleExportAttributesFromJAR(Element e, File config, File toDir) { for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) { Attribute a = c.getAttribute("EXPORT"); if (a != null && a.getValue().equals("copy")) { /* Copy file from JAR */ File file = GUI.restoreConfigRelativePath(config, new File(c.getText())); InputStream inputStream = GUI.class.getResourceAsStream("/" + file.getName()); if (inputStream == null) { throw new RuntimeException("Could not unpack file: " + file); } byte[] fileData = ArrayUtils.readFromStream(inputStream); if (fileData == null) { logger.info("Failed unpacking file"); throw new RuntimeException("Could not unpack file: " + file); } if (OVERWRITE || !file.exists()) { boolean ok = ArrayUtils.writeToFile(file, fileData); if (!ok) { throw new RuntimeException("Failed unpacking file: " + file); } logger.info("Unpacked file from JAR: " + file.getName()); } else if (OVERWRITE) { logger.info("Skip: unpack file from JAR: " + file.getName()); } } /* Recursive search */ handleExportAttributesFromJAR(c, config, toDir); } }
/** * Compute Variable Importance, based on GEDEON: DATA MINING OF INPUTS: ANALYSING MAGNITUDE AND * FUNCTIONAL MEASURES * * @return variable importances for input features */ public float[] computeVariableImportances() { float[] vi = new float[units[0]]; Arrays.fill(vi, 0f); float[][] Qik = new float[units[0]][units[2]]; // importance of input i on output k float[] sum_wj = new float[units[1]]; // sum of incoming weights into first hidden layer float[] sum_wk = new float[units[2]]; // sum of incoming weights into output layer (or second hidden layer) for (float[] Qi : Qik) Arrays.fill(Qi, 0f); Arrays.fill(sum_wj, 0f); Arrays.fill(sum_wk, 0f); // compute sum of absolute incoming weights for (int j = 0; j < units[1]; j++) { for (int i = 0; i < units[0]; i++) { float wij = get_weights(0).get(j, i); sum_wj[j] += Math.abs(wij); } } for (int k = 0; k < units[2]; k++) { for (int j = 0; j < units[1]; j++) { float wjk = get_weights(1).get(k, j); sum_wk[k] += Math.abs(wjk); } } // compute importance of input i on output k as product of connecting weights going through j for (int i = 0; i < units[0]; i++) { for (int k = 0; k < units[2]; k++) { for (int j = 0; j < units[1]; j++) { float wij = get_weights(0).get(j, i); float wjk = get_weights(1).get(k, j); // Qik[i][k] += Math.abs(wij)/sum_wj[j] * wjk; //Wong,Gedeon,Taggart '95 Qik[i][k] += Math.abs(wij) / sum_wj[j] * Math.abs(wjk) / sum_wk[k]; // Gedeon '97 } } } // normalize Qik over all outputs k for (int k = 0; k < units[2]; k++) { float sumQk = 0; for (int i = 0; i < units[0]; i++) sumQk += Qik[i][k]; for (int i = 0; i < units[0]; i++) Qik[i][k] /= sumQk; } // importance for feature i is the sum over k of i->k importances for (int i = 0; i < units[0]; i++) vi[i] = ArrayUtils.sum(Qik[i]); // normalize importances such that max(vi) = 1 ArrayUtils.div(vi, ArrayUtils.maxValue(vi)); // zero out missing categorical variables if they were never seen if (_saw_missing_cats != null) { for (int i = 0; i < _saw_missing_cats.length; ++i) { assert (data_info._catMissing[i] == 1); // have a missing bucket for each categorical if (!_saw_missing_cats[i]) vi[data_info._catOffsets[i + 1] - 1] = 0; } } return vi; }
/** * 访问服务器环境 * * @param names 参数名字 * @param values 参数值 */ public static void visitEnvServerByParameters(String[] names, String[] values) { int len = Math.min(ArrayUtils.getLength(names), ArrayUtils.getLength(values)); String[] segs = new String[len]; for (int i = 0; i < len; i++) { try { // 设计器里面据说为了改什么界面统一, 把分隔符统一用File.separator, 意味着在windows里面报表路径变成了\ // 以前的超链, 以及预览url什么的都是/, 产品组的意思就是用到的地方替换下, 真恶心. String value = values[i].replaceAll("\\\\", "/"); segs[i] = URLEncoder.encode(CodeUtils.cjkEncode(names[i]), EncodeConstants.ENCODING_UTF_8) + "=" + URLEncoder.encode(CodeUtils.cjkEncode(value), "UTF-8"); } catch (UnsupportedEncodingException e) { FRContext.getLogger().error(e.getMessage(), e); } } String postfixOfUri = (segs.length > 0 ? "?" + StableUtils.join(segs, "&") : StringUtils.EMPTY); if (FRContext.getCurrentEnv() instanceof RemoteEnv) { try { if (Utils.isEmbeddedParameter(postfixOfUri)) { String time = Calendar.getInstance().getTime().toString().replaceAll(" ", ""); boolean isUserPrivilege = ((RemoteEnv) FRContext.getCurrentEnv()).writePrivilegeMap(time, postfixOfUri); postfixOfUri = isUserPrivilege ? postfixOfUri + "&fr_check_url=" + time + "&id=" + FRContext.getCurrentEnv().getUserID() : postfixOfUri; } String urlPath = getWebBrowserPath(); Desktop.getDesktop().browse(new URI(urlPath + postfixOfUri)); } catch (Exception e) { FRContext.getLogger().error("cannot open the url Successful", e); } } else { try { String web = GeneralContext.getCurrentAppNameOfEnv(); String url = "http://localhost:" + DesignerEnvManager.getEnvManager().getJettyServerPort() + "/" + web + "/" + ConfigManager.getProviderInstance().getServletMapping() + postfixOfUri; StartServer.browerURLWithLocalEnv(url); } catch (Throwable e) { // } } }
/** * create matroid with matrix a as element repeated for rows and cols * * @param a * @param rows * @param cols * @return */ public static int[][] repmat(int[][] a, int rows, int cols) { int[][] b = (int[][]) ArrayUtils.copy(a); for (int i = 0; i < cols; i++) { b = concat(b, a, false); } int[][] c = (int[][]) ArrayUtils.copy(b); for (int i = 0; i < rows; i++) { c = concat(c, b, true); } return c; }
// ************************************************************************** // Test methods // ************************************************************************** public void test1D() { double[] buff = new double[100]; for (int i = 0; i < 100; i++) { buff[i] = Math.random(); // range between 0 and 1 } ArrayUtils.normalize(buff, -1, 1, 0, 100); // Buffer, Min val, Max, // start index, length assertTrue(MathUtils.isApproximatelyEqual(-1, ArrayUtils.min(buff))); assertTrue(MathUtils.isApproximatelyEqual(1, ArrayUtils.max(buff))); }
private void sendData(byte[] data) { int maxShieldFrameBytes = (MAX_OUTPUT_BYTES - 3) / 2; ArrayList<byte[]> subArrays = new ArrayList<>(); for (int i = 0; i < data.length; i += maxShieldFrameBytes) { byte[] subArray = (i + maxShieldFrameBytes > data.length) ? ArrayUtils.copyOfRange(data, i, data.length) : ArrayUtils.copyOfRange(data, i, i + maxShieldFrameBytes); subArrays.add(subArray); } synchronized (sendingDataLock) { for (byte[] sub : subArrays) sysex(SERIAL_DATA, sub); } }
public void test2D() { double[][] buff = new double[4][4]; for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { buff[j][i] = Math.random(); // range between 0 and 1 } } ArrayUtils.normalize(buff, -10, 10, 1, 2, 1, 2); assertTrue(MathUtils.isApproximatelyEqual(-10, ArrayUtils.min(buff))); assertTrue(MathUtils.isApproximatelyEqual(10, ArrayUtils.max(buff))); }
public void testArrayAdd6() throws Exception { String[] res = (String[]) ArrayUtils.add(new String[] {"a", "b"}, 2, "c"); assertEquals(3, res.length); assertEquals("a", res[0]); assertEquals("b", res[1]); assertEquals("c", res[2]); }
@Test public void testFindStartOfArrayJDK() { final int index = ArrayUtils.findStartOfArrayJDK(target, toFind); Assert.assertTrue( "The index returned should be 2 but was " + index, index == CORRECT_INDEX_VALUE); }
public void test3D() { double[][][] buff = new double[8][4][2]; for (int k = 0; k < 8; k++) { for (int j = 0; j < 4; j++) { for (int i = 0; i < 2; i++) buff[k][j][i] = Math.random(); // range between 0 and 1 } } ArrayUtils.normalize(buff, -1, 1); // Buffer, Min val, Max, // start index, length // ArrayUtils.print(buff); assertTrue(MathUtils.isApproximatelyEqual(-1, ArrayUtils.min(buff))); assertTrue(MathUtils.isApproximatelyEqual(1, ArrayUtils.max(buff))); }
@Test(expected = IllegalArgumentException.class) public void testFindStartOfArrayJDK_Empty() { final int index = ArrayUtils.findStartOfArrayJDK(targetEmpty, toFind); Assert.assertTrue( "The index returned should be -1 but was " + index, index == ArrayUtils.ARRAY_NOT_FOUND); }
public void testReverseArrayInPlace() { final byte[] test = {1, 2, 3, 4, 5, 6, 7, 8, 9}; ArrayUtils.reverseArrayInPlace(test); for (int i = 0; i < test.length; i++) { assertEquals(9 - i, test[i]); } }
/** * Returns the desired Method much like <code>Class.getMethod</code>, however it ensures that the * returned Method is from a public class or interface and not from an anonymous inner class. This * means that the Method is invokable and doesn't fall foul of Java bug <a * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957">4071957</a>). <code> * <pre>Set set = Collections.unmodifiableSet(...); * Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty", new Class[0]); * Object result = method.invoke(set, new Object[]);</pre></code> * * @param cls the class to check, not null * @param methodName the name of the method * @param parameterTypes the list of parameters * @return the method * @throws NullPointerException if the class is null * @throws SecurityException if a a security violation occured * @throws NoSuchMethodException if the method is not found in the given class or if the metothod * doen't conform with the requirements */ public static Method getPublicMethod(Class<?> cls, String methodName, Class<?> parameterTypes[]) throws SecurityException, NoSuchMethodException { Method declaredMethod = cls.getMethod(methodName, parameterTypes); if (Modifier.isPublic(declaredMethod.getDeclaringClass().getModifiers())) { return declaredMethod; } List<Class<?>> candidateClasses = new ArrayList<Class<?>>(); candidateClasses.addAll(getAllInterfaces(cls)); candidateClasses.addAll(getAllSuperclasses(cls)); for (Class<?> candidateClass : candidateClasses) { if (!Modifier.isPublic(candidateClass.getModifiers())) { continue; } Method candidateMethod; try { candidateMethod = candidateClass.getMethod(methodName, parameterTypes); } catch (NoSuchMethodException ex) { continue; } if (Modifier.isPublic(candidateMethod.getDeclaringClass().getModifiers())) { return candidateMethod; } } throw new NoSuchMethodException( "Can't find a public method for " + methodName + " " + ArrayUtils.toString(parameterTypes)); }
/** * ************************************************************************************************* * * <p>************************************************************************************************* */ public static Method geMethodThatBestMatches( Class class_i, String methodName_i, Class[] methodParameterTypes_i) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { // determine if we have already found a matching method Method method; String key = class_i + ">" + methodName_i + ":" + ArrayUtils.toString(methodParameterTypes_i, ","); synchronized (ReflectionUtils.class) { if (ourMethodCache == null) { ourMethodCache = new LRUCache(100); } method = (Method) ourMethodCache.get(key); } if (method == null) { // look for a matching method MethodMatcher methodMatcher = new MethodMatcher(class_i); method = methodMatcher.findBestMethodMatch(methodName_i, methodParameterTypes_i); if (method != null) { synchronized (ReflectionUtils.class) { ourMethodCache.put(key, method); } } } // return the method return method; }
/** * Project each archetype into original feature space * * @param frame Original training data with m rows and n columns * @param destination_key Frame Id for output * @return Frame containing k rows and n columns, where each row corresponds to an archetype */ public Frame scoreArchetypes(Frame frame, Key destination_key, boolean reverse_transform) { final int ncols = _output._names.length; Frame adaptedFr = new Frame(frame); adaptTestForTrain(adaptedFr, true, false); assert ncols == adaptedFr.numCols(); String[][] adaptedDomme = adaptedFr.domains(); double[][] proj = new double[_parms._k][_output._nnums + _output._ncats]; // Categorical columns for (int d = 0; d < _output._ncats; d++) { double[][] block = _output._archetypes_raw.getCatBlock(d); for (int k = 0; k < _parms._k; k++) proj[k][_output._permutation[d]] = _parms.mimpute(block[k], _output._lossFunc[d]); } // Numeric columns for (int d = _output._ncats; d < (_output._ncats + _output._nnums); d++) { int ds = d - _output._ncats; for (int k = 0; k < _parms._k; k++) { double num = _output._archetypes_raw.getNum(ds, k); proj[k][_output._permutation[d]] = _parms.impute(num, _output._lossFunc[d]); if (reverse_transform) proj[k][_output._permutation[d]] = proj[k][_output._permutation[d]] / _output._normMul[ds] + _output._normSub[ds]; } } // Convert projection of archetypes into a frame with correct domains Frame f = ArrayUtils.frame( (null == destination_key ? Key.make() : destination_key), adaptedFr.names(), proj); for (int i = 0; i < ncols; i++) f.vec(i).setDomain(adaptedDomme[i]); return f; }
@Test public void testFindStartOfArrayJDK_doesNotContain() { final int index = ArrayUtils.findStartOfArrayJDK(targetConfuse, toFind); Assert.assertTrue( "The index returned should be -1 but was " + index, index == ArrayUtils.ARRAY_NOT_FOUND); }
public static void main(String[] args) { int al = 10; Integer a[] = new Integer[al]; for (int i = 0; i < al; i++) { a[i] = al - i; } System.out.println("Antes de sort"); for (int i = 0; i < al; i++) { System.out.println(a[i]); } ArrayUtils.sort(a, a.length); System.out.println("Despues de sort"); for (int i = 0; i < al; i++) { System.out.println(a[i]); } int asl = 3; String as[] = new String[asl]; as[0] = "Hola"; as[1] = "Mundo"; as[2] = "Cruel"; System.out.println("Antes de sort"); for (int i = 0; i < asl; i++) { System.out.println(as[i]); } ArrayUtils.sort(as, as.length); System.out.println("Despues de sort"); for (int i = 0; i < asl; i++) { System.out.println(as[i]); } Libro l1 = new Libro("The Lord of the Rings", "J. R. R. Tolkien", 1000); Libro l2 = new Libro("Harry Potter", "J. K. Rowling", 600); Catalogo c1 = new Catalogo(); c1.agregarLibro(l1); c1.agregarLibro(l2); System.out.println("Antes de sort"); c1.mostrar(); c1.ordenar(); System.out.println("Despues de sort"); c1.mostrar(); }
/** Append an integer as a four byte number. */ public void appendInt(int x) { elems = ArrayUtils.ensureCapacity(elems, length + 3); elems[length] = (byte) ((x >> 24) & 0xFF); elems[length + 1] = (byte) ((x >> 16) & 0xFF); elems[length + 2] = (byte) ((x >> 8) & 0xFF); elems[length + 3] = (byte) ((x) & 0xFF); length = length + 4; }
public void testAsByteArray() { final byte[] testVector = {1, 0, (byte) 255, 1, 16, (byte) 240, Byte.MAX_VALUE, Byte.MIN_VALUE}; final ArrayList<Byte> al = new ArrayList<>(); for (final byte b : testVector) { al.add(b); } assertTrue(Arrays.equals(testVector, ArrayUtils.asByteArray(al))); }
public void write(char[] cbuf, int off, int len) { if (off == 0 && cbuf.length == len) { this.add(cbuf); } else { char[] buffer = ArrayUtils.copyRange(cbuf, off, off + len); this.add(buffer); } }
public void testAsLongArray() { final long[] testVector = {1, 0, (byte) 255, 1, 16, (byte) 240, Long.MAX_VALUE, Long.MIN_VALUE}; final ArrayList<Long> al = new ArrayList<>(); for (final long b : testVector) { al.add(b); } assertTrue(Arrays.equals(testVector, ArrayUtils.asLongArray(al))); }
private int swapLowestWithRootAndRemove() { int root = heap[0]; ArrayUtils.swap(heap, 0, size - 1); heap[size - 1] = minHeap ? Integer.MAX_VALUE : Integer.MIN_VALUE; size--; downHeap(); return root; }
private static void handleExportAttributesToJAR(Element e, GUI gui, File toDir) { /* Checks configuration for EXPORT attributes: * copy: file copy file to exported JAR, update file path. * discard: remove config element */ for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) { Attribute a = c.getAttribute("EXPORT"); if (a != null && a.getValue().equals("copy")) { /* Copy file to JAR */ File file = gui.restorePortablePath(new File(c.getText())); if (!file.exists()) { throw new RuntimeException("File not found: " + file); } byte[] data = ArrayUtils.readFromFile(file); if (data == null) { throw new RuntimeException("Could not copy file: " + file); } String newFilename = file.getName(); while (new File(toDir, newFilename).exists()) { newFilename += "-1"; } boolean ok = ArrayUtils.writeToFile(new File(toDir, newFilename), data); if (!ok) { throw new RuntimeException("Error when copying file: " + file); } logger.info( "Simconfig: Copied file: " + file.getAbsolutePath() + " -> " + ("[CONFIG_DIR]/" + newFilename)); ((Element) c).setText("[CONFIG_DIR]/" + newFilename); } else if (a != null && a.getValue().equals("discard")) { /* Remove config element */ e.removeChild(c.getName()); logger.info("Simconfig: Discarded element '" + c.getName() + "': " + c.getText()); continue; } else if (a != null) { throw new RuntimeException("Unknown EXPORT attribute value: " + a.getValue()); } /* Recursive search */ handleExportAttributesToJAR(c, gui, toDir); } }
private static byte[] padding(byte[] sourceBytes, byte b) { int paddingSize = 8 - sourceBytes.length % 8; byte[] paddingBytes = new byte[paddingSize]; for (int i = 0; i < paddingBytes.length; ++i) { paddingBytes[i] = b; } sourceBytes = ArrayUtils.addAll(sourceBytes, paddingBytes); return sourceBytes; }
@Test public void testCopy() { String[] from = {"Lorem", "ipsum", "dolor", "sit"}; String[] to = new String[from.length]; ArrayUtils.copy(from, to); // We should have two different objects assertFalse(from == to); assertArrayEquals(from, to); }
// Call builder specific score code and then correct probabilities // if it is necessary. void score2(Chunk chks[], double weight, double offset, double fs[ /*nclass*/], int row) { double sum = score1(chks, weight, offset, fs, row); if (isClassifier()) { if (!Double.isInfinite(sum) && sum > 0f && sum != 1f) ArrayUtils.div(fs, sum); if (_parms._balance_classes) GenModel.correctProbabilities( fs, _model._output._priorClassDist, _model._output._modelClassDist); } }
public LinearClassifier createLinearClassifier(double[] weights) { double[][] weights2D; if (objective != null) { weights2D = objective.to2D(weights); } else { weights2D = ArrayUtils.to2D(weights, featureIndex.size(), labelIndex.size()); } return new LinearClassifier<L, F>(weights2D, featureIndex, labelIndex); }
@Override public void reduce(Lloyds mr) { for (int clu = 0; clu < _k; clu++) { long ra = _size[clu]; long rb = mr._size[clu]; double[] ma = _cMeans[clu]; double[] mb = mr._cMeans[clu]; for (int c = 0; c < ma.length; c++) // Recursive mean if (ra + rb > 0) ma[c] = (ma[c] * ra + mb[c] * rb) / (ra + rb); } ArrayUtils.add(_cats, mr._cats); ArrayUtils.add(_cSqr, mr._cSqr); ArrayUtils.add(_size, mr._size); // track global worst-row if (_worst_err < mr._worst_err) { _worst_err = mr._worst_err; _worst_row = mr._worst_row; } }
/** * @param cacheNames 2 Optional caches names. * @return 2 preconfigured meta caches. */ private GridCacheConfiguration[] metaCaches(String... cacheNames) { assertTrue(ArrayUtils.isEmpty(cacheNames) || cacheNames.length == 2); if (ArrayUtils.isEmpty(cacheNames)) cacheNames = new String[] {metaCache1Name, metaCache2Name}; GridCacheConfiguration[] res = new GridCacheConfiguration[cacheNames.length]; for (int i = 0; i < cacheNames.length; i++) { GridCacheConfiguration metaCache = defaultCacheConfiguration(); metaCache.setName(cacheNames[i]); metaCache.setAtomicityMode(TRANSACTIONAL); metaCache.setQueryIndexEnabled(false); res[i] = metaCache; } return res; }