idx int64 0 41.2k | question stringlengths 73 5.81k | target stringlengths 5 918 |
|---|---|---|
11,000 | public void put ( K [ ] key , V value ) { Node child = root . get ( key [ 0 ] ) ; if ( child == null ) { child = new Node ( key [ 0 ] ) ; root . put ( key [ 0 ] , child ) ; } child . addChild ( key , value , 1 ) ; } | Add a key with associated value to the trie . |
11,001 | public V get ( K [ ] key ) { Node child = root . get ( key [ 0 ] ) ; if ( child != null ) { return child . getChild ( key , 1 ) ; } return null ; } | Returns the associated value of a given key . Returns null if the key doesn t exist in the trie . |
11,002 | public DenseMatrix inverse ( ) { int m = lu . nrows ( ) ; int n = lu . ncols ( ) ; if ( m != n ) { throw new IllegalArgumentException ( String . format ( "Matrix is not square: %d x %d" , m , n ) ) ; } DenseMatrix inv = Matrix . zeros ( n , n ) ; for ( int i = 0 ; i < n ; i ++ ) { inv . set ( i , piv [ i ] , 1.0 ) ; } solve ( inv ) ; return inv ; } | Returns the matrix inverse . For pseudo inverse use QRDecomposition . |
11,003 | private static double AndersonDarling ( double [ ] x ) { int n = x . length ; Arrays . sort ( x ) ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = GaussianDistribution . getInstance ( ) . cdf ( x [ i ] ) ; if ( x [ i ] == 0 ) x [ i ] = 0.0000001 ; if ( x [ i ] == 1 ) x [ i ] = 0.9999999 ; } double A = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { A -= ( 2 * i + 1 ) * ( Math . log ( x [ i ] ) + Math . log ( 1 - x [ n - i - 1 ] ) ) ; } A = A / n - n ; A *= ( 1 + 4.0 / n - 25.0 / ( n * n ) ) ; return A ; } | Calculates the Anderson - Darling statistic for one - dimensional normality test . |
11,004 | public String get ( String name ) { Parameter param = parameters . get ( name ) ; if ( param != null ) return param . value ; else return null ; } | Returns the value of a given parameter . Null if the parameter does not present . |
11,005 | private List < String > filterMonadics ( String [ ] args ) { List < String > filteredArgs = new ArrayList < > ( ) ; for ( String arg : args ) { filteredArgs . add ( arg ) ; Parameter param = parameters . get ( arg ) ; if ( param != null && param . paramType == ParameterType . MONADIC ) { filteredArgs . add ( "1" ) ; } } return filteredArgs ; } | Parses the args array looking for monads arguments without value such as - verbose and if found inserts forces a value of 1 and returns the transformed args as a List |
11,006 | public List < String > parse ( String [ ] args ) { List < String > extras = new ArrayList < > ( ) ; List < String > filteredArgs = filterMonadics ( args ) ; for ( int i = 0 ; i < filteredArgs . size ( ) ; i ++ ) { String key = filteredArgs . get ( i ) ; if ( key . equalsIgnoreCase ( "-h" ) || key . equalsIgnoreCase ( "-help" ) ) { System . out . println ( usage ) ; System . exit ( 0 ) ; } if ( parameters . containsKey ( key ) ) { Parameter param = parameters . get ( key ) ; param . value = filteredArgs . get ( i + 1 ) ; switch ( param . paramType ) { case INTEGER : try { Integer . parseInt ( param . value ) ; } catch ( Exception ex ) { System . err . println ( "Invalid parameter " + param . name + ' ' + param . value ) ; System . err . println ( usage ) ; System . exit ( 1 ) ; } break ; case FLOAT : try { Double . parseDouble ( param . value ) ; } catch ( Exception ex ) { System . err . println ( "Invalid parameter " + param . name + ' ' + param . value ) ; System . err . println ( usage ) ; System . exit ( 1 ) ; } break ; default : } i ++ ; } else { extras . add ( key ) ; } } for ( String key : parameters . keySet ( ) ) { Parameter param = parameters . get ( key ) ; if ( param . mandatory && param . value == null ) { System . err . println ( "Missing mandatory parameter: " + key ) ; System . err . println ( usage ) ; System . exit ( 1 ) ; } } return extras ; } | Parse the arguments . |
11,007 | public DecisionTree [ ] getTrees ( ) { DecisionTree [ ] forest = new DecisionTree [ trees . size ( ) ] ; for ( int i = 0 ; i < forest . length ; i ++ ) forest [ i ] = trees . get ( i ) . tree ; return forest ; } | Returns the decision trees . |
11,008 | public void setMnemonic ( int mnemonic ) { this . mnemonic = mnemonic ; renderButton . setMnemonic ( mnemonic ) ; editButton . setMnemonic ( mnemonic ) ; } | The mnemonic to activate the button when the cell has focus |
11,009 | private void add ( Node node , int size , int index , int [ ] itemset , int support ) { if ( node . children == null ) { node . children = new Node [ size ] ; } int item = order [ itemset [ index ] ] ; if ( node . children [ item ] == null ) { node . children [ item ] = new Node ( itemset [ index ] ) ; } if ( index == 0 ) { node . children [ item ] . support += support ; } else { add ( node . children [ item ] , item , index - 1 , itemset , support ) ; } } | Inserts a node into a T - tree . |
11,010 | public int getSupport ( int [ ] itemset ) { if ( root . children != null ) { return getSupport ( itemset , itemset . length - 1 , root ) ; } else { return 0 ; } } | Returns the support value for the given item set . |
11,011 | private int getSupport ( int [ ] itemset , int index , Node node ) { int item = order [ itemset [ index ] ] ; Node child = node . children [ item ] ; if ( child != null ) { if ( index == 0 ) { return child . support ; } else { if ( child . children != null ) { return getSupport ( itemset , index - 1 , child ) ; } } } return 0 ; } | Returns the support value for the given item set if found in the T - tree and 0 otherwise . |
11,012 | private void initTokenizer ( StreamTokenizer tokenizer ) { tokenizer . resetSyntax ( ) ; tokenizer . whitespaceChars ( 0 , ' ' ) ; tokenizer . wordChars ( ' ' + 1 , '\u00FF' ) ; tokenizer . whitespaceChars ( ',' , ',' ) ; tokenizer . commentChar ( '%' ) ; tokenizer . quoteChar ( '"' ) ; tokenizer . quoteChar ( '\'' ) ; tokenizer . ordinaryChar ( '{' ) ; tokenizer . ordinaryChar ( '}' ) ; tokenizer . eolIsSignificant ( true ) ; } | Initializes the StreamTokenizer used for reading the ARFF file . |
11,013 | private void getFirstToken ( StreamTokenizer tokenizer ) throws IOException { while ( tokenizer . nextToken ( ) == StreamTokenizer . TT_EOL ) { } if ( ( tokenizer . ttype == '\'' ) || ( tokenizer . ttype == '"' ) ) { tokenizer . ttype = StreamTokenizer . TT_WORD ; } else if ( ( tokenizer . ttype == StreamTokenizer . TT_WORD ) && ( tokenizer . sval . equals ( "?" ) ) ) { tokenizer . ttype = '?' ; } } | Gets next token skipping empty lines . |
11,014 | private void getLastToken ( StreamTokenizer tokenizer , boolean endOfFileOk ) throws IOException , ParseException { if ( ( tokenizer . nextToken ( ) != StreamTokenizer . TT_EOL ) && ( ( tokenizer . ttype != StreamTokenizer . TT_EOF ) || ! endOfFileOk ) ) { throw new ParseException ( "end of line expected" , tokenizer . lineno ( ) ) ; } } | Gets token and checks if it s end of line . |
11,015 | private void getNextToken ( StreamTokenizer tokenizer ) throws IOException , ParseException { if ( tokenizer . nextToken ( ) == StreamTokenizer . TT_EOL ) { throw new ParseException ( "premature end of line" , tokenizer . lineno ( ) ) ; } if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { throw new ParseException ( PREMATURE_END_OF_FILE , tokenizer . lineno ( ) ) ; } else if ( ( tokenizer . ttype == '\'' ) || ( tokenizer . ttype == '"' ) ) { tokenizer . ttype = StreamTokenizer . TT_WORD ; } else if ( ( tokenizer . ttype == StreamTokenizer . TT_WORD ) && ( tokenizer . sval . equals ( "?" ) ) ) { tokenizer . ttype = '?' ; } } | Gets next token checking for a premature and of line . |
11,016 | private String readHeader ( StreamTokenizer tokenizer , List < Attribute > attributes ) throws IOException , ParseException { String relationName = null ; attributes . clear ( ) ; getFirstToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { throw new ParseException ( PREMATURE_END_OF_FILE , tokenizer . lineno ( ) ) ; } if ( ARFF_RELATION . equalsIgnoreCase ( tokenizer . sval ) ) { getNextToken ( tokenizer ) ; relationName = tokenizer . sval ; getLastToken ( tokenizer , false ) ; } else { throw new ParseException ( "keyword " + ARFF_RELATION + " expected" , tokenizer . lineno ( ) ) ; } getFirstToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { throw new ParseException ( PREMATURE_END_OF_FILE , tokenizer . lineno ( ) ) ; } while ( ARFF_ATTRIBUTE . equalsIgnoreCase ( tokenizer . sval ) ) { attributes . add ( parseAttribute ( tokenizer ) ) ; } if ( ! ARFF_DATA . equalsIgnoreCase ( tokenizer . sval ) ) { throw new ParseException ( "keyword " + ARFF_DATA + " expected" , tokenizer . lineno ( ) ) ; } if ( attributes . isEmpty ( ) ) { throw new ParseException ( "no attributes declared" , tokenizer . lineno ( ) ) ; } if ( responseIndex >= attributes . size ( ) ) { throw new ParseException ( "Invalid response variable index" , responseIndex ) ; } return relationName ; } | Reads and stores header of an ARFF file . |
11,017 | private Attribute parseAttribute ( StreamTokenizer tokenizer ) throws IOException , ParseException { Attribute attribute = null ; getNextToken ( tokenizer ) ; String attributeName = tokenizer . sval ; getNextToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_WORD ) { if ( tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_REAL ) || tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_INTEGER ) || tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_NUMERIC ) ) { attribute = new NumericAttribute ( attributeName ) ; readTillEOL ( tokenizer ) ; } else if ( tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_STRING ) ) { attribute = new StringAttribute ( attributeName ) ; readTillEOL ( tokenizer ) ; } else if ( tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_DATE ) ) { String format = null ; if ( tokenizer . nextToken ( ) != StreamTokenizer . TT_EOL ) { if ( ( tokenizer . ttype != StreamTokenizer . TT_WORD ) && ( tokenizer . ttype != '\'' ) && ( tokenizer . ttype != '\"' ) ) { throw new ParseException ( "not a valid date format" , tokenizer . lineno ( ) ) ; } format = tokenizer . sval ; readTillEOL ( tokenizer ) ; } else { tokenizer . pushBack ( ) ; } attribute = new DateAttribute ( attributeName , null , format ) ; readTillEOL ( tokenizer ) ; } else if ( tokenizer . sval . equalsIgnoreCase ( ARFF_ATTRIBUTE_RELATIONAL ) ) { readTillEOL ( tokenizer ) ; } else if ( tokenizer . sval . equalsIgnoreCase ( ARFF_END_SUBRELATION ) ) { getNextToken ( tokenizer ) ; } else { throw new ParseException ( "Invalid attribute type or invalid enumeration" , tokenizer . lineno ( ) ) ; } } else { List < String > attributeValues = new ArrayList < > ( ) ; tokenizer . pushBack ( ) ; if ( tokenizer . nextToken ( ) != '{' ) { throw new ParseException ( "{ expected at beginning of enumeration" , tokenizer . lineno ( ) ) ; } while ( tokenizer . nextToken ( ) != '}' ) { if ( tokenizer . ttype == StreamTokenizer . TT_EOL ) { throw new ParseException ( "} expected at end of enumeration" , tokenizer . lineno ( ) ) ; } else { attributeValues . add ( tokenizer . sval . trim ( ) ) ; } } String [ ] values = new String [ attributeValues . size ( ) ] ; for ( int i = 0 ; i < values . length ; i ++ ) { values [ i ] = attributeValues . get ( i ) ; } attribute = new NominalAttribute ( attributeName , values ) ; } getLastToken ( tokenizer , false ) ; getFirstToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { throw new ParseException ( PREMATURE_END_OF_FILE , tokenizer . lineno ( ) ) ; } return attribute ; } | Parses the attribute declaration . |
11,018 | private void readTillEOL ( StreamTokenizer tokenizer ) throws IOException { while ( tokenizer . nextToken ( ) != StreamTokenizer . TT_EOL ) { } tokenizer . pushBack ( ) ; } | Reads and skips all tokens before next end of line token . |
11,019 | public static Attribute [ ] getAttributes ( InputStream stream ) throws IOException , ParseException { Reader r = new BufferedReader ( new InputStreamReader ( stream ) ) ; StreamTokenizer tokenizer = new StreamTokenizer ( r ) ; ArffParser parser = new ArffParser ( ) ; parser . initTokenizer ( tokenizer ) ; List < Attribute > attributes = new ArrayList < > ( ) ; parser . readHeader ( tokenizer , attributes ) ; return attributes . toArray ( new Attribute [ attributes . size ( ) ] ) ; } | Returns the attribute set of given stream . |
11,020 | public AttributeDataset parse ( InputStream stream ) throws IOException , ParseException { try ( Reader r = new BufferedReader ( new InputStreamReader ( stream ) ) ) { StreamTokenizer tokenizer = new StreamTokenizer ( r ) ; initTokenizer ( tokenizer ) ; List < Attribute > attributes = new ArrayList < > ( ) ; String relationName = readHeader ( tokenizer , attributes ) ; if ( attributes . isEmpty ( ) ) { throw new IOException ( "no header information available" ) ; } Attribute response = null ; Attribute [ ] attr = new Attribute [ attributes . size ( ) ] ; attributes . toArray ( attr ) ; for ( int i = 0 ; i < attributes . size ( ) ; i ++ ) { if ( responseIndex == i ) { response = attributes . remove ( i ) ; break ; } } AttributeDataset data = new AttributeDataset ( relationName , attributes . toArray ( new Attribute [ attributes . size ( ) ] ) , response ) ; while ( true ) { getFirstToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { break ; } if ( tokenizer . ttype == '{' ) { readSparseInstance ( tokenizer , data , attr ) ; } else { readInstance ( tokenizer , data , attr ) ; } } for ( Attribute attribute : attributes ) { if ( attribute instanceof NominalAttribute ) { NominalAttribute a = ( NominalAttribute ) attribute ; a . setOpen ( false ) ; } if ( attribute instanceof StringAttribute ) { StringAttribute a = ( StringAttribute ) attribute ; a . setOpen ( false ) ; } } return data ; } } | Parse a dataset from given stream . |
11,021 | private void readInstance ( StreamTokenizer tokenizer , AttributeDataset data , Attribute [ ] attributes ) throws IOException , ParseException { double [ ] x = responseIndex >= 0 ? new double [ attributes . length - 1 ] : new double [ attributes . length ] ; double y = Double . NaN ; for ( int i = 0 , k = 0 ; i < attributes . length ; i ++ ) { if ( i > 0 ) { getNextToken ( tokenizer ) ; } if ( i == responseIndex ) { if ( tokenizer . ttype == '?' ) { y = Double . NaN ; } else { y = attributes [ i ] . valueOf ( tokenizer . sval ) ; } } else { if ( tokenizer . ttype == '?' ) { x [ k ++ ] = Double . NaN ; } else { x [ k ++ ] = attributes [ i ] . valueOf ( tokenizer . sval ) ; } } } if ( Double . isNaN ( y ) ) data . add ( x ) ; else data . add ( x , y ) ; } | Reads a single instance . |
11,022 | private void readSparseInstance ( StreamTokenizer tokenizer , AttributeDataset data , Attribute [ ] attributes ) throws IOException , ParseException { double [ ] x = responseIndex >= 0 ? new double [ attributes . length - 1 ] : new double [ attributes . length ] ; double y = Double . NaN ; int index = - 1 ; do { getNextToken ( tokenizer ) ; if ( tokenizer . ttype == '}' ) { break ; } String s = tokenizer . sval . trim ( ) ; if ( index < 0 ) { index = Integer . parseInt ( s ) ; if ( index < 0 || index >= attributes . length ) { throw new ParseException ( "Invalid attribute index: " + index , tokenizer . lineno ( ) ) ; } } else { String val = s ; if ( index != responseIndex ) { if ( val . equals ( "?" ) ) { x [ index ] = Double . NaN ; } else { x [ index ] = attributes [ index ] . valueOf ( val ) ; } } else { if ( val . equals ( "?" ) ) { y = Double . NaN ; } else { y = attributes [ index ] . valueOf ( val ) ; } } index = - 1 ; } } while ( tokenizer . ttype == StreamTokenizer . TT_WORD ) ; if ( Double . isNaN ( y ) ) data . add ( x ) ; else data . add ( x , y ) ; } | Reads a sparse instance using the tokenizer . |
11,023 | private static double startv ( Matrix A , double [ ] [ ] q , double [ ] [ ] wptr , int step ) { double rnm = Math . dot ( wptr [ 0 ] , wptr [ 0 ] ) ; double [ ] r = wptr [ 0 ] ; for ( int id = 0 ; id < 3 ; id ++ ) { if ( id > 0 || step > 0 || rnm == 0 ) { for ( int i = 0 ; i < r . length ; i ++ ) { r [ i ] = Math . random ( ) - 0.5 ; } } Math . copy ( wptr [ 0 ] , wptr [ 3 ] ) ; A . ax ( wptr [ 3 ] , wptr [ 0 ] ) ; Math . copy ( wptr [ 0 ] , wptr [ 3 ] ) ; rnm = Math . dot ( wptr [ 0 ] , wptr [ 3 ] ) ; if ( rnm > 0.0 ) { break ; } } if ( rnm <= 0.0 ) { logger . error ( "Lanczos method was unable to find a starting vector within range." ) ; return - 1 ; } if ( step > 0 ) { for ( int i = 0 ; i < step ; i ++ ) { double t = Math . dot ( wptr [ 3 ] , q [ i ] ) ; Math . axpy ( - t , q [ i ] , wptr [ 0 ] ) ; } double t = Math . dot ( wptr [ 4 ] , wptr [ 0 ] ) ; Math . axpy ( - t , wptr [ 2 ] , wptr [ 0 ] ) ; Math . copy ( wptr [ 0 ] , wptr [ 3 ] ) ; t = Math . dot ( wptr [ 3 ] , wptr [ 0 ] ) ; if ( t <= Math . EPSILON * rnm ) { t = 0.0 ; } rnm = t ; } return Math . sqrt ( rnm ) ; } | Generate a starting vector in r and returns |r| . It returns zero if the range is spanned and throws exception if no starting vector within range of operator can be found . |
11,024 | private static void ortbnd ( double [ ] alf , double [ ] bet , double [ ] eta , double [ ] oldeta , int step , double rnm , double eps ) { if ( step < 1 ) { return ; } if ( 0 != rnm ) { if ( step > 1 ) { oldeta [ 0 ] = ( bet [ 1 ] * eta [ 1 ] + ( alf [ 0 ] - alf [ step ] ) * eta [ 0 ] - bet [ step ] * oldeta [ 0 ] ) / rnm + eps ; } for ( int i = 1 ; i <= step - 2 ; i ++ ) { oldeta [ i ] = ( bet [ i + 1 ] * eta [ i + 1 ] + ( alf [ i ] - alf [ step ] ) * eta [ i ] + bet [ i ] * eta [ i - 1 ] - bet [ step ] * oldeta [ i ] ) / rnm + eps ; } } oldeta [ step - 1 ] = eps ; for ( int i = 0 ; i < step ; i ++ ) { double swap = eta [ i ] ; eta [ i ] = oldeta [ i ] ; oldeta [ i ] = swap ; } eta [ step ] = eps ; } | Update the eta recurrence . |
11,025 | private static double purge ( int ll , double [ ] [ ] Q , double [ ] r , double [ ] q , double [ ] ra , double [ ] qa , double [ ] eta , double [ ] oldeta , int step , double rnm , double tol , double eps , double reps ) { if ( step < ll + 2 ) { return rnm ; } double t , tq , tr ; int k = idamax ( step - ( ll + 1 ) , eta , ll , 1 ) + ll ; if ( Math . abs ( eta [ k ] ) > reps ) { double reps1 = eps / reps ; int iteration = 0 ; boolean flag = true ; while ( iteration < 2 && flag ) { if ( rnm > tol ) { tq = 0.0 ; tr = 0.0 ; for ( int i = ll ; i < step ; i ++ ) { t = - Math . dot ( qa , Q [ i ] ) ; tq += Math . abs ( t ) ; Math . axpy ( t , Q [ i ] , q ) ; t = - Math . dot ( ra , Q [ i ] ) ; tr += Math . abs ( t ) ; Math . axpy ( t , Q [ i ] , r ) ; } Math . copy ( q , qa ) ; t = - Math . dot ( r , qa ) ; tr += Math . abs ( t ) ; Math . axpy ( t , q , r ) ; Math . copy ( r , ra ) ; rnm = Math . sqrt ( Math . dot ( ra , r ) ) ; if ( tq <= reps1 && tr <= reps1 * rnm ) { flag = false ; } } iteration ++ ; } for ( int i = ll ; i <= step ; i ++ ) { eta [ i ] = eps ; oldeta [ i ] = eps ; } } return rnm ; } | Examine the state of orthogonality between the new Lanczos vector and the previous ones to decide whether re - orthogonalization should be performed . |
11,026 | private static int idamax ( int n , double [ ] dx , int ix0 , int incx ) { int ix , imax ; double dmax ; if ( n < 1 ) { return - 1 ; } if ( n == 1 ) { return 0 ; } if ( incx == 0 ) { return - 1 ; } ix = ( incx < 0 ) ? ix0 + ( ( - n + 1 ) * incx ) : ix0 ; imax = ix ; dmax = Math . abs ( dx [ ix ] ) ; for ( int i = 1 ; i < n ; i ++ ) { ix += incx ; double dtemp = Math . abs ( dx [ ix ] ) ; if ( dtemp > dmax ) { dmax = dtemp ; imax = ix ; } } return imax ; } | Find the index of element having maximum absolute value . |
11,027 | private static void store ( double [ ] [ ] q , int j , double [ ] s ) { if ( null == q [ j ] ) { q [ j ] = s . clone ( ) ; } else { Math . copy ( s , q [ j ] ) ; } } | Based on the input operation flag stores to or retrieves from memory a vector . |
11,028 | public static double d ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; double dist = 0.0 ; for ( int i = 0 ; i < x . length ; i ++ ) { double d = Math . abs ( x [ i ] - y [ i ] ) ; if ( dist < d ) dist = d ; } return dist ; } | Chebyshev distance between the two arrays of type integer . |
11,029 | public void valueChanged ( TreeSelectionEvent e ) { DefaultMutableTreeNode node = ( DefaultMutableTreeNode ) tree . getLastSelectedPathComponent ( ) ; if ( node != null && node . isLeaf ( ) ) { int pos = workspace . getDividerLocation ( ) ; workspace . setTopComponent ( ( JPanel ) node . getUserObject ( ) ) ; workspace . setDividerLocation ( pos ) ; } } | Required by TreeSelectionListener interface . |
11,030 | public void learn ( double [ ] [ ] data ) { int p = data [ 0 ] . length ; Attribute [ ] attributes = new Attribute [ p ] ; for ( int i = 0 ; i < p ; i ++ ) { attributes [ i ] = new NumericAttribute ( "V" + i ) ; } learn ( attributes , data ) ; } | Learns transformation parameters from a dataset . All features are assumed numeric . |
11,031 | public double [ ] [ ] transform ( double [ ] [ ] x ) { double [ ] [ ] y = new double [ x . length ] [ ] ; for ( int i = 0 ; i < y . length ; i ++ ) { y [ i ] = transform ( x [ i ] ) ; } return y ; } | Transform an array of feature vectors . |
11,032 | public double d ( double [ ] x , double [ ] y ) { if ( x . length != y . length ) throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; return 1 - Math . cor ( x , y ) ; } | Pearson correlation distance between the two arrays of type double . |
11,033 | public static double pearson ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; return 1 - Math . cor ( x , y ) ; } | Pearson correlation distance between the two arrays of type int . |
11,034 | public static double spearman ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; return 1 - Math . spearman ( x , y ) ; } | Spearman correlation distance between the two arrays of type int . |
11,035 | public static double kendall ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; return 1 - Math . kendall ( x , y ) ; } | Kendall rank correlation distance between the two arrays of type int . |
11,036 | public static void swap ( float arr [ ] , int i , int j ) { float a = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = a ; } | Swap two positions . |
11,037 | private double getNodeCost ( Node node , double [ ] center ) { int d = center . length ; double scatter = 0.0 ; for ( int i = 0 ; i < d ; i ++ ) { double x = ( node . sum [ i ] / node . count ) - center [ i ] ; scatter += x * x ; } return node . cost + node . count * scatter ; } | Returns the total contribution of all data in the given kd - tree node assuming they are all assigned to a mean at the given location . |
11,038 | public double clustering ( double [ ] [ ] centroids , double [ ] [ ] sums , int [ ] counts , int [ ] membership ) { int k = centroids . length ; Arrays . fill ( counts , 0 ) ; int [ ] candidates = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { candidates [ i ] = i ; Arrays . fill ( sums [ i ] , 0.0 ) ; } return filter ( root , centroids , candidates , k , sums , counts , membership ) ; } | Given k cluster centroids this method assigns data to nearest centroids . The return value is the distortion to the centroids . The parameter sums will hold the sum of data for each cluster . The parameter counts hold the number of data of each cluster . If membership is not null it should be an array of size n that will be filled with the index of the cluster [ 0 - k ) that each data point is assigned to . |
11,039 | static void columnAverageImpute ( double [ ] [ ] data ) throws MissingValueImputationException { for ( int j = 0 ; j < data [ 0 ] . length ; j ++ ) { int n = 0 ; double sum = 0.0 ; for ( int i = 0 ; i < data . length ; i ++ ) { if ( ! Double . isNaN ( data [ i ] [ j ] ) ) { n ++ ; sum += data [ i ] [ j ] ; } } if ( n == 0 ) { continue ; } if ( n < data . length ) { double avg = sum / n ; for ( int i = 0 ; i < data . length ; i ++ ) { if ( Double . isNaN ( data [ i ] [ j ] ) ) { data [ i ] [ j ] = avg ; } } } } } | Impute the missing values with column averages . |
11,040 | public static < T > double loocv ( ClassifierTrainer < T > trainer , T [ ] x , int [ ] y ) { int m = 0 ; int n = x . length ; LOOCV loocv = new LOOCV ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { T [ ] trainx = Math . slice ( x , loocv . train [ i ] ) ; int [ ] trainy = Math . slice ( y , loocv . train [ i ] ) ; Classifier < T > classifier = trainer . train ( trainx , trainy ) ; if ( classifier . predict ( x [ loocv . test [ i ] ] ) == y [ loocv . test [ i ] ] ) { m ++ ; } } return ( double ) m / n ; } | Leave - one - out cross validation of a classification model . |
11,041 | public static < T > double cv ( int k , ClassifierTrainer < T > trainer , T [ ] x , int [ ] y , ClassificationMeasure measure ) { if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k for k-fold cross validation: " + k ) ; } int n = x . length ; int [ ] predictions = new int [ n ] ; CrossValidation cv = new CrossValidation ( n , k ) ; for ( int i = 0 ; i < k ; i ++ ) { T [ ] trainx = Math . slice ( x , cv . train [ i ] ) ; int [ ] trainy = Math . slice ( y , cv . train [ i ] ) ; Classifier < T > classifier = trainer . train ( trainx , trainy ) ; for ( int j : cv . test [ i ] ) { predictions [ j ] = classifier . predict ( x [ j ] ) ; } } return measure . measure ( y , predictions ) ; } | Cross validation of a classification model . |
11,042 | public static < T > double cv ( int k , RegressionTrainer < T > trainer , T [ ] x , double [ ] y , RegressionMeasure measure ) { if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k for k-fold cross validation: " + k ) ; } int n = x . length ; double [ ] predictions = new double [ n ] ; CrossValidation cv = new CrossValidation ( n , k ) ; for ( int i = 0 ; i < k ; i ++ ) { T [ ] trainx = Math . slice ( x , cv . train [ i ] ) ; double [ ] trainy = Math . slice ( y , cv . train [ i ] ) ; Regression < T > model = trainer . train ( trainx , trainy ) ; for ( int j : cv . test [ i ] ) { predictions [ j ] = model . predict ( x [ j ] ) ; } } return measure . measure ( y , predictions ) ; } | Cross validation of a regression model . |
11,043 | public static < T > double [ ] bootstrap ( int k , ClassifierTrainer < T > trainer , T [ ] x , int [ ] y ) { if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k for k-fold bootstrap: " + k ) ; } int n = x . length ; double [ ] results = new double [ k ] ; Accuracy measure = new Accuracy ( ) ; Bootstrap bootstrap = new Bootstrap ( n , k ) ; for ( int i = 0 ; i < k ; i ++ ) { T [ ] trainx = Math . slice ( x , bootstrap . train [ i ] ) ; int [ ] trainy = Math . slice ( y , bootstrap . train [ i ] ) ; Classifier < T > classifier = trainer . train ( trainx , trainy ) ; int nt = bootstrap . test [ i ] . length ; int [ ] truth = new int [ nt ] ; int [ ] predictions = new int [ nt ] ; for ( int j = 0 ; j < nt ; j ++ ) { int l = bootstrap . test [ i ] [ j ] ; truth [ j ] = y [ l ] ; predictions [ j ] = classifier . predict ( x [ l ] ) ; } results [ i ] = measure . measure ( truth , predictions ) ; } return results ; } | Bootstrap accuracy estimation of a classification model . |
11,044 | public static < T > double [ ] bootstrap ( int k , RegressionTrainer < T > trainer , T [ ] x , double [ ] y , RegressionMeasure measure ) { if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k for k-fold bootstrap: " + k ) ; } int n = x . length ; double [ ] results = new double [ k ] ; Bootstrap bootstrap = new Bootstrap ( n , k ) ; for ( int i = 0 ; i < k ; i ++ ) { T [ ] trainx = Math . slice ( x , bootstrap . train [ i ] ) ; double [ ] trainy = Math . slice ( y , bootstrap . train [ i ] ) ; Regression < T > model = trainer . train ( trainx , trainy ) ; int nt = bootstrap . test [ i ] . length ; double [ ] truth = new double [ nt ] ; double [ ] predictions = new double [ nt ] ; for ( int j = 0 ; j < nt ; j ++ ) { int l = bootstrap . test [ i ] [ j ] ; truth [ j ] = y [ l ] ; predictions [ j ] = model . predict ( x [ l ] ) ; } results [ i ] = measure . measure ( truth , predictions ) ; } return results ; } | Bootstrap performance estimation of a regression model . |
11,045 | public SparseMatrix toSparseMatrix ( ) { int [ ] pos = new int [ numColumns ] ; int [ ] colIndex = new int [ numColumns + 1 ] ; for ( int i = 0 ; i < numColumns ; i ++ ) { colIndex [ i + 1 ] = colIndex [ i ] + colSize [ i ] ; } int nrows = size ( ) ; int [ ] rowIndex = new int [ n ] ; double [ ] x = new double [ n ] ; for ( int i = 0 ; i < nrows ; i ++ ) { for ( int j : get ( i ) . x ) { int k = colIndex [ j ] + pos [ j ] ; rowIndex [ k ] = i ; x [ k ] = 1 ; pos [ j ] ++ ; } } return new SparseMatrix ( nrows , numColumns , x , rowIndex , colIndex ) ; } | Convert into Harwell - Boeing column - compressed sparse matrix format . |
11,046 | public Text add ( String id , String title , String body ) { ArrayList < String > bag = new ArrayList < > ( ) ; for ( String sentence : splitter . split ( body ) ) { String [ ] tokens = tokenizer . split ( sentence ) ; for ( int i = 0 ; i < tokens . length ; i ++ ) { tokens [ i ] = tokens [ i ] . toLowerCase ( ) ; } for ( String w : tokens ) { boolean keep = true ; if ( punctuations != null && punctuations . contains ( w ) ) { keep = false ; } else if ( stopWords != null && stopWords . contains ( w ) ) { keep = false ; } if ( keep ) { size ++ ; bag . add ( w ) ; Integer f = freq . get ( w ) ; if ( f == null ) { f = 1 ; } else { f = f + 1 ; } freq . put ( w , f ) ; } } for ( int i = 0 ; i < tokens . length - 1 ; i ++ ) { String w1 = tokens [ i ] ; String w2 = tokens [ i + 1 ] ; if ( freq . containsKey ( w1 ) && freq . containsKey ( w2 ) ) { Bigram bigram = new Bigram ( w1 , w2 ) ; Integer f = freq2 . get ( bigram ) ; if ( f == null ) { f = 1 ; } else { f = f + 1 ; } freq2 . put ( bigram , f ) ; } } } String [ ] words = new String [ bag . size ( ) ] ; for ( int i = 0 ; i < words . length ; i ++ ) { words [ i ] = bag . get ( i ) ; } SimpleText doc = new SimpleText ( id , title , body , words ) ; docs . add ( doc ) ; for ( String term : doc . unique ( ) ) { List < SimpleText > hit = invertedFile . get ( term ) ; if ( hit == null ) { hit = new ArrayList < > ( ) ; invertedFile . put ( term , hit ) ; } hit . add ( doc ) ; } return doc ; } | Add a document to the corpus . |
11,047 | public double [ ] dijkstra ( int s , boolean weighted ) { double [ ] wt = new double [ n ] ; Arrays . fill ( wt , Double . POSITIVE_INFINITY ) ; PriorityQueue queue = new PriorityQueue ( wt ) ; for ( int v = 0 ; v < n ; v ++ ) { queue . insert ( v ) ; } wt [ s ] = 0.0 ; queue . lower ( s ) ; while ( ! queue . empty ( ) ) { int v = queue . poll ( ) ; if ( ! Double . isInfinite ( wt [ v ] ) ) { for ( int w = 0 ; w < n ; w ++ ) { if ( graph [ v ] [ w ] != 0.0 ) { double p = weighted ? wt [ v ] + graph [ v ] [ w ] : wt [ v ] + 1 ; if ( p < wt [ w ] ) { wt [ w ] = p ; queue . lower ( w ) ; } } } } } return wt ; } | Calculates the shortest path by Dijkstra algorithm . |
11,048 | public double [ ] [ ] sample ( int n ) { double [ ] [ ] samples = new double [ 2 * n ] [ ] ; MultivariateGaussianDistribution [ ] gauss = new MultivariateGaussianDistribution [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { gauss [ i ] = new MultivariateGaussianDistribution ( m [ i ] , v ) ; } for ( int i = 0 ; i < n ; i ++ ) { samples [ i ] = gauss [ Math . random ( prob ) ] . rand ( ) ; } for ( int i = 0 ; i < k ; i ++ ) { gauss [ i ] = new MultivariateGaussianDistribution ( m [ k + i ] , v ) ; } for ( int i = 0 ; i < n ; i ++ ) { samples [ n + i ] = gauss [ Math . random ( prob ) ] . rand ( ) ; } return samples ; } | Generate n samples from each class . |
11,049 | public int purge ( int minPts ) { List < Neuron > outliers = new ArrayList < > ( ) ; for ( Neuron neuron : neurons ) { if ( neuron . n < minPts ) { outliers . add ( neuron ) ; } } neurons . removeAll ( outliers ) ; for ( Neuron neuron : neurons ) { neuron . neighbors . removeAll ( outliers ) ; } outliers . clear ( ) ; for ( Neuron neuron : neurons ) { if ( neuron . neighbors . isEmpty ( ) ) { outliers . add ( neuron ) ; } } neurons . removeAll ( outliers ) ; return neurons . size ( ) ; } | Removes neurons with the number of samples less than a given threshold . The neurons without neighbors will also be removed . |
11,050 | private double bic ( int n , int d , double distortion ) { double variance = distortion / ( n - 1 ) ; double p1 = - n * LOG2PI ; double p2 = - n * d * Math . log ( variance ) ; double p3 = - ( n - 1 ) ; double L = ( p1 + p2 + p3 ) / 2 ; int numParameters = d + 1 ; return L - 0.5 * numParameters * Math . log ( n ) ; } | Calculates the BIC for single cluster . |
11,051 | private double bic ( int k , int n , int d , double distortion , int [ ] clusterSize ) { double variance = distortion / ( n - k ) ; double L = 0.0 ; for ( int i = 0 ; i < k ; i ++ ) { L += logLikelihood ( k , n , clusterSize [ i ] , d , variance ) ; } int numParameters = k + k * d ; return L - 0.5 * numParameters * Math . log ( n ) ; } | Calculates the BIC for the given set of centers . |
11,052 | private static double logLikelihood ( int k , int n , int ni , int d , double variance ) { double p1 = - ni * LOG2PI ; double p2 = - ni * d * Math . log ( variance ) ; double p3 = - ( ni - k ) ; double p4 = ni * Math . log ( ni ) ; double p5 = - ni * Math . log ( n ) ; double loglike = ( p1 + p2 + p3 ) / 2 + p4 + p5 ; return loglike ; } | Estimate the log - likelihood of the data for the given model . |
11,053 | public void nextDoubles ( double [ ] d , double lo , double hi ) { real . nextDoubles ( d ) ; double l = hi - lo ; int n = d . length ; for ( int i = 0 ; i < n ; i ++ ) { d [ i ] = lo + l * d [ i ] ; } } | Generate n uniform random numbers in the range [ lo hi ) |
11,054 | public int [ ] permutate ( int n ) { int [ ] x = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = i ; } permutate ( x ) ; return x ; } | Generates a permutation of 0 1 2 ... n - 1 which is useful for sampling without replacement . |
11,055 | public void add ( double [ ] x ) { if ( root == null ) { root = new Node ( ) ; root . add ( new Leaf ( x ) ) ; root . update ( x ) ; } else { root . add ( x ) ; } } | Add a data point into CF tree . |
11,056 | public void setGraphics ( java . awt . Graphics2D g2d ) { this . g2d = g2d ; g2d . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ; g2d . setRenderingHint ( RenderingHints . KEY_RENDERING , RenderingHints . VALUE_RENDER_QUALITY ) ; } | Set the Java2D graphics object . |
11,057 | public void clip ( ) { int x = ( int ) ( projection . canvas . getWidth ( ) * projection . canvas . margin ) ; int y = ( int ) ( projection . canvas . getHeight ( ) * projection . canvas . margin ) ; int w = ( int ) ( projection . canvas . getWidth ( ) * ( 1 - 2 * projection . canvas . margin ) ) ; int h = ( int ) ( projection . canvas . getHeight ( ) * ( 1 - 2 * projection . canvas . margin ) ) ; originalClip = g2d . getClip ( ) ; g2d . clipRect ( x , y , w , h ) ; } | Restrict the draw area to the valid base coordinate space . |
11,058 | private void drawLine ( int [ ] ... coord ) { int [ ] x = new int [ coord . length ] ; for ( int i = 0 ; i < coord . length ; i ++ ) { x [ i ] = coord [ i ] [ 0 ] ; } int [ ] y = new int [ coord . length ] ; for ( int i = 0 ; i < coord . length ; i ++ ) { y [ i ] = coord [ i ] [ 1 ] ; } g2d . drawPolyline ( x , y , coord . length ) ; } | Draw poly line . |
11,059 | public void drawLine ( double [ ] ... coord ) { int [ ] [ ] sc = new int [ coord . length ] [ ] ; for ( int i = 0 ; i < sc . length ; i ++ ) { sc [ i ] = projection . screenProjection ( coord [ i ] ) ; } drawLine ( sc ) ; } | Draw poly line . The coordinates are in logical coordinates . |
11,060 | public void drawLineBaseRatio ( double [ ] ... coord ) { int [ ] [ ] sc = new int [ coord . length ] [ ] ; for ( int i = 0 ; i < sc . length ; i ++ ) { sc [ i ] = projection . screenProjectionBaseRatio ( coord [ i ] ) ; } drawLine ( sc ) ; } | Draw poly line . The logical coordinates are proportional to the base coordinates . |
11,061 | public void drawPoint ( char dot , double ... coord ) { int size = 2 ; int midSize = 3 ; int bigSize = 4 ; int [ ] sc = projection . screenProjection ( coord ) ; int x = sc [ 0 ] ; int y = sc [ 1 ] ; switch ( dot ) { case '+' : g2d . drawLine ( x - size , y , x + size , y ) ; g2d . drawLine ( x , y - size , x , y + size ) ; break ; case '-' : g2d . drawLine ( x - size , y , x + size , y ) ; break ; case '|' : g2d . drawLine ( x , y - size , x , y + size ) ; break ; case 'x' : g2d . drawLine ( x - size , y - size , x + size , y + size ) ; g2d . drawLine ( x + size , y - size , x - size , y + size ) ; break ; case '*' : g2d . drawLine ( x - bigSize , y , x + bigSize , y ) ; g2d . drawLine ( x , y - bigSize , x , y + bigSize ) ; g2d . drawLine ( x - midSize , y - midSize , x + midSize , y + midSize ) ; g2d . drawLine ( x + midSize , y - midSize , x - midSize , y + midSize ) ; break ; case 'o' : g2d . drawOval ( x - size , y - size , 2 * size , 2 * size ) ; break ; case 'O' : g2d . drawOval ( x - bigSize , y - bigSize , 2 * bigSize , 2 * bigSize ) ; break ; case '@' : g2d . fillOval ( x - size , y - size , 2 * size , 2 * size ) ; break ; case '#' : g2d . fillOval ( x - bigSize , y - bigSize , 2 * bigSize , 2 * bigSize ) ; break ; case 's' : g2d . drawRect ( x - size , y - size , 2 * size , 2 * size ) ; break ; case 'S' : g2d . drawRect ( x - bigSize , y - bigSize , 2 * bigSize , 2 * bigSize ) ; break ; case 'q' : g2d . fillRect ( x - size , y - size , 2 * size , 2 * size ) ; break ; case 'Q' : g2d . fillRect ( x - bigSize , y - bigSize , 2 * bigSize , 2 * bigSize ) ; break ; default : g2d . drawRect ( x , y , 1 , 1 ) ; break ; } } | Draw a dot with given pattern . The coordinates are in logical coordinates . |
11,062 | public void fillPolygon ( double [ ] ... coord ) { int [ ] [ ] c = new int [ coord . length ] [ 2 ] ; for ( int i = 0 ; i < coord . length ; i ++ ) { c [ i ] = projection . screenProjection ( coord [ i ] ) ; } int [ ] x = new int [ c . length ] ; for ( int i = 0 ; i < c . length ; i ++ ) { x [ i ] = c [ i ] [ 0 ] ; } int [ ] y = new int [ c . length ] ; for ( int i = 0 ; i < c . length ; i ++ ) { y [ i ] = c [ i ] [ 1 ] ; } g2d . fillPolygon ( x , y , c . length ) ; } | Fill polygon . The coordinates are in logical coordinates . |
11,063 | public void fillPolygon ( float alpha , double [ ] ... coord ) { int [ ] [ ] c = new int [ coord . length ] [ 2 ] ; for ( int i = 0 ; i < coord . length ; i ++ ) { c [ i ] = projection . screenProjection ( coord [ i ] ) ; } int [ ] x = new int [ c . length ] ; for ( int i = 0 ; i < c . length ; i ++ ) { x [ i ] = c [ i ] [ 0 ] ; } int [ ] y = new int [ c . length ] ; for ( int i = 0 ; i < c . length ; i ++ ) { y [ i ] = c [ i ] [ 1 ] ; } Composite cs = g2d . getComposite ( ) ; g2d . setComposite ( AlphaComposite . getInstance ( AlphaComposite . SRC_OVER , alpha ) ) ; g2d . fillPolygon ( x , y , c . length ) ; g2d . setComposite ( cs ) ; } | Fill polygon . The coordinates are in logical coordinates . This also supports basic alpha compositing rules for combining source and destination colors to achieve blending and transparency effects with graphics and images . |
11,064 | public void drawRect ( double [ ] topLeft , double [ ] rightBottom ) { if ( ! ( projection instanceof Projection2D ) ) { throw new UnsupportedOperationException ( "Only 2D graphics supports drawing rectangles." ) ; } int [ ] sc = projection . screenProjection ( topLeft ) ; int [ ] sc2 = projection . screenProjection ( rightBottom ) ; g2d . drawRect ( sc [ 0 ] , sc [ 1 ] , sc2 [ 0 ] - sc [ 0 ] , sc2 [ 1 ] - sc [ 1 ] ) ; } | Draw the outline of the specified rectangle . |
11,065 | public void drawRectBaseRatio ( double [ ] topLeft , double [ ] rightBottom ) { if ( ! ( projection instanceof Projection2D ) ) { throw new UnsupportedOperationException ( "Only 2D graphics supports drawing rectangles." ) ; } int [ ] sc = projection . screenProjectionBaseRatio ( topLeft ) ; int [ ] sc2 = projection . screenProjectionBaseRatio ( rightBottom ) ; g2d . drawRect ( sc [ 0 ] , sc [ 1 ] , sc2 [ 0 ] - sc [ 0 ] , sc2 [ 1 ] - sc [ 1 ] ) ; } | Draw the outline of the specified rectangle . The logical coordinates are proportional to the base coordinates . |
11,066 | public void fillRectBaseRatio ( double [ ] topLeft , double [ ] rightBottom ) { if ( ! ( projection instanceof Projection2D ) ) { throw new UnsupportedOperationException ( "Only 2D graphics supports drawing rectangles." ) ; } int [ ] sc = projection . screenProjectionBaseRatio ( topLeft ) ; int [ ] sc2 = projection . screenProjectionBaseRatio ( rightBottom ) ; g2d . fillRect ( sc [ 0 ] , sc [ 1 ] , sc2 [ 0 ] - sc [ 0 ] , sc2 [ 1 ] - sc [ 1 ] ) ; } | Fill the specified rectangle . The logical coordinates are proportional to the base coordinates . |
11,067 | public void rotate ( double x , double y ) { if ( ! ( projection instanceof Projection3D ) ) { throw new UnsupportedOperationException ( "Only 3D graphics supports rotation." ) ; } ( ( Projection3D ) projection ) . rotate ( x , y ) ; } | Rotate the 3D view based on the changes on mouse position . |
11,068 | public double learn ( double [ ] x ) { if ( x . length != n ) { throw new IllegalArgumentException ( String . format ( "Invalid input vector size: %d, expected: %d" , x . length , n ) ) ; } projection . ax ( x , y ) ; for ( int j = 0 ; j < p ; j ++ ) { for ( int i = 0 ; i < n ; i ++ ) { double delta = x [ i ] ; for ( int l = 0 ; l <= j ; l ++ ) { delta -= projection . get ( l , i ) * y [ l ] ; } projection . add ( j , i , r * y [ j ] * delta ) ; if ( Double . isInfinite ( projection . get ( j , i ) ) ) { throw new IllegalStateException ( "GHA lost convergence. Lower learning rate?" ) ; } } } projection . ax ( x , y ) ; projection . atx ( y , wy ) ; return Math . squaredDistance ( x , wy ) ; } | Update the model with a new sample . |
11,069 | private static RadialBasisFunction [ ] rep ( RadialBasisFunction rbf , int k ) { RadialBasisFunction [ ] arr = new RadialBasisFunction [ k ] ; Arrays . fill ( arr , rbf ) ; return arr ; } | Returns an array of radial basis functions initialized with given values . |
11,070 | public DenseMatrix getS ( ) { DenseMatrix S = Matrix . zeros ( U . nrows ( ) , V . nrows ( ) ) ; for ( int i = 0 ; i < s . length ; i ++ ) { S . set ( i , i , s [ i ] ) ; } return S ; } | Returns the diagonal matrix of singular values |
11,071 | public int rank ( ) { if ( ! full ) { throw new IllegalStateException ( "This is not a FULL singular value decomposition." ) ; } int r = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] > tol ) { r ++ ; } } return r ; } | Returns the effective numerical matrix rank . The number of nonnegligible singular values . |
11,072 | public int nullity ( ) { if ( ! full ) { throw new IllegalStateException ( "This is not a FULL singular value decomposition." ) ; } int r = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] <= tol ) { r ++ ; } } return r ; } | Returns the dimension of null space . The number of negligible singular values . |
11,073 | public DenseMatrix range ( ) { if ( ! full ) { throw new IllegalStateException ( "This is not a FULL singular value decomposition." ) ; } int nr = 0 ; DenseMatrix rnge = Matrix . zeros ( m , rank ( ) ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] > tol ) { for ( int i = 0 ; i < m ; i ++ ) { rnge . set ( i , nr , U . get ( i , j ) ) ; } nr ++ ; } } return rnge ; } | Returns a matrix of which columns give an orthonormal basis for the range space . |
11,074 | public DenseMatrix nullspace ( ) { if ( ! full ) { throw new IllegalStateException ( "This is not a FULL singular value decomposition." ) ; } int nn = 0 ; DenseMatrix nullsp = Matrix . zeros ( n , nullity ( ) ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] <= tol ) { for ( int jj = 0 ; jj < n ; jj ++ ) { nullsp . set ( jj , nn , V . get ( jj , j ) ) ; } nn ++ ; } } return nullsp ; } | Returns a matrix of which columns give an orthonormal basis for the null space . |
11,075 | public void ensureCapacity ( int capacity ) { if ( capacity > data . length ) { int newCap = Math . max ( data . length << 1 , capacity ) ; int [ ] tmp = new int [ newCap ] ; System . arraycopy ( data , 0 , tmp , 0 , data . length ) ; data = tmp ; } } | Increases the capacity if necessary to ensure that it can hold at least the number of values specified by the minimum capacity argument . |
11,076 | public IntArrayList set ( int index , int val ) { if ( index < 0 || index >= size ) { throw new IndexOutOfBoundsException ( String . valueOf ( index ) ) ; } data [ index ] = val ; return this ; } | Replaces the value at the specified position in this list with the specified value . |
11,077 | public int remove ( int index ) { if ( index < 0 || index >= size ) { throw new IndexOutOfBoundsException ( String . valueOf ( index ) ) ; } int old = get ( index ) ; if ( index == 0 ) { System . arraycopy ( data , 1 , data , 0 , size - 1 ) ; } else if ( size - 1 == index ) { } else { System . arraycopy ( data , index + 1 , data , index , size - ( index + 1 ) ) ; } size -- ; return old ; } | Removes the value at specified index from the list . |
11,078 | private double rdist ( double [ ] x1 , double [ ] x2 ) { double d = 0.0 ; for ( int i = 0 ; i < x1 . length ; i ++ ) { double t = x1 [ i ] - x2 [ i ] ; d += t * t ; } return Math . sqrt ( d ) ; } | Cartesian distance . |
11,079 | public static double [ ] [ ] histogram ( int [ ] data , double [ ] breaks ) { int k = breaks . length - 1 ; if ( k <= 1 ) { throw new IllegalArgumentException ( "Invalid number of bins: " + k ) ; } double [ ] [ ] freq = new double [ 3 ] [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { freq [ 0 ] [ i ] = breaks [ i ] ; freq [ 1 ] [ i ] = breaks [ i + 1 ] ; freq [ 2 ] [ i ] = 0 ; } for ( int d : data ) { int j = Arrays . binarySearch ( breaks , d ) ; if ( j >= k ) { j = k - 1 ; } if ( j < - 1 && j >= - breaks . length ) { j = - j - 2 ; } if ( j >= 0 ) { freq [ 2 ] [ j ] ++ ; } } return freq ; } | Generate the histogram of n bins . |
11,080 | public static double [ ] breaks ( double [ ] x , double h ) { return breaks ( Math . min ( x ) , Math . max ( x ) , h ) ; } | Returns the breakpoints between histogram cells for a dataset based on a suggested bin width h . |
11,081 | public static double [ ] breaks ( double min , double max , double h ) { if ( h <= 0.0 ) { throw new IllegalArgumentException ( "Invalid bin width: " + h ) ; } if ( min > max ) { throw new IllegalArgumentException ( "Invalid lower and upper bounds: " + min + " > " + max ) ; } int k = ( int ) Math . ceil ( ( max - min ) / h ) ; double [ ] breaks = new double [ k + 1 ] ; breaks [ 0 ] = min - ( h * k - ( max - min ) ) / 2 ; breaks [ k ] = max + ( h * k - ( max - min ) ) / 2 ; for ( int i = 1 ; i < k ; i ++ ) { breaks [ i ] = breaks [ i - 1 ] + h ; } return breaks ; } | Returns the breakpoints between histogram cells for a given range based on a suggested bin width h . |
11,082 | public static double [ ] breaks ( double [ ] x , int k ) { return breaks ( Math . min ( x ) , Math . max ( x ) , k ) ; } | Returns the breakpoints between histogram cells for a dataset . |
11,083 | public static double [ ] breaks ( double min , double max , int k ) { if ( k <= 1 ) { throw new IllegalArgumentException ( "Invalid number of bins: " + k ) ; } if ( min > max ) { throw new IllegalArgumentException ( "Invalid lower and upper bounds: " + min + " > " + max ) ; } double h = ( max - min ) / k ; return breaks ( min , max , h ) ; } | Returns the breakpoints between histogram cells for a given range . |
11,084 | public static int bins ( double [ ] x , double h ) { if ( h <= 0.0 ) { throw new IllegalArgumentException ( "Invalid bin width: " + h ) ; } double max = Math . max ( x ) ; double min = Math . min ( x ) ; return ( int ) Math . ceil ( ( max - min ) / h ) ; } | Returns the number of bins for a data based on a suggested bin width h . |
11,085 | public BitString [ ] learn ( int size , int generation , ClassifierTrainer < double [ ] > trainer , ClassificationMeasure measure , double [ ] [ ] x , int [ ] y , int k ) { if ( size <= 0 ) { throw new IllegalArgumentException ( "Invalid population size: " + size ) ; } if ( k < 2 ) { throw new IllegalArgumentException ( "Invalid k-fold cross validation: " + k ) ; } if ( x . length != y . length ) { throw new IllegalArgumentException ( String . format ( "The sizes of X and Y don't match: %d != %d" , x . length , y . length ) ) ; } int p = x [ 0 ] . length ; ClassificationFitness fitness = new ClassificationFitness ( trainer , measure , x , y , k ) ; BitString [ ] seeds = new BitString [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { seeds [ i ] = new BitString ( p , fitness , crossover , crossoverRate , mutationRate ) ; } GeneticAlgorithm < BitString > ga = new GeneticAlgorithm < > ( seeds , selection ) ; ga . evolve ( generation ) ; return seeds ; } | Genetic algorithm based feature selection for classification . |
11,086 | public double valueOf ( String s ) throws ParseException { Integer i = map . get ( s ) ; if ( i == null ) { if ( open ) { i = values . size ( ) ; map . put ( s , i ) ; values . add ( s ) ; } else { throw new ParseException ( "Invalid string value: " + s , 0 ) ; } } return i ; } | Returns the ordinal value of a string value . |
11,087 | private void initBaseCoordsProjection ( ) { baseScreenCoords = new int [ canvas . base . baseCoords . length ] [ 2 ] ; for ( int i = 0 ; i < canvas . base . dimension + 1 ; i ++ ) { double [ ] ratio = baseCoordsScreenProjectionRatio ( canvas . base . baseCoords [ i ] ) ; baseScreenCoords [ i ] [ 0 ] = ( int ) ( canvas . getWidth ( ) * ( canvas . margin + ( 1 - 2 * canvas . margin ) * ratio [ 0 ] ) ) ; baseScreenCoords [ i ] [ 1 ] = ( int ) ( canvas . getHeight ( ) - canvas . getHeight ( ) * ( canvas . margin + ( 1 - 2 * canvas . margin ) * ratio [ 1 ] ) ) ; } } | Initialize base coordinates on Java2D screen . |
11,088 | public int [ ] screenProjection ( double ... coord ) { double [ ] sc = new double [ 2 ] ; sc [ 0 ] = baseScreenCoords [ 0 ] [ 0 ] ; sc [ 1 ] = baseScreenCoords [ 0 ] [ 1 ] ; for ( int i = 0 ; i < canvas . base . dimension ; i ++ ) { sc [ 0 ] += ( ( coord [ i ] - canvas . base . baseCoords [ 0 ] [ i ] ) / ( canvas . base . baseCoords [ i + 1 ] [ i ] - canvas . base . baseCoords [ 0 ] [ i ] ) ) * ( baseScreenCoords [ i + 1 ] [ 0 ] - baseScreenCoords [ 0 ] [ 0 ] ) ; sc [ 1 ] += ( ( coord [ i ] - canvas . base . baseCoords [ 0 ] [ i ] ) / ( canvas . base . baseCoords [ i + 1 ] [ i ] - canvas . base . baseCoords [ 0 ] [ i ] ) ) * ( baseScreenCoords [ i + 1 ] [ 1 ] - baseScreenCoords [ 0 ] [ 1 ] ) ; } return new int [ ] { ( int ) sc [ 0 ] , ( int ) sc [ 1 ] } ; } | Project logical coordinates to Java2D coordinates . |
11,089 | public int [ ] screenProjectionBaseRatio ( double ... coord ) { double [ ] sc = new double [ 2 ] ; sc [ 0 ] = baseScreenCoords [ 0 ] [ 0 ] ; sc [ 1 ] = baseScreenCoords [ 0 ] [ 1 ] ; for ( int i = 0 ; i < canvas . base . dimension ; i ++ ) { sc [ 0 ] += coord [ i ] * ( baseScreenCoords [ i + 1 ] [ 0 ] - baseScreenCoords [ 0 ] [ 0 ] ) ; sc [ 1 ] += coord [ i ] * ( baseScreenCoords [ i + 1 ] [ 1 ] - baseScreenCoords [ 0 ] [ 1 ] ) ; } return new int [ ] { ( int ) sc [ 0 ] , ( int ) sc [ 1 ] } ; } | Project logical coordinates in base ratio to Java2D coordinates . |
11,090 | public double logLikelihood ( double [ ] x ) { double L = 0.0 ; for ( double xi : x ) L += logp ( xi ) ; return L ; } | The likelihood given a sample set following the distribution . |
11,091 | private static void createThreadPool ( ) { if ( nprocs == - 1 ) { int n = - 1 ; try { String env = System . getProperty ( "smile.threads" ) ; if ( env != null ) { n = Integer . parseInt ( env ) ; } } catch ( Exception ex ) { logger . error ( "Failed to create multi-core execution thread pool" , ex ) ; } if ( n < 1 ) { nprocs = Runtime . getRuntime ( ) . availableProcessors ( ) ; } else { nprocs = n ; } if ( nprocs > 1 ) { threads = ( ThreadPoolExecutor ) Executors . newFixedThreadPool ( nprocs , new SimpleDeamonThreadFactory ( ) ) ; } } } | Creates the worker thread pool . |
11,092 | public static < T > List < T > run ( Collection < ? extends Callable < T > > tasks ) throws Exception { createThreadPool ( ) ; List < T > results = new ArrayList < > ( ) ; if ( threads == null ) { for ( Callable < T > task : tasks ) { results . add ( task . call ( ) ) ; } } else { if ( threads . getActiveCount ( ) < nprocs ) { List < Future < T > > futures = threads . invokeAll ( tasks ) ; for ( Future < T > future : futures ) { results . add ( future . get ( ) ) ; } } else { for ( Callable < T > task : tasks ) { results . add ( task . call ( ) ) ; } } } return results ; } | Executes the given tasks serially or parallel depending on the number of cores of the system . Returns a list of result objects of each task . The results of this method are undefined if the given collection is modified while this operation is in progress . |
11,093 | public static PlotCanvas plot ( String id , double [ ] data ) { Histogram histogram = new Histogram ( data ) ; histogram . setID ( id ) ; double [ ] lowerBound = { Math . min ( data ) , 0 } ; double [ ] upperBound = { Math . max ( data ) , 0 } ; double [ ] [ ] freq = histogram . getHistogram ( ) ; for ( int i = 0 ; i < freq . length ; i ++ ) { if ( freq [ i ] [ 1 ] > upperBound [ 1 ] ) { upperBound [ 1 ] = freq [ i ] [ 1 ] ; } } PlotCanvas canvas = new PlotCanvas ( lowerBound , upperBound ) ; canvas . getAxis ( 0 ) . setGridVisible ( false ) ; canvas . add ( histogram ) ; return canvas ; } | Create a plot canvas with the histogram plot . |
11,094 | TotalSupportTree buildTotalSupportTree ( ) { TotalSupportTree ttree = new TotalSupportTree ( minSupport , T0 . numFreqItems , T0 . order ) ; learn ( null , null , ttree ) ; return ttree ; } | Mines the frequent item sets . The discovered frequent item sets will be stored in a total support tree . |
11,095 | private long learn ( PrintStream out , List < ItemSet > list , TotalSupportTree ttree ) { if ( MulticoreExecutor . getThreadPoolSize ( ) > 1 ) { return grow ( out , list , ttree , T0 , null , null , null ) ; } else { return grow ( out , list , ttree , T0 , null ) ; } } | Mines the frequent item sets . The discovered frequent item sets will be printed out to the provided stream . |
11,096 | private void collect ( PrintStream out , List < ItemSet > list , TotalSupportTree ttree , int [ ] itemset , int support ) { if ( list != null ) { synchronized ( list ) { list . add ( new ItemSet ( itemset , support ) ) ; } } if ( out != null ) { synchronized ( out ) { for ( int i = 0 ; i < itemset . length ; i ++ ) { out . format ( "%d " , itemset [ i ] ) ; } out . format ( "(%d)%n" , support ) ; } } if ( ttree != null ) { synchronized ( ttree ) { ttree . add ( itemset , support ) ; } } } | Adds an item set to the result . |
11,097 | private long grow ( PrintStream out , List < ItemSet > list , TotalSupportTree ttree , FPTree . Node node , int [ ] itemset , int support ) { int height = 0 ; for ( FPTree . Node currentNode = node ; currentNode != null ; currentNode = currentNode . parent ) { height ++ ; } int n = 0 ; if ( height > 0 ) { int [ ] items = new int [ height ] ; int i = 0 ; for ( FPTree . Node currentNode = node ; currentNode != null ; currentNode = currentNode . parent ) { items [ i ++ ] = currentNode . id ; } int [ ] itemIndexStack = new int [ height ] ; int itemIndexStackPos = 0 ; itemset = insert ( itemset , items [ itemIndexStack [ itemIndexStackPos ] ] ) ; collect ( out , list , ttree , itemset , support ) ; n ++ ; while ( itemIndexStack [ 0 ] < height - 1 ) { if ( itemIndexStack [ itemIndexStackPos ] < height - 1 ) { itemIndexStackPos ++ ; itemIndexStack [ itemIndexStackPos ] = itemIndexStack [ itemIndexStackPos - 1 ] + 1 ; itemset = insert ( itemset , items [ itemIndexStack [ itemIndexStackPos ] ] ) ; collect ( out , list , ttree , itemset , support ) ; n ++ ; } else { itemset = drop ( itemset ) ; if ( itemset != null ) { itemIndexStackPos -- ; itemIndexStack [ itemIndexStackPos ] = itemIndexStack [ itemIndexStackPos ] + 1 ; itemset [ 0 ] = items [ itemIndexStack [ itemIndexStackPos ] ] ; collect ( out , list , ttree , itemset , support ) ; n ++ ; } } } } return n ; } | Mines all combinations along a single path tree |
11,098 | private long grow ( PrintStream out , List < ItemSet > list , TotalSupportTree ttree , HeaderTableItem header , int [ ] itemset , int [ ] localItemSupport , int [ ] prefixItemset ) { long n = 1 ; int support = header . count ; int item = header . id ; itemset = insert ( itemset , item ) ; collect ( out , list , ttree , itemset , support ) ; if ( header . node . next == null ) { FPTree . Node node = header . node ; n += grow ( out , list , ttree , node . parent , itemset , support ) ; } else { if ( getLocalItemSupport ( header . node , localItemSupport ) ) { FPTree fptree = getLocalFPTree ( header . node , localItemSupport , prefixItemset ) ; n += grow ( out , list , ttree , fptree , itemset , localItemSupport , prefixItemset ) ; } } return n ; } | Mines FP - tree with respect to a single element in the header table . |
11,099 | private boolean getLocalItemSupport ( FPTree . Node node , int [ ] localItemSupport ) { boolean end = true ; Arrays . fill ( localItemSupport , 0 ) ; while ( node != null ) { int support = node . count ; Node parent = node . parent ; while ( parent != null ) { localItemSupport [ parent . id ] += support ; parent = parent . parent ; end = false ; } node = node . next ; } return ! end ; } | Counts the supports of single items in ancestor item sets linked list . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.