X: matrix of features, one sample per row (without bias unit)
    y: values (continuous) corresponding to rows (samples) in X
170225
def normal_equation(x, y):
    
    num_samples = y.size
    x = np.insert(x, 0, np.ones(num_samples), axis=1)

    return pinv(x)*y
368954
def toy_linear_1d_classification(seed=default_seed):
    """"""
    def sample_class(f):
        p = 1. / (1. + np.exp(-f))
        c = np.random.binomial(1, p)
        c = np.where(c, 1, -1)
        return c

    np.random.seed(seed=seed)
    x1 = np.random.normal(-3, 5, 20)
    x2 = np.random.normal(3, 5, 20)
    X = (np.r_[x1, x2])[:, None]
    return {'X': X, 'Y':  sample_class(2.*X), 'F': 2.*X, 'covariates' : ['X'], 'response': [discrete({'positive': 1, 'negative': -1})],'seed' : seed}
468556
def infer_x(self, y_desired, **kwargs):
        
        
        sigma = kwargs.get('sigma', self.sigma)
        k     = kwargs.get('k', self.k)
         
        xq = self._guess_x(y_desired, k = k, sigma = sigma, **kwargs)[0]    
         
        dists, index = self.fmodel.dataset.nn_x(xq, k = k)

        w = self.fmodel._weights(dists, sigma*sigma)
        
        X   = np.array([self.fmodel.dataset.get_x_padded(i) for i in index])
        Y    = np.array([self.fmodel.dataset.get_y(i) for i in index])
         
        W   = np.diag(w)
        WX  = np.dot(W, X)
        WXT = WX.T
             
        B   = np.dot(np.linalg.pinv(np.dot(WXT, WX)),WXT)
        
        M = np.dot(B, np.dot(W, Y))
        
        _, idx = self.fmodel.dataset.nn_y(y_desired, k=1)        
        ynn = self.fmodel.dataset.get_y(idx[0])
        
        eps = 0.00001
        
        J = np.zeros((len(xq),len(y_desired)), dtype = np.float64)
        for i in range(len(xq)):
            xi = np.array(xq, dtype = np.float64)
            xi[i] = xi[i] + eps
            yi = np.dot(np.append(1.0, xi), M).ravel()
            J[i] = (yi - ynn) / eps
        J = np.transpose(J)
        Jinv = np.linalg.pinv(J)
        x = np.dot(Jinv, y_desired - ynn)
        
        return [x]
981517
def sample_normal(x, y)
      gradient_x, gradient_y = sample_gradient x, y
      length = Gosu::distance 0, 0, gradient_x, gradient_y
      if length == 0
        [0, 0] # This could be NaN in edge cases.
      else
        [gradient_x / length, gradient_y / length]
      end
    end
76099
def predict_raw(self, X):
        
        # b -- bias for the input and h layers
        b = np.ones((X.shape[0], 1))
        w2 = self.w[-(self.h + 1):].reshape(self.h + 1, 1)
        w1 = self.w[:-(self.h + 1)].reshape(self.i + 1, self.h)

        # Make X to have the same number of columns as self.i.
        # Because of the sparse matrix representation, X for prediction can
        # have a different number of columns.
        if X.shape[1] > self.i:
            # If X has more columns, cut extra columns.
            X = X[:, :self.i]
        elif X.shape[1] < self.i:
            # If X has less columns, cut the rows of the weight matrix between
            # the input and h layers instead of X itself because the SciPy
            # sparse matrix does not support .set_shape() yet.
            idx = range(X.shape[1])
            idx.append(self.i)        # Include the last row for the bias
            w1 = w1[idx, :]

        if sparse.issparse(X):
            return np.hstack((sigm(sparse.hstack((X, b)).dot(w1)), b)).dot(w2)
        else:
            return np.hstack((sigm(np.hstack((X, b)).dot(w1)), b)).dot(w2)
1085094
def _guess_x_simple(self, y_desired, y_dims=None, **kwargs):
        """"""
        _, indexes = self.fmodel.dataset.nn_y(y_desired, dims=y_dims, k = 10)
        return [self.fmodel.get_x(i) for i in indexes]
743193
def sample_gradient(x, y)
      d0 = sample_distance x, y - 1
      d1 = sample_distance x - 1, y
      d2 = sample_distance x + 1, y
      d3 = sample_distance x, y + 1

      [(d2 - d1) / @scale, (d3 - d0) / @scale]
    end
1217277
def specify_data_set(self, x_input, y_input):
        
        self.x = x_input
        self.y = y_input
1123771
def _create_model(self, X, Y):
        
        from sklearn.ensemble import RandomForestRegressor
        self.X = X
        self.Y = Y
        self.model = RandomForestRegressor(bootstrap = self.bootstrap,
                                           criterion = self.criterion,
                                           max_depth = self.max_depth,
                                           max_features = self.max_features,
                                           max_leaf_nodes = self.max_leaf_nodes,
                                           min_samples_leaf = self.min_samples_leaf,
                                           min_samples_split = self.min_samples_split,
                                           min_weight_fraction_leaf = self.min_weight_fraction_leaf,
                                           n_estimators = self.n_estimators,
                                           n_jobs = self.n_jobs,
                                           oob_score = self.oob_score,
                                           random_state = self.random_state,
                                           verbose = self.verbose,
                                           warm_start = self.warm_start)

        #self.model = RandomForestRegressor()
        self.model.fit(X,Y.flatten())
804205
def _group_by_sample_and_batch(samples):
    
    out = collections.defaultdict(list)
    for data in samples:
        out[(dd.get_sample_name(data), dd.get_align_bam(data), tuple(_get_batches(data)))].append(data)
    return [xs[0] for xs in out.values()]
921284
def sample(self, num_rows):
        

        sampled_values = []
        for i in range(num_rows):
            sampled_values.append(self._sample_row())

        return pd.DataFrame(sampled_values, columns=self.columns)
861195
def calculate_covariance_matrix(X):
    

    n_features = X.shape[1]    
    S = np.zeros((n_features, n_features))
    m = np.mean(X, axis=0).reshape(n_features, 1)

    for x in X:
        v = x.reshape(n_features, 1) - m
        S += v @ v.T

    return 1/(X.shape[0]-1) * S
311851
def get_matrix(self, indices):
        """"""
        new = numpy.empty(self.samples1.shape)
        for idx in range(len(indices)):
            if indices[idx]:
                new[idx] = self.samples1[idx]
            else:
                new[idx] = self.samples2[idx]

        if self.poly:
            new = self.poly(*new)
        return new
216480
def infer_x(self, y):
        
        assert len(y) == self.fmodel.dim_y, "Wrong dimension for y. Expected %i, got %i" % (self.fmodel.dim_y, len(y))
        if len(self.fmodel.dataset) == 0:
            return [[0.0]*self.dim_x]
        else:
            _, index = self.fmodel.dataset.nn_y(y, k=1)
            return [self.fmodel.dataset.get_x(index[0])]
450498
function mean( out, x ) {
	var len = x.length,
		i;
	if ( out.length !== len ) {
		throw new Error( 'mean()::invalid input arguments. Input and output matrices must be the same length.' );
	}
	for ( i = 0; i < len; i++ ) {
		out.data[ i ] = MEAN( x.data[ i ] );
	}
	return out;
}
1063033
def fit(self, X, y=None):
        
        self.columns_ = X.columns
        columns = self.columns
        if columns is None:
            columns = X.select_dtypes(include=["category"]).columns
        else:
            for column in columns:
                assert is_categorical_dtype(X[column]), "Must be categorical"

        self.categorical_columns_ = columns
        self.non_categorical_columns_ = X.columns.drop(self.categorical_columns_)

        if _HAS_CTD:
            self.dtypes_ = {col: X[col].dtype for col in self.categorical_columns_}
        else:
            self.dtypes_ = {
                col: (X[col].cat.categories, X[col].cat.ordered)
                for col in self.categorical_columns_
            }

        left = len(self.non_categorical_columns_)
        self.categorical_blocks_ = {}
        for col in self.categorical_columns_:
            right = left + len(X[col].cat.categories)
            if self.drop_first:
                right -= 1
            self.categorical_blocks_[col], left = slice(left, right), right

        if isinstance(X, pd.DataFrame):
            sample = X.iloc[:1]
        else:
            sample = X._meta_nonempty

        self.transformed_columns_ = pd.get_dummies(
            sample, drop_first=self.drop_first
        ).columns
        return self
121649
def predict_f_samples(self, Xnew, num_samples):
        
        mu, var = self._build_predict(Xnew, full_cov=True)  # N x P, # P x N x N
        jitter = tf.eye(tf.shape(mu)[0], dtype=settings.float_type) * settings.jitter
        samples = []
        for i in range(self.num_latent):
            L = tf.cholesky(var[i, :, :] + jitter)
            shape = tf.stack([tf.shape(L)[0], num_samples])
            V = tf.random_normal(shape, dtype=settings.float_type)
            samples.append(mu[:, i:i + 1] + tf.matmul(L, V))
        return tf.transpose(tf.stack(samples))