idx int64 0 41.2k | question stringlengths 73 5.81k | target stringlengths 5 918 |
|---|---|---|
11,100 | private FPTree getLocalFPTree ( FPTree . Node node , int [ ] localItemSupport , int [ ] prefixItemset ) { FPTree tree = new FPTree ( localItemSupport , minSupport ) ; while ( node != null ) { Node parent = node . parent ; int i = prefixItemset . length ; while ( parent != null ) { if ( localItemSupport [ parent . id ] >= minSupport ) { prefixItemset [ -- i ] = parent . id ; } parent = parent . parent ; } if ( i < prefixItemset . length ) { tree . add ( i , prefixItemset . length , prefixItemset , node . count ) ; } node = node . next ; } return tree ; } | Generates a local FP tree |
11,101 | static int [ ] insert ( int [ ] itemset , int item ) { if ( itemset == null ) { int [ ] newItemset = { item } ; return newItemset ; } else { int n = itemset . length + 1 ; int [ ] newItemset = new int [ n ] ; newItemset [ 0 ] = item ; System . arraycopy ( itemset , 0 , newItemset , 1 , n - 1 ) ; return newItemset ; } } | Insert a item to the front of an item set . |
11,102 | static int [ ] drop ( int [ ] itemset ) { if ( itemset . length >= 1 ) { int n = itemset . length - 1 ; int [ ] newItemset = new int [ n ] ; System . arraycopy ( itemset , 1 , newItemset , 0 , n ) ; return newItemset ; } else { return null ; } } | Drops an item form the front of an item set . |
11,103 | private void setSlice ( ) { if ( labels == null ) { double min = base . getPrecisionUnit ( ) [ index ] * Math . ceil ( base . getLowerBounds ( ) [ index ] / base . getPrecisionUnit ( ) [ index ] ) ; double max = base . getPrecisionUnit ( ) [ index ] * Math . floor ( base . getUpperBounds ( ) [ index ] / base . getPrecisionUnit ( ) [ index ] ) ; linearSlices = ( int ) Math . ceil ( Math . round ( ( max - min ) / base . getPrecisionUnit ( ) [ index ] , 1 ) ) ; if ( linearSlices <= 0 ) { linearSlices = 1 ; } if ( linearSlices < 3 ) { linearSlices *= 2 ; } linesSlicing = new double [ linearSlices + 3 ] ; labelsSlicing = new double [ linearSlices + 3 ] ; double pitch = ( max - min ) / linearSlices ; for ( int i = 1 ; i <= linearSlices + 1 ; i ++ ) { linesSlicing [ i ] = min + ( i - 1 ) * pitch ; labelsSlicing [ i ] = min + ( i - 1 ) * pitch ; } linesSlicing [ 0 ] = base . getLowerBounds ( ) [ index ] ; labelsSlicing [ 0 ] = base . getLowerBounds ( ) [ index ] ; linesSlicing [ linearSlices + 2 ] = base . getUpperBounds ( ) [ index ] ; labelsSlicing [ linearSlices + 2 ] = base . getUpperBounds ( ) [ index ] ; } else { linesSlicing = new double [ labels . size ( ) + 2 ] ; labelsSlicing = new double [ labels . size ( ) ] ; gridLabelStrings = new String [ labels . size ( ) ] ; linesSlicing [ 0 ] = base . getLowerBounds ( ) [ index ] ; int i = 1 ; for ( String string : labels . keySet ( ) ) { linesSlicing [ i ] = labels . get ( string ) ; labelsSlicing [ i - 1 ] = labels . get ( string ) ; gridLabelStrings [ i - 1 ] = string ; i ++ ; } linesSlicing [ i ] = base . getUpperBounds ( ) [ index ] ; Arrays . sort ( linesSlicing ) ; QuickSort . sort ( labelsSlicing , gridLabelStrings ) ; } } | Set the slices of axis . |
11,104 | private void initGridLines ( ) { gridLines = new Line [ base . getDimension ( ) - 1 ] [ linesSlicing . length ] ; int i2 = 0 ; for ( int i = 0 ; i < base . getDimension ( ) - 1 ; i ++ ) { if ( i2 == index ) { i2 ++ ; } for ( int j = 0 ; j < gridLines [ i ] . length ; j ++ ) { double [ ] originBase = new double [ base . getDimension ( ) ] ; double [ ] endBase = new double [ base . getDimension ( ) ] ; System . arraycopy ( origin , 0 , originBase , 0 , base . getDimension ( ) ) ; System . arraycopy ( origin , 0 , endBase , 0 , base . getDimension ( ) ) ; endBase [ i2 ] = base . getCoordinateSpace ( ) [ i2 + 1 ] [ i2 ] ; originBase [ index ] = linesSlicing [ j ] ; endBase [ index ] = linesSlicing [ j ] ; if ( j == 0 || j == gridLines [ i ] . length - 1 ) { gridLines [ i ] [ j ] = new Line ( originBase , endBase ) ; } else { gridLines [ i ] [ j ] = new Line ( Line . Style . DOT , Color . lightGray , originBase , endBase ) ; } } i2 ++ ; } } | Initialize grid lines . |
11,105 | public Axis removeLabel ( String label ) { if ( labels == null ) { throw new IllegalStateException ( ) ; } labels . remove ( label ) ; setSlice ( ) ; initGridLabels ( ) ; return this ; } | Remove a label from the axis . |
11,106 | public Axis setAxisLabel ( String label ) { if ( label == null ) { if ( index == 0 ) { label = "X" ; } else if ( index == 1 ) { label = "Y" ; } else if ( index == 2 ) { label = "Z" ; } } if ( label != null ) { double [ ] position = new double [ base . getDimension ( ) ] ; if ( base . getDimension ( ) == 2 ) { position [ index ] = 0.5 ; if ( index == 0 ) { position [ 1 ] = - 0.1 ; axisLabel = new BaseLabel ( label , 0.5 , 1.0 , position ) ; } else { position [ 0 ] = - 0.15 ; axisLabel = new BaseLabel ( label , 0.5 , 0.5 , - Math . PI / 2 , position ) ; } } else { if ( index == 0 ) { position [ 2 ] = 1.0 ; position [ index ] = 0.5 ; axisLabel = new BaseLabel ( label , 0.5 , - 2.0 , position ) ; } else if ( index == 1 ) { position [ 0 ] = 1.0 ; position [ index ] = 0.5 ; axisLabel = new BaseLabel ( label , 0.5 , 3.0 , position ) ; } else if ( index == 2 ) { position [ 1 ] = 1.0 ; position [ index ] = 1.0 ; axisLabel = new BaseLabel ( label , - 0.5 , - 1.0 , position ) ; } } axisLabel . setFont ( axisLabelFont ) ; } return this ; } | Sets the label of this axis . |
11,107 | public void paint ( Graphics g ) { if ( gridLines != null ) { if ( gridVisible ) { for ( int i = 0 ; i < gridLines . length ; i ++ ) { for ( int j = 1 ; j < gridLines [ i ] . length - 1 ; j ++ ) { gridLines [ i ] [ j ] . paint ( g ) ; } } } if ( frameVisible ) { for ( int i = 0 ; i < gridLines . length ; i ++ ) { gridLines [ i ] [ 0 ] . paint ( g ) ; gridLines [ i ] [ gridLines [ i ] . length - 1 ] . paint ( g ) ; } } } if ( labelVisible ) { if ( gridLabels != null ) { int [ ] xy = g . projection . screenProjection ( gridLabels [ 1 ] . getCoordinate ( ) ) ; int prevx = xy [ 0 ] ; int prevy = xy [ 1 ] ; for ( int i = 0 ; i < gridLabels . length ; i ++ ) { if ( ! gridLabels [ i ] . text . isEmpty ( ) ) { double [ ] coord = gridLabels [ i ] . getCoordinate ( ) ; xy = g . projection . screenProjection ( coord ) ; int x = xy [ 0 ] ; int y = xy [ 1 ] ; if ( base . getDimension ( ) == 2 && index == 0 && rotation != 0.0 ) { if ( ( prevx == x && prevy == y ) || Math . abs ( x - prevx ) > gridLabels [ i ] . font . getSize ( ) ) { gridLabels [ i ] . paint ( g ) ; prevx = x ; prevy = y ; } } else if ( base . getDimension ( ) == 2 && index == 1 ) { if ( ( prevx == x && prevy == y && i == 0 ) || Math . abs ( prevy - y ) > gridLabels [ i ] . font . getSize ( ) ) { gridLabels [ i ] . paint ( g ) ; prevx = x ; prevy = y ; } } else { if ( ( prevx == x && prevy == y ) || Math . abs ( x - prevx ) > g . g2d . getFontMetrics ( gridLabels [ i ] . font ) . stringWidth ( gridLabels [ i ] . text ) || Math . abs ( prevy - y ) > gridLabels [ i ] . font . getSize ( ) ) { gridLabels [ i ] . paint ( g ) ; prevx = x ; prevy = y ; } } } } } if ( axisLabel != null ) { axisLabel . paint ( g ) ; } } } | Draw the axis . |
11,108 | private void singlePointCrossover ( BitString father , BitString mother , BitString [ ] offsprings ) { int point = 0 ; while ( point == 0 ) { point = Math . randomInt ( length ) ; } int [ ] son = new int [ length ] ; System . arraycopy ( father . bits , 0 , son , 0 , point ) ; System . arraycopy ( mother . bits , point , son , point , length - point ) ; int [ ] daughter = new int [ length ] ; System . arraycopy ( mother . bits , 0 , daughter , 0 , point ) ; System . arraycopy ( father . bits , point , daughter , point , length - point ) ; offsprings [ 0 ] = new BitString ( son , measure , crossover , crossoverRate , mutationRate ) ; offsprings [ 1 ] = new BitString ( daughter , measure , crossover , crossoverRate , mutationRate ) ; } | Single point crossover . |
11,109 | private void twoPointCrossover ( BitString father , BitString mother , BitString [ ] offsprings ) { int point1 = 0 ; while ( point1 == 0 || point1 == length - 1 ) { point1 = Math . randomInt ( length ) ; } int point2 = 0 ; while ( point2 == point1 || point2 == 0 || point2 == length - 1 ) { point2 = Math . randomInt ( length ) ; } if ( point2 < point1 ) { int p = point1 ; point1 = point2 ; point2 = p ; } int [ ] son = new int [ length ] ; System . arraycopy ( father . bits , 0 , son , 0 , point1 ) ; System . arraycopy ( mother . bits , point1 , son , point1 , point2 - point1 ) ; System . arraycopy ( father . bits , point2 , son , point2 , length - point2 ) ; int [ ] daughter = new int [ length ] ; System . arraycopy ( mother . bits , 0 , daughter , 0 , point1 ) ; System . arraycopy ( father . bits , point1 , daughter , point1 , point2 - point1 ) ; System . arraycopy ( mother . bits , point2 , daughter , point2 , length - point2 ) ; offsprings [ 0 ] = new BitString ( son , measure , crossover , crossoverRate , mutationRate ) ; offsprings [ 1 ] = new BitString ( daughter , measure , crossover , crossoverRate , mutationRate ) ; } | Two point crossover . |
11,110 | private void uniformCrossover ( BitString father , BitString mother , BitString [ ] offsprings ) { int [ ] son = new int [ length ] ; int [ ] daughter = new int [ length ] ; for ( int i = 0 ; i < length ; i ++ ) { if ( Math . random ( ) < 0.5 ) { son [ i ] = father . bits [ i ] ; daughter [ i ] = mother . bits [ i ] ; } else { son [ i ] = mother . bits [ i ] ; daughter [ i ] = father . bits [ i ] ; } } offsprings [ 0 ] = new BitString ( son , measure , crossover , crossoverRate , mutationRate ) ; offsprings [ 1 ] = new BitString ( daughter , measure , crossover , crossoverRate , mutationRate ) ; } | Uniform crossover . |
11,111 | public PCA setProjection ( int p ) { if ( p < 1 || p > n ) { throw new IllegalArgumentException ( "Invalid dimension of feature space: " + p ) ; } this . p = p ; projection = Matrix . zeros ( p , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < p ; j ++ ) { projection . set ( j , i , eigvectors . get ( i , j ) ) ; } } pmu = new double [ p ] ; projection . ax ( mu , pmu ) ; return this ; } | Set the projection matrix with given number of principal components . |
11,112 | @ SuppressWarnings ( "rawtypes" ) protected SortKey getSortKey ( JTable table , int column ) { RowSorter rowSorter = table . getRowSorter ( ) ; if ( rowSorter == null ) { return null ; } List sortedColumns = rowSorter . getSortKeys ( ) ; if ( ! sortedColumns . isEmpty ( ) ) { return ( SortKey ) sortedColumns . get ( 0 ) ; } return null ; } | Returns the current sort key or null if the column is unsorted . |
11,113 | private void organize ( ) { int m = ( int ) Math . sqrt ( contentPane . getComponentCount ( ) ) ; if ( m <= 0 ) m = 1 ; contentPane . setLayout ( new GridLayout ( m , 0 , 0 , 0 ) ) ; } | Reorganize the plots in the frame . Basically it reset the surface layout based on the number of plots in the frame . |
11,114 | private void initToolBar ( ) { toolbar = new JToolBar ( JToolBar . VERTICAL ) ; toolbar . setFloatable ( false ) ; add ( toolbar , BorderLayout . WEST ) ; JButton button = makeButton ( "save" , SAVE , "Save" , "Save" ) ; toolbar . add ( button ) ; button = makeButton ( "print" , PRINT , "Print" , "Print" ) ; toolbar . add ( button ) ; } | Initialize toolbar . |
11,115 | private JButton makeButton ( String imageName , String actionCommand , String toolTipText , String altText ) { String imgLocation = "images/" + imageName + "16.png" ; URL imageURL = PlotCanvas . class . getResource ( imgLocation ) ; JButton button = new JButton ( ) ; button . setActionCommand ( actionCommand ) ; button . setToolTipText ( toolTipText ) ; button . addActionListener ( this ) ; if ( imageURL != null ) { button . setIcon ( new ImageIcon ( imageURL , altText ) ) ; } else { button . setText ( altText ) ; logger . error ( "Resource not found: {}" , imgLocation ) ; } return button ; } | Creates a button for toolbar . |
11,116 | public static int [ ] [ ] sort ( Attribute [ ] attributes , double [ ] [ ] x ) { int n = x . length ; int p = x [ 0 ] . length ; double [ ] a = new double [ n ] ; int [ ] [ ] index = new int [ p ] [ ] ; for ( int j = 0 ; j < p ; j ++ ) { if ( attributes [ j ] . getType ( ) == Attribute . Type . NUMERIC ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = x [ i ] [ j ] ; } index [ j ] = QuickSort . sort ( a ) ; } } return index ; } | Sorts each variable and returns the index of values in ascending order . Only numeric attributes will be sorted . Note that the order of original array is NOT altered . |
11,117 | public void impute ( double [ ] [ ] data , int maxIter ) throws MissingValueImputationException { if ( maxIter < 1 ) { throw new IllegalArgumentException ( "Invalid maximum number of iterations: " + maxIter ) ; } int [ ] count = new int [ data [ 0 ] . length ] ; for ( int i = 0 ; i < data . length ; i ++ ) { int n = 0 ; for ( int j = 0 ; j < data [ i ] . length ; j ++ ) { if ( Double . isNaN ( data [ i ] [ j ] ) ) { n ++ ; count [ j ] ++ ; } } if ( n == data [ i ] . length ) { throw new MissingValueImputationException ( "The whole row " + i + " is missing" ) ; } } for ( int i = 0 ; i < data [ 0 ] . length ; i ++ ) { if ( count [ i ] == data . length ) { throw new MissingValueImputationException ( "The whole column " + i + " is missing" ) ; } } double [ ] [ ] full = new double [ data . length ] [ ] ; for ( int i = 0 ; i < full . length ; i ++ ) { full [ i ] = data [ i ] . clone ( ) ; } KMeansImputation . columnAverageImpute ( full ) ; for ( int iter = 0 ; iter < maxIter ; iter ++ ) { svdImpute ( data , full ) ; } for ( int i = 0 ; i < data . length ; i ++ ) { System . arraycopy ( full [ i ] , 0 , data [ i ] , 0 , data [ i ] . length ) ; } } | Impute missing values in the dataset . |
11,118 | private void svdImpute ( double [ ] [ ] raw , double [ ] [ ] data ) { SVD svd = Matrix . newInstance ( data ) . svd ( ) ; int d = data [ 0 ] . length ; for ( int i = 0 ; i < raw . length ; i ++ ) { int missing = 0 ; for ( int j = 0 ; j < d ; j ++ ) { if ( Double . isNaN ( raw [ i ] [ j ] ) ) { missing ++ ; } else { data [ i ] [ j ] = raw [ i ] [ j ] ; } } if ( missing == 0 ) { continue ; } DenseMatrix A = Matrix . zeros ( d - missing , k ) ; double [ ] b = new double [ d - missing ] ; for ( int j = 0 , m = 0 ; j < d ; j ++ ) { if ( ! Double . isNaN ( raw [ i ] [ j ] ) ) { for ( int l = 0 ; l < k ; l ++ ) { A . set ( m , l , svd . getV ( ) . get ( j , l ) ) ; } b [ m ++ ] = raw [ i ] [ j ] ; } } double [ ] s = new double [ k ] ; QR qr = A . qr ( ) ; qr . solve ( b , s ) ; for ( int j = 0 ; j < d ; j ++ ) { if ( Double . isNaN ( raw [ i ] [ j ] ) ) { data [ i ] [ j ] = 0 ; for ( int l = 0 ; l < k ; l ++ ) { data [ i ] [ j ] += s [ l ] * svd . getV ( ) . get ( j , l ) ; } } } } } | Impute the missing values by SVD . |
11,119 | private static double bcuint ( double [ ] y , double [ ] y1 , double [ ] y2 , double [ ] y12 , double x1l , double x1u , double x2l , double x2u , double x1p , double x2p ) { if ( x1u == x1l ) { throw new IllegalArgumentException ( "Nearby control points take same value: " + x1u ) ; } if ( x2u == x2l ) { throw new IllegalArgumentException ( "Nearby control points take same value: " + x2u ) ; } double t , u , d1 = x1u - x1l , d2 = x2u - x2l ; double [ ] [ ] c = bcucof ( y , y1 , y2 , y12 , d1 , d2 ) ; t = ( x1p - x1l ) / d1 ; u = ( x2p - x2l ) / d2 ; double ansy = 0.0 ; for ( int i = 3 ; i >= 0 ; i -- ) { ansy = t * ansy + ( ( c [ i ] [ 3 ] * u + c [ i ] [ 2 ] ) * u + c [ i ] [ 1 ] ) * u + c [ i ] [ 0 ] ; } return ansy ; } | Bicubic interpolation within a grid square . |
11,120 | public static PlotCanvas plot ( double [ ] [ ] vertices , int [ ] [ ] edges ) { double [ ] lowerBound = Math . colMin ( vertices ) ; double [ ] upperBound = Math . colMax ( vertices ) ; PlotCanvas canvas = new PlotCanvas ( lowerBound , upperBound ) ; Wireframe frame = new Wireframe ( vertices , edges ) ; canvas . add ( frame ) ; return canvas ; } | Create a wire frame plot canvas . |
11,121 | public int [ ] feature ( double [ ] x ) { if ( x . length != attributes . length ) { throw new IllegalArgumentException ( String . format ( "Invalid feature vector size %d, expected %d" , x . length , attributes . length ) ) ; } int [ ] features = new int [ attributes . length ] ; for ( int i = 0 ; i < features . length ; i ++ ) { int f = ( int ) x [ i ] ; if ( Math . floor ( x [ i ] ) != x [ i ] || f < 0 || f >= attributes [ i ] . size ( ) ) { throw new IllegalArgumentException ( String . format ( "Invalid value of attribute %s: %d" , attributes [ i ] . toString ( ) , f ) ) ; } features [ i ] = f + base [ i ] ; } return features ; } | Generates the compact representation of sparse binary features for given object . |
11,122 | public String format ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . substring ( i , i + 1 ) . equals ( "*" ) && i > 0 ) if ( isOperand ( s . substring ( i - 1 , i ) , var ) && i < s . length ( ) - 1 && s . substring ( i + 1 , i + 2 ) . equals ( var ) ) s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; } return s ; } | Modifies the string to look more like it would if someone wrote the expression out on paper . |
11,123 | public String parse ( String expression ) throws InvalidExpressionException { this . expression = expression ; try { var = check ( ) ; } catch ( InvalidExpressionException e ) { e . printStackTrace ( ) ; } this . expression = formatString ( expression ) ; System . out . println ( this . expression ) ; tokens = tokenize ( this . expression ) ; return this . expression ; } | Creates tokens in polish notation for and re - formats the expression unless an expression without valid syntax is passed in . |
11,124 | private String formatString ( String exp ) { exp = exp . replaceAll ( "\\s" , "" ) ; exp = exp . toLowerCase ( ) ; int count = 0 ; if ( exp . substring ( 0 , 1 ) . equals ( "-" ) ) { exp = "$" + exp . substring ( 1 ) ; } for ( int i = 0 ; i < exp . length ( ) ; i ++ ) { if ( exp . substring ( i , i + 1 ) . equals ( "(" ) ) count ++ ; else if ( exp . substring ( i , i + 1 ) . equals ( ")" ) ) count -- ; } while ( count > 0 ) { exp += ")" ; count -- ; } for ( int i = 0 ; i < exp . length ( ) - 1 ; i ++ ) { String tmp1 = exp . substring ( i , i + 1 ) ; String tmp2 = exp . substring ( i + 1 , i + 2 ) ; if ( tmp2 . equals ( "-" ) && ( ExpressionParser . isOperator ( tmp1 ) || tmp1 . equals ( "(" ) ) ) exp = exp . substring ( 0 , i + 1 ) + "$" + exp . substring ( i + 2 ) ; else if ( ( tmp1 . matches ( "[0-9]+" ) || tmp1 . equals ( var ) ) && ( tmp2 . equals ( "(" ) || tmp2 . equals ( var ) ) ) exp = exp . substring ( 0 , i + 1 ) + "*" + exp . substring ( i + 1 ) ; } return exp ; } | adds and deletes characters to aid in the creation of the binary expression tree |
11,125 | static void reverse ( double [ ] d , DenseMatrix V ) { int m = V . nrows ( ) ; int n = d . length ; int half = n / 2 ; for ( int i = 0 ; i < half ; i ++ ) { double tmp = d [ i ] ; d [ i ] = d [ n - i - 1 ] ; d [ n - i - 1 ] = tmp ; } for ( int j = 0 ; j < half ; j ++ ) { for ( int i = 0 ; i < m ; i ++ ) { double tmp = V . get ( i , j ) ; V . set ( i , j , V . get ( i , n - j - 1 ) ) ; V . set ( i , n - j - 1 , tmp ) ; } } } | Reverse the array to match JMatrix . |
11,126 | public static double logistic ( double x ) { double y = 0.0 ; if ( x < - 40 ) { y = 2.353853e+17 ; } else if ( x > 40 ) { y = 1.0 + 4.248354e-18 ; } else { y = 1.0 + Math . exp ( - x ) ; } return 1.0 / y ; } | Logistic sigmoid function . |
11,127 | public static double round ( double x , int decimal ) { if ( decimal < 0 ) { return round ( x / pow ( 10 , - decimal ) ) * pow ( 10 , - decimal ) ; } else { return round ( x * pow ( 10 , decimal ) ) / pow ( 10 , decimal ) ; } } | Round a double vale to given digits such as 10^n where n is a positive or negative integer . |
11,128 | public static double logFactorial ( int n ) { if ( n < 0 ) { throw new IllegalArgumentException ( String . format ( "n has to be nonnegative: %d" , n ) ) ; } double f = 0.0 ; for ( int i = 2 ; i <= n ; i ++ ) { f += Math . log ( i ) ; } return f ; } | log of factorial of n |
11,129 | public static double choose ( int n , int k ) { if ( n < 0 || k < 0 ) { throw new IllegalArgumentException ( String . format ( "Invalid n = %d, k = %d" , n , k ) ) ; } if ( n < k ) { return 0.0 ; } return Math . floor ( 0.5 + Math . exp ( logChoose ( n , k ) ) ) ; } | n choose k . Returns 0 if n is less than k . |
11,130 | public static double logChoose ( int n , int k ) { if ( n < 0 || k < 0 || k > n ) { throw new IllegalArgumentException ( String . format ( "Invalid n = %d, k = %d" , n , k ) ) ; } return Math . logFactorial ( n ) - Math . logFactorial ( k ) - Math . logFactorial ( n - k ) ; } | log of n choose k |
11,131 | public static int [ ] random ( double [ ] prob , int n ) { double [ ] q = new double [ prob . length ] ; for ( int i = 0 ; i < prob . length ; i ++ ) { q [ i ] = prob [ i ] * prob . length ; } int [ ] a = new int [ prob . length ] ; for ( int i = 0 ; i < prob . length ; i ++ ) { a [ i ] = i ; } int [ ] HL = new int [ prob . length ] ; int head = 0 ; int tail = prob . length - 1 ; for ( int i = 0 ; i < prob . length ; i ++ ) { if ( q [ i ] >= 1.0 ) { HL [ head ++ ] = i ; } else { HL [ tail -- ] = i ; } } while ( head != 0 && tail != prob . length - 1 ) { int j = HL [ tail + 1 ] ; int k = HL [ head - 1 ] ; a [ j ] = k ; q [ k ] += q [ j ] - 1 ; tail ++ ; if ( q [ k ] < 1.0 ) { HL [ tail -- ] = k ; head -- ; } } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { double rU = random ( ) * prob . length ; int k = ( int ) ( rU ) ; rU -= k ; if ( rU < q [ k ] ) { ans [ i ] = k ; } else { ans [ i ] = a [ k ] ; } } return ans ; } | Given a set of m probabilities draw with replacement a set of n random number in [ 0 m ) . |
11,132 | public static double [ ] random ( double lo , double hi , int n ) { double [ ] x = new double [ n ] ; random . get ( ) . nextDoubles ( x , lo , hi ) ; return x ; } | Generate n uniform random numbers in the range [ lo hi ) . |
11,133 | public static int randomInt ( int lo , int hi ) { int w = hi - lo ; return lo + random . get ( ) . nextInt ( w ) ; } | Returns a random integer in [ lo hi ) . |
11,134 | public static int [ ] c ( int [ ] ... x ) { int n = 0 ; for ( int i = 0 ; i < x . length ; i ++ ) { n += x . length ; } int [ ] y = new int [ n ] ; for ( int i = 0 , k = 0 ; i < x . length ; i ++ ) { for ( int xi : x [ i ] ) { y [ k ++ ] = xi ; } } return y ; } | Merges multiple vectors into one . |
11,135 | public static boolean contains ( double [ ] [ ] polygon , double x , double y ) { if ( polygon . length <= 2 ) { return false ; } int hits = 0 ; int n = polygon . length ; double lastx = polygon [ n - 1 ] [ 0 ] ; double lasty = polygon [ n - 1 ] [ 1 ] ; double curx , cury ; for ( int i = 0 ; i < n ; lastx = curx , lasty = cury , i ++ ) { curx = polygon [ i ] [ 0 ] ; cury = polygon [ i ] [ 1 ] ; if ( cury == lasty ) { continue ; } double leftx ; if ( curx < lastx ) { if ( x >= lastx ) { continue ; } leftx = curx ; } else { if ( x >= curx ) { continue ; } leftx = lastx ; } double test1 , test2 ; if ( cury < lasty ) { if ( y < cury || y >= lasty ) { continue ; } if ( x < leftx ) { hits ++ ; continue ; } test1 = x - curx ; test2 = y - cury ; } else { if ( y < lasty || y >= cury ) { continue ; } if ( x < leftx ) { hits ++ ; continue ; } test1 = x - lastx ; test2 = y - lasty ; } if ( test1 < ( test2 / ( lasty - cury ) * ( lastx - curx ) ) ) { hits ++ ; } } return ( ( hits & 1 ) != 0 ) ; } | Determines if the polygon contains the specified coordinates . |
11,136 | public static void reverse ( int [ ] a ) { int i = 0 , j = a . length - 1 ; while ( i < j ) { SortUtils . swap ( a , i ++ , j -- ) ; } } | Reverses the order of the elements in the specified array . |
11,137 | public static double [ ] rowMin ( double [ ] [ ] data ) { double [ ] x = new double [ data . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = min ( data [ i ] ) ; } return x ; } | Returns the row minimum for a matrix . |
11,138 | public static double [ ] rowMax ( double [ ] [ ] data ) { double [ ] x = new double [ data . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = max ( data [ i ] ) ; } return x ; } | Returns the row maximum for a matrix . |
11,139 | public static double [ ] rowSums ( double [ ] [ ] data ) { double [ ] x = new double [ data . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = sum ( data [ i ] ) ; } return x ; } | Returns the row sums for a matrix . |
11,140 | public static double [ ] rowMeans ( double [ ] [ ] data ) { double [ ] x = new double [ data . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = mean ( data [ i ] ) ; } return x ; } | Returns the row means for a matrix . |
11,141 | public static double [ ] rowSds ( double [ ] [ ] data ) { double [ ] x = new double [ data . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = sd ( data [ i ] ) ; } return x ; } | Returns the row standard deviations for a matrix . |
11,142 | public static double [ ] colMin ( double [ ] [ ] data ) { double [ ] x = new double [ data [ 0 ] . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = Double . POSITIVE_INFINITY ; } for ( int i = 0 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < x . length ; j ++ ) { if ( x [ j ] > data [ i ] [ j ] ) { x [ j ] = data [ i ] [ j ] ; } } } return x ; } | Returns the column minimum for a matrix . |
11,143 | public static double [ ] colMax ( double [ ] [ ] data ) { double [ ] x = new double [ data [ 0 ] . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = Double . NEGATIVE_INFINITY ; } for ( int i = 0 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < x . length ; j ++ ) { if ( x [ j ] < data [ i ] [ j ] ) { x [ j ] = data [ i ] [ j ] ; } } } return x ; } | Returns the column maximum for a matrix . |
11,144 | public static double [ ] colSums ( double [ ] [ ] data ) { double [ ] x = data [ 0 ] . clone ( ) ; for ( int i = 1 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < x . length ; j ++ ) { x [ j ] += data [ i ] [ j ] ; } } return x ; } | Returns the column sums for a matrix . |
11,145 | public static double [ ] colMeans ( double [ ] [ ] data ) { double [ ] x = data [ 0 ] . clone ( ) ; for ( int i = 1 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < x . length ; j ++ ) { x [ j ] += data [ i ] [ j ] ; } } scale ( 1.0 / data . length , x ) ; return x ; } | Returns the column means for a matrix . |
11,146 | public static double [ ] colSds ( double [ ] [ ] data ) { if ( data . length < 2 ) { throw new IllegalArgumentException ( "Array length is less than 2." ) ; } int p = data [ 0 ] . length ; double [ ] sum = new double [ p ] ; double [ ] sumsq = new double [ p ] ; for ( double [ ] x : data ) { for ( int i = 0 ; i < p ; i ++ ) { sum [ i ] += x [ i ] ; sumsq [ i ] += x [ i ] * x [ i ] ; } } int n = data . length - 1 ; for ( int i = 0 ; i < p ; i ++ ) { sumsq [ i ] = java . lang . Math . sqrt ( sumsq [ i ] / n - ( sum [ i ] / data . length ) * ( sum [ i ] / n ) ) ; } return sumsq ; } | Returns the column deviations for a matrix . |
11,147 | public static int sum ( int [ ] x ) { double sum = 0.0 ; for ( int n : x ) { sum += n ; } if ( sum > Integer . MAX_VALUE || sum < - Integer . MAX_VALUE ) { throw new ArithmeticException ( "Sum overflow: " + sum ) ; } return ( int ) sum ; } | Returns the sum of an array . |
11,148 | public static < T extends Comparable < ? super T > > T median ( T [ ] a ) { return QuickSelect . median ( a ) ; } | Find the median of an array of type double . The input array will be rearranged . |
11,149 | public static double var ( int [ ] x ) { if ( x . length < 2 ) { throw new IllegalArgumentException ( "Array length is less than 2." ) ; } double sum = 0.0 ; double sumsq = 0.0 ; for ( int xi : x ) { sum += xi ; sumsq += xi * xi ; } int n = x . length - 1 ; return sumsq / n - ( sum / x . length ) * ( sum / n ) ; } | Returns the variance of an array . |
11,150 | public static double squaredDistance ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Input vector sizes are different." ) ; } double sum = 0.0 ; for ( int i = 0 ; i < x . length ; i ++ ) { sum += sqr ( x [ i ] - y [ i ] ) ; } return sum ; } | The squared Euclidean distance . |
11,151 | public static double dot ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Arrays have different length." ) ; } double p = 0.0 ; for ( int i = 0 ; i < x . length ; i ++ ) { p += x [ i ] * y [ i ] ; } return p ; } | Returns the dot product between two vectors . |
11,152 | public static double dot ( SparseArray x , SparseArray y ) { Iterator < SparseArray . Entry > it1 = x . iterator ( ) ; Iterator < SparseArray . Entry > it2 = y . iterator ( ) ; SparseArray . Entry e1 = it1 . hasNext ( ) ? it1 . next ( ) : null ; SparseArray . Entry e2 = it2 . hasNext ( ) ? it2 . next ( ) : null ; double s = 0.0 ; while ( e1 != null && e2 != null ) { if ( e1 . i == e2 . i ) { s += e1 . x * e2 . x ; e1 = it1 . hasNext ( ) ? it1 . next ( ) : null ; e2 = it2 . hasNext ( ) ? it2 . next ( ) : null ; } else if ( e1 . i > e2 . i ) { e2 = it2 . hasNext ( ) ? it2 . next ( ) : null ; } else { e1 = it1 . hasNext ( ) ? it1 . next ( ) : null ; } } return s ; } | Returns the dot product between two sparse arrays . |
11,153 | public static double cov ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Arrays have different length." ) ; } if ( x . length < 3 ) { throw new IllegalArgumentException ( "array length has to be at least 3." ) ; } double mx = mean ( x ) ; double my = mean ( y ) ; double Sxy = 0.0 ; for ( int i = 0 ; i < x . length ; i ++ ) { double dx = x [ i ] - mx ; double dy = y [ i ] - my ; Sxy += dx * dy ; } return Sxy / ( x . length - 1 ) ; } | Returns the covariance between two vectors . |
11,154 | public static double [ ] [ ] cov ( double [ ] [ ] data , double [ ] mu ) { double [ ] [ ] sigma = new double [ data [ 0 ] . length ] [ data [ 0 ] . length ] ; for ( int i = 0 ; i < data . length ; i ++ ) { for ( int j = 0 ; j < mu . length ; j ++ ) { for ( int k = 0 ; k <= j ; k ++ ) { sigma [ j ] [ k ] += ( data [ i ] [ j ] - mu [ j ] ) * ( data [ i ] [ k ] - mu [ k ] ) ; } } } int n = data . length - 1 ; for ( int j = 0 ; j < mu . length ; j ++ ) { for ( int k = 0 ; k <= j ; k ++ ) { sigma [ j ] [ k ] /= n ; sigma [ k ] [ j ] = sigma [ j ] [ k ] ; } } return sigma ; } | Returns the sample covariance matrix . |
11,155 | public static double cor ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Arrays have different length." ) ; } if ( x . length < 3 ) { throw new IllegalArgumentException ( "array length has to be at least 3." ) ; } double Sxy = cov ( x , y ) ; double Sxx = var ( x ) ; double Syy = var ( y ) ; if ( Sxx == 0 || Syy == 0 ) { return Double . NaN ; } return Sxy / java . lang . Math . sqrt ( Sxx * Syy ) ; } | Returns the correlation coefficient between two vectors . |
11,156 | public static double [ ] [ ] cor ( double [ ] [ ] data , double [ ] mu ) { double [ ] [ ] sigma = cov ( data , mu ) ; int n = data [ 0 ] . length ; double [ ] sd = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sd [ i ] = sqrt ( sigma [ i ] [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { sigma [ i ] [ j ] /= sd [ i ] * sd [ j ] ; sigma [ j ] [ i ] = sigma [ i ] [ j ] ; } } return sigma ; } | Returns the sample correlation matrix . |
11,157 | public static double kendall ( int [ ] x , int [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( "Input vector sizes are different." ) ; } int is = 0 , n2 = 0 , n1 = 0 , n = x . length ; double aa , a2 , a1 ; for ( int j = 0 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { a1 = x [ j ] - x [ k ] ; a2 = y [ j ] - y [ k ] ; aa = a1 * a2 ; if ( aa != 0.0 ) { ++ n1 ; ++ n2 ; if ( aa > 0 ) { ++ is ; } else { -- is ; } } else { if ( a1 != 0.0 ) { ++ n1 ; } if ( a2 != 0.0 ) { ++ n2 ; } } } } double tau = is / ( Math . sqrt ( n1 ) * Math . sqrt ( n2 ) ) ; return tau ; } | The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant . It is used with non - parametric data . |
11,158 | public static double norm1 ( double [ ] x ) { double norm = 0.0 ; for ( double n : x ) { norm += Math . abs ( n ) ; } return norm ; } | L1 vector norm . |
11,159 | public static double norm2 ( double [ ] x ) { double norm = 0.0 ; for ( double n : x ) { norm += n * n ; } norm = Math . sqrt ( norm ) ; return norm ; } | L2 vector norm . |
11,160 | public static double normInf ( double [ ] x ) { int n = x . length ; double f = Math . abs ( x [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { f = Math . max ( f , Math . abs ( x [ i ] ) ) ; } return f ; } | L - infinity vector norm . Maximum absolute value . |
11,161 | public static void standardize ( double [ ] x ) { double mu = mean ( x ) ; double sigma = sd ( x ) ; if ( isZero ( sigma ) ) { logger . warn ( "array has variance of 0." ) ; return ; } for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] = ( x [ i ] - mu ) / sigma ; } } | Standardizes an array to mean 0 and variance 1 . |
11,162 | public static void standardize ( double [ ] [ ] x ) { int n = x . length ; int p = x [ 0 ] . length ; double [ ] center = colMeans ( x ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < p ; j ++ ) { x [ i ] [ j ] = x [ i ] [ j ] - center [ j ] ; } } double [ ] scale = new double [ p ] ; for ( int j = 0 ; j < p ; j ++ ) { for ( int i = 0 ; i < n ; i ++ ) { scale [ j ] += Math . sqr ( x [ i ] [ j ] ) ; } scale [ j ] = Math . sqrt ( scale [ j ] / ( n - 1 ) ) ; if ( ! Math . isZero ( scale [ j ] ) ) { for ( int i = 0 ; i < n ; i ++ ) { x [ i ] [ j ] /= scale [ j ] ; } } } } | Standardizes each column of a matrix to 0 mean and unit variance . |
11,163 | public static void unitize1 ( double [ ] x ) { double n = norm1 ( x ) ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] /= n ; } } | Unitize an array so that L1 norm of x is 1 . |
11,164 | public static void unitize2 ( double [ ] x ) { double n = norm ( x ) ; for ( int i = 0 ; i < x . length ; i ++ ) { x [ i ] /= n ; } } | Unitize an array so that L2 norm of x = 1 . |
11,165 | private static int row ( int [ ] r , int f ) { int i = 0 ; while ( i < r . length && r [ i ] < f ) { ++ i ; } return ( ( i < r . length && r [ i ] == f ) ? i : - 1 ) ; } | Returns the index of given frequency . |
11,166 | public static void swap ( int [ ] x , int i , int j ) { int s = x [ i ] ; x [ i ] = x [ j ] ; x [ j ] = s ; } | Swap two elements of an array . |
11,167 | public static < E > void swap ( E [ ] x , E [ ] y ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; } for ( int i = 0 ; i < x . length ; i ++ ) { E s = x [ i ] ; x [ i ] = y [ i ] ; y [ i ] = s ; } } | Swap two arrays . |
11,168 | public static void plus ( double [ ] y , double [ ] x ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; } for ( int i = 0 ; i < x . length ; i ++ ) { y [ i ] += x [ i ] ; } } | Element - wise sum of two arrays y = x + y . |
11,169 | public static void minus ( double [ ] y , double [ ] x ) { if ( x . length != y . length ) { throw new IllegalArgumentException ( String . format ( "Arrays have different length: x[%d], y[%d]" , x . length , y . length ) ) ; } for ( int i = 0 ; i < x . length ; i ++ ) { y [ i ] -= x [ i ] ; } } | Element - wise subtraction of two arrays y = y - x . |
11,170 | public static double [ ] pow ( double [ ] x , double n ) { double [ ] array = new double [ x . length ] ; for ( int i = 0 ; i < x . length ; i ++ ) { array [ i ] = Math . pow ( x [ i ] , n ) ; } return array ; } | Raise each element of an array to a scalar power . |
11,171 | public static int [ ] [ ] sort ( double [ ] [ ] x ) { int n = x . length ; int p = x [ 0 ] . length ; double [ ] a = new double [ n ] ; int [ ] [ ] index = new int [ p ] [ ] ; for ( int j = 0 ; j < p ; j ++ ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = x [ i ] [ j ] ; } index [ j ] = QuickSort . sort ( a ) ; } return index ; } | Sorts each variable and returns the index of values in ascending order . Note that the order of original array is NOT altered . |
11,172 | public String createInfix ( Expression root ) { String str = "" ; String closeParen = "" ; String leftOpenParen = "" ; String leftCloseParen = "" ; if ( root == null ) { return str ; } if ( ExpressionParser . isOperand ( root . getType ( ) , var ) ) { str += root . getType ( ) ; } else if ( root . getType ( ) . equals ( "$" ) ) { str += "-" ; } else if ( ExpressionParser . isFunction ( root . getType ( ) ) ) { str += root . getType ( ) ; str += "(" ; closeParen = ")" ; } else { int parentPrecedence = ExpressionParser . getPrecedence ( root . getType ( ) ) ; str += root . getType ( ) ; if ( ExpressionParser . isOperator ( root . getLeftChild ( ) . getType ( ) ) && ( ExpressionParser . getPrecedence ( root . getLeftChild ( ) . getType ( ) ) < parentPrecedence ) ) { leftOpenParen = "(" ; leftCloseParen = ")" ; } else if ( ExpressionParser . isOperator ( root . getRightChild ( ) . getType ( ) ) && ( ExpressionParser . getPrecedence ( root . getRightChild ( ) . getType ( ) ) < parentPrecedence ) ) { str += "(" ; closeParen = ")" ; } } return leftOpenParen + createInfix ( root . getLeftChild ( ) ) + leftCloseParen + str + createInfix ( root . getRightChild ( ) ) + closeParen ; } | creates string representing infix expression |
11,173 | public Expression constructTree ( ArrayList < String > postTokens ) { Expression root = null ; Stack < Expression > nodes = new Stack < > ( ) ; for ( String str : postTokens ) { if ( str . isEmpty ( ) ) { continue ; } if ( str . matches ( "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?" ) ) { nodes . push ( new Constant ( Double . parseDouble ( str ) ) ) ; } else if ( str . equals ( var ) ) { nodes . push ( new Variable ( var ) ) ; } else if ( ! nodes . isEmpty ( ) && ExpressionParser . isFunction ( str ) ) { Expression function = matchFunc ( str , nodes . pop ( ) ) ; nodes . push ( function ) ; } else if ( ! nodes . isEmpty ( ) && str . equals ( "$" ) ) { Expression unaryMinus = new Negation ( nodes . pop ( ) ) ; nodes . push ( unaryMinus ) ; } else if ( ! nodes . isEmpty ( ) ) { Expression right = nodes . pop ( ) ; Expression binaryOperator = matchOperator ( str , nodes . pop ( ) , right ) ; nodes . push ( binaryOperator ) ; } } if ( ! nodes . isEmpty ( ) ) root = nodes . pop ( ) ; return root ; } | reads the tokens in order from the list and builds a tree |
11,174 | public SVM < T > train ( T [ ] x , int [ ] y , double [ ] weight ) { SVM < T > svm = null ; if ( k == 2 ) { svm = new SVM < > ( kernel , Cp , Cn ) ; } else { if ( this . weight == null ) { svm = new SVM < > ( kernel , Cp , k , strategy ) ; } else { svm = new SVM < > ( kernel , Cp , this . weight , strategy ) ; } } svm . setTolerance ( tol ) ; for ( int i = 1 ; i <= epochs ; i ++ ) { svm . learn ( x , y , weight ) ; } svm . finish ( ) ; return svm ; } | Learns a SVM classifier with given training data . |
11,175 | public void learn ( double [ ] [ ] x , int [ ] y ) { if ( model == Model . GENERAL ) { throw new UnsupportedOperationException ( "General-mode Naive Bayes classifier doesn't support online learning." ) ; } if ( model == Model . MULTINOMIAL ) { for ( int i = 0 ; i < x . length ; i ++ ) { if ( x [ i ] . length != p ) { throw new IllegalArgumentException ( "Invalid input vector size: " + x [ i ] . length ) ; } for ( int j = 0 ; j < p ; j ++ ) { ntc [ y [ i ] ] [ j ] += x [ i ] [ j ] ; nt [ y [ i ] ] += x [ i ] [ j ] ; } n ++ ; nc [ y [ i ] ] ++ ; } } else if ( model == Model . POLYAURN ) { for ( int i = 0 ; i < x . length ; i ++ ) { if ( x [ i ] . length != p ) { throw new IllegalArgumentException ( "Invalid input vector size: " + x [ i ] . length ) ; } for ( int j = 0 ; j < p ; j ++ ) { ntc [ y [ i ] ] [ j ] += x [ i ] [ j ] * 2 ; nt [ y [ i ] ] += x [ i ] [ j ] * 2 ; } n ++ ; nc [ y [ i ] ] ++ ; } } else { for ( int i = 0 ; i < x . length ; i ++ ) { if ( x [ i ] . length != p ) { throw new IllegalArgumentException ( "Invalid input vector size: " + x [ i ] . length ) ; } for ( int j = 0 ; j < p ; j ++ ) { if ( x [ i ] [ j ] > 0 ) { ntc [ y [ i ] ] [ j ] ++ ; } } n ++ ; nc [ y [ i ] ] ++ ; } } update ( ) ; } | Online learning of naive Bayes classifier on sequences which are modeled as a bag of words . Note that this method is NOT applicable for naive Bayes classifier with general generation model . |
11,176 | private void update ( ) { if ( ! predefinedPriori ) { for ( int c = 0 ; c < k ; c ++ ) { priori [ c ] = ( nc [ c ] + EPSILON ) / ( n + k * EPSILON ) ; } } if ( model == Model . MULTINOMIAL || model == Model . POLYAURN ) { for ( int c = 0 ; c < k ; c ++ ) { for ( int t = 0 ; t < p ; t ++ ) { condprob [ c ] [ t ] = ( ntc [ c ] [ t ] + sigma ) / ( nt [ c ] + sigma * p ) ; } } } else { for ( int c = 0 ; c < k ; c ++ ) { for ( int t = 0 ; t < p ; t ++ ) { condprob [ c ] [ t ] = ( ntc [ c ] [ t ] + sigma ) / ( nc [ c ] + sigma * 2 ) ; } } } } | Update conditional probabilities . |
11,177 | public int predict ( double [ ] x , double [ ] posteriori ) { if ( x . length != p ) { throw new IllegalArgumentException ( String . format ( "Invalid input vector size: %d" , x . length ) ) ; } if ( posteriori != null && posteriori . length != k ) { throw new IllegalArgumentException ( String . format ( "Invalid posteriori vector size: %d, expected: %d" , posteriori . length , k ) ) ; } int label = - 1 ; double max = Double . NEGATIVE_INFINITY ; boolean any = model == Model . GENERAL ? true : false ; for ( int i = 0 ; i < k ; i ++ ) { double logprob = Math . log ( priori [ i ] ) ; for ( int j = 0 ; j < p ; j ++ ) { switch ( model ) { case GENERAL : logprob += prob [ i ] [ j ] . logp ( x [ j ] ) ; break ; case MULTINOMIAL : case POLYAURN : if ( x [ j ] > 0 ) { logprob += x [ j ] * Math . log ( condprob [ i ] [ j ] ) ; any = true ; } break ; case BERNOULLI : if ( x [ j ] > 0 ) { logprob += Math . log ( condprob [ i ] [ j ] ) ; any = true ; } else { logprob += Math . log ( 1.0 - condprob [ i ] [ j ] ) ; } break ; } } if ( logprob > max && any ) { max = logprob ; label = i ; } if ( posteriori != null ) { posteriori [ i ] = logprob ; } } if ( posteriori != null && any ) { double Z = 0.0 ; for ( int i = 0 ; i < k ; i ++ ) { posteriori [ i ] = Math . exp ( posteriori [ i ] - max ) ; Z += posteriori [ i ] ; } for ( int i = 0 ; i < k ; i ++ ) { posteriori [ i ] /= Z ; } } return label ; } | Predict the class of an instance . |
11,178 | public double get ( int i ) { for ( Entry e : array ) { if ( e . i == i ) { return e . x ; } } return 0.0 ; } | Returns the value of i - th entry . |
11,179 | public boolean set ( int i , double x ) { if ( x == 0.0 ) { remove ( i ) ; return false ; } Iterator < Entry > it = array . iterator ( ) ; for ( int k = 0 ; it . hasNext ( ) ; k ++ ) { Entry e = it . next ( ) ; if ( e . i == i ) { e . x = x ; return false ; } else if ( e . i > i ) { array . add ( k , new Entry ( i , x ) ) ; return true ; } } array . add ( new Entry ( i , x ) ) ; return true ; } | Sets or add an entry . |
11,180 | public void remove ( int i ) { Iterator < Entry > it = array . iterator ( ) ; while ( it . hasNext ( ) ) { Entry e = it . next ( ) ; if ( e . i == i ) { it . remove ( ) ; break ; } } } | Removes an entry . |
11,181 | public RandomForest merge ( RandomForest other ) { if ( this . importance . length != other . importance . length ) { throw new IllegalArgumentException ( "RandomForest have different sizes of feature vectors" ) ; } ArrayList < RegressionTree > mergedTrees = new ArrayList < > ( ) ; mergedTrees . addAll ( this . trees ) ; mergedTrees . addAll ( other . trees ) ; double weightedMergedError = ( ( this . error * this . trees . size ( ) ) + ( other . error * other . trees . size ( ) ) ) / ( this . trees . size ( ) + other . trees . size ( ) ) ; double [ ] mergedImportance = calculateImportance ( mergedTrees , this . importance . length ) ; return new RandomForest ( mergedTrees , weightedMergedError , mergedImportance ) ; } | Merges together two random forests and returns a new forest consisting of trees from both input forests . |
11,182 | public SparseMatrix parse ( InputStream stream ) throws IOException , ParseException { int nrows = 0 , ncols = 0 , n = 0 ; int [ ] colIndex ; int [ ] rowIndex ; double [ ] data ; try ( Scanner scanner = new Scanner ( stream ) ) { String line = scanner . nextLine ( ) ; String [ ] tokens = line . split ( "\\s+" ) ; if ( tokens . length == 3 ) { try { nrows = Integer . parseInt ( tokens [ 0 ] ) ; ncols = Integer . parseInt ( tokens [ 1 ] ) ; n = Integer . parseInt ( tokens [ 2 ] ) ; } catch ( Exception ex ) { } } if ( n == 0 ) { line = scanner . nextLine ( ) . trim ( ) ; tokens = line . split ( "\\s+" ) ; int RHSCRD = Integer . parseInt ( tokens [ 4 ] ) ; line = scanner . nextLine ( ) . trim ( ) ; if ( ! line . startsWith ( "R" ) ) { throw new UnsupportedOperationException ( "SparseMatrixParser supports only real-valued matrix." ) ; } tokens = line . split ( "\\s+" ) ; nrows = Integer . parseInt ( tokens [ 1 ] ) ; ncols = Integer . parseInt ( tokens [ 2 ] ) ; n = Integer . parseInt ( tokens [ 3 ] ) ; line = scanner . nextLine ( ) ; if ( RHSCRD > 0 ) { line = scanner . nextLine ( ) ; } } colIndex = new int [ ncols + 1 ] ; rowIndex = new int [ n ] ; data = new double [ n ] ; for ( int i = 0 ; i <= ncols ; i ++ ) { colIndex [ i ] = scanner . nextInt ( ) - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { rowIndex [ i ] = scanner . nextInt ( ) - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { data [ i ] = scanner . nextDouble ( ) ; } } SparseMatrix matrix = new SparseMatrix ( nrows , ncols , data , rowIndex , colIndex ) ; return matrix ; } | Parse a Harwell - Boeing column - compressed sparse matrix dataset from an input stream . |
11,183 | private static Preconditioner diagonalPreconditioner ( Matrix A ) { return new Preconditioner ( ) { public void asolve ( double [ ] b , double [ ] x ) { double [ ] diag = A . diag ( ) ; int n = diag . length ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = diag [ i ] != 0.0 ? b [ i ] / diag [ i ] : b [ i ] ; } } } ; } | Returns a simple preconditioner matrix that is the trivial diagonal part of A in some cases . |
11,184 | private static double snorm ( double [ ] x , int itol ) { int n = x . length ; if ( itol <= 3 ) { double ans = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += x [ i ] * x [ i ] ; } return Math . sqrt ( ans ) ; } else { int isamax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( x [ i ] ) > Math . abs ( x [ isamax ] ) ) { isamax = i ; } } return Math . abs ( x [ isamax ] ) ; } } | Compute L2 or L - infinity norms for a vector x as signaled by itol . |
11,185 | public JComponent learn ( ) { JPanel pane = new JPanel ( new GridLayout ( 1 , 2 ) ) ; double [ ] [ ] data = dataset [ datasetIndex ] . toArray ( new double [ dataset [ datasetIndex ] . size ( ) ] [ ] ) ; String [ ] labels = dataset [ datasetIndex ] . toArray ( new String [ dataset [ datasetIndex ] . size ( ) ] ) ; if ( labels [ 0 ] == null ) { Attribute [ ] attr = dataset [ datasetIndex ] . attributes ( ) ; labels = new String [ attr . length ] ; for ( int i = 0 ; i < labels . length ; i ++ ) { labels [ i ] = attr [ i ] . getName ( ) ; } } long clock = System . currentTimeMillis ( ) ; SammonMapping sammon = new SammonMapping ( data , 2 ) ; System . out . format ( "Learn Sammon's Mapping (k=2) from %d samples in %dms\n" , data . length , System . currentTimeMillis ( ) - clock ) ; PlotCanvas plot = ScatterPlot . plot ( sammon . getCoordinates ( ) , labels ) ; plot . setTitle ( "Sammon's Mapping (k = 2)" ) ; pane . add ( plot ) ; clock = System . currentTimeMillis ( ) ; sammon = new SammonMapping ( data , 3 ) ; System . out . format ( "Learn Sammon's Mapping (k=3) from %d samples in %dms\n" , data . length , System . currentTimeMillis ( ) - clock ) ; plot = ScatterPlot . plot ( sammon . getCoordinates ( ) , labels ) ; plot . setTitle ( "Sammon's Mapping (k = 3)" ) ; pane . add ( plot ) ; return pane ; } | Execute the MDS algorithm and return a swing JComponent representing the clusters . |
11,186 | public void add ( double [ ] vals ) { ensureCapacity ( size + vals . length ) ; System . arraycopy ( vals , 0 , data , size , vals . length ) ; size += vals . length ; } | Appends an array to the end of this list . |
11,187 | private static double snorm ( double [ ] sx ) { int n = sx . length ; double ans = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += sx [ i ] * sx [ i ] ; } return Math . sqrt ( ans ) ; } | Compute squared root of L2 norms for a vector . |
11,188 | private void gram ( SupportVector i ) { int n = sv . size ( ) ; int m = MulticoreExecutor . getThreadPoolSize ( ) ; i . kcache = new DoubleArrayList ( n ) ; if ( n < 100 || m < 2 ) { for ( SupportVector v : sv ) { i . kcache . add ( kernel . k ( i . x , v . x ) ) ; } } else { List < KernelTask > tasks = new ArrayList < > ( m + 1 ) ; int step = n / m ; if ( step < 100 ) { step = 100 ; } int start = 0 ; int end = step ; for ( int l = 0 ; l < m - 1 ; l ++ ) { tasks . add ( new KernelTask ( i , start , end ) ) ; start += step ; end += step ; } tasks . add ( new KernelTask ( i , start , n ) ) ; try { for ( double [ ] ki : MulticoreExecutor . run ( tasks ) ) { for ( double kij : ki ) { i . kcache . add ( kij ) ; } } } catch ( Exception ex ) { for ( SupportVector v : sv ) { i . kcache . add ( kernel . k ( i . x , v . x ) ) ; } } } } | Calculate the row of kernel matrix for a vector i . |
11,189 | private int dfs ( int [ ] [ ] merge , int index , int [ ] order , int i ) { int n = merge . length + 1 ; if ( merge [ index ] [ 0 ] > merge . length ) { i = dfs ( merge , merge [ index ] [ 0 ] - n , order , i ) ; } else { order [ i ++ ] = merge [ index ] [ 0 ] ; } if ( merge [ index ] [ 1 ] > merge . length ) { i = dfs ( merge , merge [ index ] [ 1 ] - n , order , i ) ; } else { order [ i ++ ] = merge [ index ] [ 1 ] ; } return i ; } | DFS the tree to find the order of leafs to avoid the cross of lines in the plot . |
11,190 | public static PlotCanvas plot ( String id , int [ ] [ ] merge , double [ ] height ) { int n = merge . length + 1 ; Dendrogram dendrogram = new Dendrogram ( merge , height ) ; double [ ] lowerBound = { - n / 100 , 0 } ; double [ ] upperBound = { n + n / 100 , 1.01 * dendrogram . getHeight ( ) } ; PlotCanvas canvas = new PlotCanvas ( lowerBound , upperBound , false ) ; canvas . getAxis ( 0 ) . setGridVisible ( false ) ; canvas . getAxis ( 0 ) . setLabelVisible ( false ) ; dendrogram . setID ( id ) ; canvas . add ( dendrogram ) ; return canvas ; } | Create a dendrogram plot . |
11,191 | public Date toDate ( double x ) { if ( Double . isNaN ( x ) ) { return null ; } return new Date ( Double . doubleToRawLongBits ( x ) ) ; } | Retruns the date object from internal double encoding . |
11,192 | public Cholesky cholesky ( ) { if ( nrows ( ) != ncols ( ) ) { throw new UnsupportedOperationException ( "Cholesky decomposition on non-square matrix" ) ; } int n = nrows ( ) ; for ( int j = 0 ; j < n ; j ++ ) { double d = 0.0 ; for ( int k = 0 ; k < j ; k ++ ) { double s = 0.0 ; for ( int i = 0 ; i < k ; i ++ ) { s += get ( k , i ) * get ( j , i ) ; } s = ( get ( j , k ) - s ) / get ( k , k ) ; set ( j , k , s ) ; d = d + s * s ; } d = get ( j , j ) - d ; if ( d < 0.0 ) { throw new IllegalArgumentException ( "The matrix is not positive definite." ) ; } set ( j , j , Math . sqrt ( d ) ) ; } return new Cholesky ( this ) ; } | Cholesky decomposition for symmetric and positive definite matrix . Only the lower triangular part will be used in the decomposition . |
11,193 | public QR qr ( ) { int m = nrows ( ) ; int n = ncols ( ) ; double [ ] rDiagonal = new double [ n ] ; for ( int k = 0 ; k < n ; k ++ ) { double nrm = 0.0 ; for ( int i = k ; i < m ; i ++ ) { nrm = Math . hypot ( nrm , get ( i , k ) ) ; } if ( nrm != 0.0 ) { if ( get ( k , k ) < 0 ) { nrm = - nrm ; } for ( int i = k ; i < m ; i ++ ) { div ( i , k , nrm ) ; } add ( k , k , 1.0 ) ; for ( int j = k + 1 ; j < n ; j ++ ) { double s = 0.0 ; for ( int i = k ; i < m ; i ++ ) { s += get ( i , k ) * get ( i , j ) ; } s = - s / get ( k , k ) ; for ( int i = k ; i < m ; i ++ ) { add ( i , j , s * get ( i , k ) ) ; } } } rDiagonal [ k ] = - nrm ; } boolean singular = false ; for ( int j = 0 ; j < rDiagonal . length ; j ++ ) { if ( rDiagonal [ j ] == 0 ) { singular = true ; break ; } } return new QR ( this , rDiagonal , singular ) ; } | QR Decomposition is computed by Householder reflections . |
11,194 | private static double [ ] balance ( DenseMatrix A ) { double sqrdx = Math . RADIX * Math . RADIX ; int n = A . nrows ( ) ; double [ ] scale = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { scale [ i ] = 1.0 ; } boolean done = false ; while ( ! done ) { done = true ; for ( int i = 0 ; i < n ; i ++ ) { double r = 0.0 , c = 0.0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != i ) { c += Math . abs ( A . get ( j , i ) ) ; r += Math . abs ( A . get ( i , j ) ) ; } } if ( c != 0.0 && r != 0.0 ) { double g = r / Math . RADIX ; double f = 1.0 ; double s = c + r ; while ( c < g ) { f *= Math . RADIX ; c *= sqrdx ; } g = r * Math . RADIX ; while ( c > g ) { f /= Math . RADIX ; c /= sqrdx ; } if ( ( c + r ) / f < 0.95 * s ) { done = false ; g = 1.0 / f ; scale [ i ] *= f ; for ( int j = 0 ; j < n ; j ++ ) { A . mul ( i , j , g ) ; } for ( int j = 0 ; j < n ; j ++ ) { A . mul ( j , i , f ) ; } } } } } return scale ; } | Given a square matrix this routine replaces it by a balanced matrix with identical eigenvalues . A symmetric matrix is already balanced and is unaffected by this procedure . |
11,195 | private static void balbak ( DenseMatrix V , double [ ] scale ) { int n = V . nrows ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { V . mul ( i , j , scale [ i ] ) ; } } } | Form the eigenvectors of a real nonsymmetric matrix by back transforming those of the corresponding balanced matrix determined by balance . |
11,196 | private static int [ ] elmhes ( DenseMatrix A ) { int n = A . nrows ( ) ; int [ ] perm = new int [ n ] ; for ( int m = 1 ; m < n - 1 ; m ++ ) { double x = 0.0 ; int i = m ; for ( int j = m ; j < n ; j ++ ) { if ( Math . abs ( A . get ( j , m - 1 ) ) > Math . abs ( x ) ) { x = A . get ( j , m - 1 ) ; i = j ; } } perm [ m ] = i ; if ( i != m ) { for ( int j = m - 1 ; j < n ; j ++ ) { double swap = A . get ( i , j ) ; A . set ( i , j , A . get ( m , j ) ) ; A . set ( m , j , swap ) ; } for ( int j = 0 ; j < n ; j ++ ) { double swap = A . get ( j , i ) ; A . set ( j , i , A . get ( j , m ) ) ; A . set ( j , m , swap ) ; } } if ( x != 0.0 ) { for ( i = m + 1 ; i < n ; i ++ ) { double y = A . get ( i , m - 1 ) ; if ( y != 0.0 ) { y /= x ; A . set ( i , m - 1 , y ) ; for ( int j = m ; j < n ; j ++ ) { A . sub ( i , j , y * A . get ( m , j ) ) ; } for ( int j = 0 ; j < n ; j ++ ) { A . add ( j , m , y * A . get ( j , i ) ) ; } } } } } return perm ; } | Reduce a real nonsymmetric matrix to upper Hessenberg form . |
11,197 | private static void eltran ( DenseMatrix A , DenseMatrix V , int [ ] perm ) { int n = A . nrows ( ) ; for ( int mp = n - 2 ; mp > 0 ; mp -- ) { for ( int k = mp + 1 ; k < n ; k ++ ) { V . set ( k , mp , A . get ( k , mp - 1 ) ) ; } int i = perm [ mp ] ; if ( i != mp ) { for ( int j = mp ; j < n ; j ++ ) { V . set ( mp , j , V . get ( i , j ) ) ; V . set ( i , j , 0.0 ) ; } V . set ( i , mp , 1.0 ) ; } } } | Accumulate the stabilized elementary similarity transformations used in the reduction of a real nonsymmetric matrix to upper Hessenberg form by elmhes . |
11,198 | private static Complex cdiv ( double xr , double xi , double yr , double yi ) { double cdivr , cdivi ; double r , d ; if ( Math . abs ( yr ) > Math . abs ( yi ) ) { r = yi / yr ; d = yr + r * yi ; cdivr = ( xr + r * xi ) / d ; cdivi = ( xi - r * xr ) / d ; } else { r = yr / yi ; d = yi + r * yr ; cdivr = ( r * xr + xi ) / d ; cdivi = ( r * xi - xr ) / d ; } return new Complex ( cdivr , cdivi ) ; } | Complex scalar division . |
11,199 | protected static void sort ( double [ ] d , double [ ] e ) { int i = 0 ; int n = d . length ; for ( int j = 1 ; j < n ; j ++ ) { double real = d [ j ] ; double img = e [ j ] ; for ( i = j - 1 ; i >= 0 ; i -- ) { if ( d [ i ] >= d [ j ] ) { break ; } d [ i + 1 ] = d [ i ] ; e [ i + 1 ] = e [ i ] ; } d [ i + 1 ] = real ; e [ i + 1 ] = img ; } } | Sort eigenvalues . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.