Python numpy.zeros() Examples

The following are 30 code examples for showing how to use numpy.zeros(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: Caffe-Python-Data-Layer   Author: liuxianming   File: MultiLabelLayer.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_a_datum(self):
        if self._compressed:
            datum = extract_sample(
                self._data[self._cur], self._mean, self._resize)
        else:
            datum = self._data[self._cur]
        # start parsing labels
        label_elems = parse_label(self._label[self._cur])
        label = np.zeros(self._label_dim)
        if not self._multilabel:
            label[0] = label_elems[0]
        else:
            for i in label_elems:
                label[i] = 1
        self._cur = (self._cur + 1) % self._sample_count
        return datum, label 
Example 2
Project: Financial-NLP   Author: Coldog2333   File: NLP.py    License: Apache License 2.0 6 votes vote down vote up
def wordbag2mat(self, wordbag): #testing
        if self.model==None:
            raise Exception("no model")
        matrix=np.empty((len(wordbag),self.len_vector))
        #如果词典中不存在该词,抛出异常,但暂时还没有自定义词典的办法,所以暂时不那么严格
        #try:
        #    for i in range(len(wordbag)):
        #        matrix[i,:]=self.model[wordbag[i]]
        #except:
        #    raise Exception("'%s' can not be found in dictionary." % wordbag[i])
        #如果词典中不存在该词,则push进一列零向量
        for i in range(len(wordbag)):
            try:
                matrix[i,:]=self.model.wv.__getitem__(wordbag[i])#[wordbag[i]]
            except:
                matrix[i,:]=np.zeros((1,self.len_vector))
        return matrix
################################ problem ##################################### 
Example 3
Project: Financial-NLP   Author: Coldog2333   File: NLP.py    License: Apache License 2.0 6 votes vote down vote up
def similarity_label(self, words, normalization=True):
        """
        you can calculate more than one word at the same time.
        """
        if self.model==None:
            raise Exception('no model.')
        if isinstance(words, string_types):
            words=[words]
        vectors=np.transpose(self.model.wv.__getitem__(words))
        if normalization:
            unit_vector=unitvec(vectors,ax=0) # 这样写比原来那样速度提升一倍
            #unit_vector=np.zeros((len(vectors),len(words)))
            #for i in range(len(words)):
            #    unit_vector[:,i]=matutils.unitvec(vectors[:,i])
            dists=np.dot(self.Label_vec_u, unit_vector)
        else:
            dists=np.dot(self.Label_vec, vectors)
        return dists 
Example 4
Project: cat-bbs   Author: aleju   File: create_dataset.py    License: MIT License 6 votes vote down vote up
def load_keypoints(image_filepath, image_height, image_width):
    """Load facial keypoints of one image."""
    fp_keypoints = "%s.cat" % (image_filepath,)
    if not os.path.isfile(fp_keypoints):
        raise Exception("Could not find keypoint coordinates for image '%s'." \
                        % (image_filepath,))
    else:
        coords_raw = open(fp_keypoints, "r").readlines()[0].strip().split(" ")
        coords_raw = [abs(int(coord)) for coord in coords_raw]
        keypoints = []
        #keypoints_arr = np.zeros((9*2,), dtype=np.int32)
        for i in range(1, len(coords_raw), 2): # first element is the number of coords
            x = np.clip(coords_raw[i], 0, image_width-1)
            y = np.clip(coords_raw[i+1], 0, image_height-1)
            keypoints.append((x, y))

        return keypoints 
Example 5
Project: LipNet-PyTorch   Author: sailordiary   File: ctc_decoder.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def wer(self, r, h):
        # initialisation
        d = np.zeros((len(r)+1)*(len(h)+1), dtype=np.uint8)
        d = d.reshape((len(r)+1, len(h)+1))
        for i in range(len(r)+1):
            for j in range(len(h)+1):
                if i == 0:
                    d[0][j] = j
                elif j == 0:
                    d[i][0] = i

        # computation
        for i in range(1, len(r)+1):
            for j in range(1, len(h)+1):
                if r[i-1] == h[j-1]:
                    d[i][j] = d[i-1][j-1]
                else:
                    substitution = d[i-1][j-1] + 1
                    insertion    = d[i][j-1] + 1
                    deletion     = d[i-1][j] + 1
                    d[i][j] = min(substitution, insertion, deletion)

        return d[len(r)][len(h)] 
Example 6
Project: fenics-topopt   Author: zfergus   File: solver.py    License: MIT License 6 votes vote down vote up
def compliance_function_fdiff(self, x, dc):
        obj = self.compliance_function(x, dc)

        x0 = x.copy()
        dc0 = dc.copy()
        dcf = np.zeros(dc.shape)
        for i, v in enumerate(x):
            x = x0.copy()
            x[i] += 1e-6
            o1 = self.compliance_function(x, dc)
            x[i] = x0[i] - 1e-6
            o2 = self.compliance_function(x, dc)
            dcf[i] = (o1 - o2) / (2e-6)
        print("finite differences: {:g}".format(np.linalg.norm(dcf - dc0)))
        dc[:] = dc0

        return obj 
Example 7
Project: fenics-topopt   Author: zfergus   File: von_mises_stress.py    License: MIT License 6 votes vote down vote up
def calculate_fdiff_stress(self, x, u, nu, side=1, dx=1e-6):
        """
        Calculate the derivative of the Von Mises stress using finite
        differences given the densities x, displacements u, and young modulus
        nu. Optionally, provide the side length (default: 1) and delta x
        (default: 1e-6).
        """
        ds = self.calculate_diff_stress(x, u, nu, side)
        dsf = numpy.zeros(x.shape)
        x = numpy.expand_dims(x, -1)
        for i in range(x.shape[0]):
            delta = scipy.sparse.coo_matrix(([dx], [[i], [0]]), shape=x.shape)
            s1 = self.calculate_stress((x + delta.A).squeeze(), u, nu, side)
            s2 = self.calculate_stress((x - delta.A).squeeze(), u, nu, side)
            dsf[i] = ((s1 - s2) / (2. * dx))[i]
        print("finite differences: {:g}".format(numpy.linalg.norm(dsf - ds)))
        return dsf 
Example 8
Project: fenics-topopt   Author: zfergus   File: solver.py    License: MIT License 6 votes vote down vote up
def compliance_function_fdiff(self, x, dc):
        obj = self.compliance_function(x, dc)

        x0 = x.copy()
        dc0 = dc.copy()
        dcf = np.zeros(dc.shape)
        for i, v in enumerate(x):
            x = x0.copy()
            x[i] += 1e-6
            o1 = self.compliance_function(x, dc)
            x[i] = x0[i] - 1e-6
            o2 = self.compliance_function(x, dc)
            dcf[i] = (o1 - o2) / (2e-6)
        print("finite differences: {:g}".format(np.linalg.norm(dcf - dc0)))
        dc[:] = dc0

        return obj 
Example 9
Project: fenics-topopt   Author: zfergus   File: von_mises_stress.py    License: MIT License 6 votes vote down vote up
def calculate_fdiff_stress(self, x, u, nu, side=1, dx=1e-6):
        """
        Calculate the derivative of the Von Mises stress using finite
        differences given the densities x, displacements u, and young modulus
        nu. Optionally, provide the side length (default: 1) and delta x
        (default: 1e-6).
        """
        ds = self.calculate_diff_stress(x, u, nu, side)
        dsf = numpy.zeros(x.shape)
        x = numpy.expand_dims(x, -1)
        for i in range(x.shape[0]):
            delta = scipy.sparse.coo_matrix(([dx], [[i], [0]]), shape=x.shape)
            s1 = self.calculate_stress((x + delta.A).squeeze(), u, nu, side)
            s2 = self.calculate_stress((x - delta.A).squeeze(), u, nu, side)
            dsf[i] = ((s1 - s2) / (2. * dx))[i]
        print("finite differences: {:g}".format(numpy.linalg.norm(dsf - ds)))
        return dsf 
Example 10
Project: aospy   Author: spencerahill   File: test_utils_times.py    License: Apache License 2.0 6 votes vote down vote up
def test_add_uniform_time_weights():
    time = np.array([15, 46, 74])
    data = np.zeros((3))
    ds = xr.DataArray(data,
                      coords=[time],
                      dims=[TIME_STR],
                      name='a').to_dataset()
    units_str = 'days since 2000-01-01 00:00:00'
    cal_str = 'noleap'
    ds[TIME_STR].attrs['units'] = units_str
    ds[TIME_STR].attrs['calendar'] = cal_str

    with pytest.raises(KeyError):
        ds[TIME_WEIGHTS_STR]

    ds = add_uniform_time_weights(ds)
    time_weights_expected = xr.DataArray(
        [1, 1, 1], coords=ds[TIME_STR].coords, name=TIME_WEIGHTS_STR)
    time_weights_expected.attrs['units'] = 'days'
    assert ds[TIME_WEIGHTS_STR].identical(time_weights_expected) 
Example 11
Project: aospy   Author: spencerahill   File: conftest.py    License: Apache License 2.0 6 votes vote down vote up
def ds_time_encoded_cf():
    time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
    bounds = np.array([0, 1])
    time = np.array([15, 46, 74])
    data = np.zeros((3))
    ds = xr.DataArray(data,
                      coords=[time],
                      dims=[TIME_STR],
                      name='a').to_dataset()
    ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
                                       coords=[time, bounds],
                                       dims=[TIME_STR, BOUNDS_STR],
                                       name=TIME_BOUNDS_STR)
    units_str = 'days since 2000-01-01 00:00:00'
    cal_str = 'noleap'
    ds[TIME_STR].attrs['units'] = units_str
    ds[TIME_STR].attrs['calendar'] = cal_str
    return ds 
Example 12
Project: aospy   Author: spencerahill   File: test_data_loader.py    License: Apache License 2.0 6 votes vote down vote up
def ds_with_time_bounds(alt_lat_str, var_name):
    time_bounds = np.array([[0, 31], [31, 59], [59, 90]])
    bounds = np.array([0, 1])
    time = np.array([15, 46, 74])
    data = np.zeros((3, 1, 1))
    lat = [0]
    lon = [0]
    ds = xr.DataArray(data,
                      coords=[time, lat, lon],
                      dims=[TIME_STR, alt_lat_str, LON_STR],
                      name=var_name).to_dataset()
    ds[TIME_BOUNDS_STR] = xr.DataArray(time_bounds,
                                       coords=[time, bounds],
                                       dims=[TIME_STR, BOUNDS_STR],
                                       name=TIME_BOUNDS_STR)
    units_str = 'days since 2000-01-01 00:00:00'
    ds[TIME_STR].attrs['units'] = units_str
    ds[TIME_BOUNDS_STR].attrs['units'] = units_str
    return ds 
Example 13
Project: aospy   Author: spencerahill   File: test_data_loader.py    License: Apache License 2.0 6 votes vote down vote up
def test_sel_var():
    time = np.array([0, 31, 59]) + 15
    data = np.zeros((3))
    ds = xr.DataArray(data,
                      coords=[time],
                      dims=[TIME_STR],
                      name=convection_rain.name).to_dataset()
    condensation_rain_alt_name, = condensation_rain.alt_names
    ds[condensation_rain_alt_name] = xr.DataArray(data, coords=[ds.time])
    result = _sel_var(ds, convection_rain)
    assert result.name == convection_rain.name

    result = _sel_var(ds, condensation_rain)
    assert result.name == condensation_rain.name

    with pytest.raises(LookupError):
        _sel_var(ds, precip) 
Example 14
Project: Att-ChemdNER   Author: lingluodlut   File: nn.py    License: Apache License 2.0 6 votes vote down vote up
def build(self):
#{{{
        import numpy as np;
        self.W = shared((self.input_dim, 4 * self.output_dim),
                               name='{}_W'.format(self.name))
        self.U = shared((self.output_dim, 4 * self.output_dim),
                                     name='{}_U'.format(self.name))

        self.b = K.variable(np.hstack((np.zeros(self.output_dim),
                                        K.get_value(self.forget_bias_init(
                                                (self.output_dim,))),
                                        np.zeros(self.output_dim),
                                        np.zeros(self.output_dim))),
                                name='{}_b'.format(self.name))
        #self.c_0 = shared((self.output_dim,), name='{}_c_0'.format(self.name)  )
        #self.h_0 = shared((self.output_dim,), name='{}_h_0'.format(self.name)  )
        self.c_0=np.zeros(self.output_dim).astype(theano.config.floatX);
        self.h_0=np.zeros(self.output_dim).astype(theano.config.floatX);
        self.params=[self.W,self.U,
                        self.b,
                    # self.c_0,self.h_0
                    ];
        #}}} 
Example 15
Project: Att-ChemdNER   Author: lingluodlut   File: theano_backend.py    License: Apache License 2.0 6 votes vote down vote up
def ctc_update_log_p(skip_idxs, zeros, active, log_p_curr, log_p_prev):
    active_skip_idxs = skip_idxs[(skip_idxs < active).nonzero()]
    active_next = T.cast(T.minimum(
        T.maximum(
            active + 1,
            T.max(T.concatenate([active_skip_idxs, [-1]])) + 2 + 1
        ), log_p_curr.shape[0]), 'int32')

    common_factor = T.max(log_p_prev[:active])
    p_prev = T.exp(log_p_prev[:active] - common_factor)
    _p_prev = zeros[:active_next]
    # copy over
    _p_prev = T.set_subtensor(_p_prev[:active], p_prev)
    # previous transitions
    _p_prev = T.inc_subtensor(_p_prev[1:], _p_prev[:-1])
    # skip transitions
    _p_prev = T.inc_subtensor(_p_prev[active_skip_idxs + 2], p_prev[active_skip_idxs])
    updated_log_p_prev = T.log(_p_prev) + common_factor

    log_p_next = T.set_subtensor(
        zeros[:active_next],
        log_p_curr[:active_next] + updated_log_p_prev
    )
    return active_next, log_p_next 
Example 16
Project: Att-ChemdNER   Author: lingluodlut   File: theano_backend.py    License: Apache License 2.0 6 votes vote down vote up
def ctc_path_probs(predict, Y, alpha=1e-4):
    smoothed_predict = (1 - alpha) * predict[:, Y] + alpha * np.float32(1.) / Y.shape[0]
    L = T.log(smoothed_predict)
    zeros = T.zeros_like(L[0])
    log_first = zeros

    f_skip_idxs = ctc_create_skip_idxs(Y)
    b_skip_idxs = ctc_create_skip_idxs(Y[::-1])  # there should be a shortcut to calculating this

    def step(log_f_curr, log_b_curr, f_active, log_f_prev, b_active, log_b_prev):
        f_active_next, log_f_next = ctc_update_log_p(f_skip_idxs, zeros, f_active, log_f_curr, log_f_prev)
        b_active_next, log_b_next = ctc_update_log_p(b_skip_idxs, zeros, b_active, log_b_curr, log_b_prev)
        return f_active_next, log_f_next, b_active_next, log_b_next

    [f_active, log_f_probs, b_active, log_b_probs], _ = theano.scan(
        step, sequences=[L, L[::-1, ::-1]], outputs_info=[np.int32(1), log_first, np.int32(1), log_first])

    idxs = T.arange(L.shape[1]).dimshuffle('x', 0)
    mask = (idxs < f_active.dimshuffle(0, 'x')) & (idxs < b_active.dimshuffle(0, 'x'))[::-1, ::-1]
    log_probs = log_f_probs + log_b_probs[::-1, ::-1] - L
    return log_probs, mask 
Example 17
def _project_im_rois(im_rois, scales):
    """Project image RoIs into the image pyramid built by _get_image_blob.
    Arguments:
        im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates
        scales (list): scale factors as returned by _get_image_blob
    Returns:
        rois (ndarray): R x 4 matrix of projected RoI coordinates
        levels (list): image pyramid levels used by each projected RoI
    """
    im_rois = im_rois.astype(np.float, copy=False)

    if len(scales) > 1:
        widths = im_rois[:, 2] - im_rois[:, 0] + 1
        heights = im_rois[:, 3] - im_rois[:, 1] + 1
        areas = widths * heights
        scaled_areas = areas[:, np.newaxis] * (scales[np.newaxis, :] ** 2)
        diff_areas = np.abs(scaled_areas - 224 * 224)
        levels = diff_areas.argmin(axis=1)[:, np.newaxis]
    else:
        levels = np.zeros((im_rois.shape[0], 1), dtype=np.int)

    rois = im_rois * scales[levels]

    return rois, levels 
Example 18
Project: unicorn-hat-hd   Author: pimoroni   File: __init__.py    License: MIT License 5 votes vote down vote up
def setup_buffer(width, height):
    """Set up the internal pixel buffer.

    :param width: width of buffer, ideally in multiples of 16
    :param height: height of buffer, ideally in multiples of 16

    """
    global _buffer_width, _buffer_height, _buf

    _buffer_width = width
    _buffer_height = height
    _buf = numpy.zeros((_buffer_width, _buffer_height, 3), dtype=int) 
Example 19
Project: Black-Box-Audio   Author: rtaori   File: tf_logits.py    License: MIT License 5 votes vote down vote up
def compute_mfcc(audio, **kwargs):
    """
    Compute the MFCC for a given audio waveform. This is
    identical to how DeepSpeech does it, but does it all in
    TensorFlow so that we can differentiate through it.
    """

    batch_size, size = audio.get_shape().as_list()
    audio = tf.cast(audio, tf.float32)

    # 1. Pre-emphasizer, a high-pass filter
    audio = tf.concat((audio[:, :1], audio[:, 1:] - 0.97*audio[:, :-1], np.zeros((batch_size,1000),dtype=np.float32)), 1)

    # 2. windowing into frames of 320 samples, overlapping
    windowed = tf.stack([audio[:, i:i+400] for i in range(0,size-320,160)],1)

    # 3. Take the FFT to convert to frequency space
    ffted = tf.spectral.rfft(windowed, [512])
    ffted = 1.0 / 512 * tf.square(tf.abs(ffted))

    # 4. Compute the Mel windowing of the FFT
    energy = tf.reduce_sum(ffted,axis=2)+1e-30
    filters = np.load("filterbanks.npy").T
    feat = tf.matmul(ffted, np.array([filters]*batch_size,dtype=np.float32))+1e-30

    # 5. Take the DCT again, because why not
    feat = tf.log(feat)
    feat = tf.spectral.dct(feat, type=2, norm='ortho')[:,:,:26]

    # 6. Amplify high frequencies for some reason
    _,nframes,ncoeff = feat.get_shape().as_list()
    n = np.arange(ncoeff)
    lift = 1 + (22/2.)*np.sin(np.pi*n/22)
    feat = lift*feat
    width = feat.get_shape().as_list()[1]

    # 7. And now stick the energy next to the features
    feat = tf.concat((tf.reshape(tf.log(energy),(-1,width,1)), feat[:, :, 1:]), axis=2)
    
    return feat 
Example 20
Project: Black-Box-Audio   Author: rtaori   File: tf_logits.py    License: MIT License 5 votes vote down vote up
def get_logits(new_input, length, first=[]):
    """
    Compute the logits for a given waveform.

    First, preprocess with the TF version of MFC above,
    and then call DeepSpeech on the features.
    """
    # new_input = tf.Print(new_input, [tf.shape(new_input)])

    # We need to init DeepSpeech the first time we're called
    if first == []:
        first.append(False)
        # Okay, so this is ugly again.
        # We just want it to not crash.
        tf.app.flags.FLAGS.alphabet_config_path = "DeepSpeech/data/alphabet.txt"
        DeepSpeech.initialize_globals()
        print('initialized deepspeech globals')

    batch_size = new_input.get_shape()[0]

    # 1. Compute the MFCCs for the input audio
    # (this is differentable with our implementation above)
    empty_context = np.zeros((batch_size, 9, 26), dtype=np.float32)
    new_input_to_mfcc = compute_mfcc(new_input)[:, ::2]
    features = tf.concat((empty_context, new_input_to_mfcc, empty_context), 1)

    # 2. We get to see 9 frames at a time to make our decision,
    # so concatenate them together.
    features = tf.reshape(features, [new_input.get_shape()[0], -1])
    features = tf.stack([features[:, i:i+19*26] for i in range(0,features.shape[1]-19*26+1,26)],1)
    features = tf.reshape(features, [batch_size, -1, 19*26])

    # 3. Whiten the data
    mean, var = tf.nn.moments(features, axes=[0,1,2])
    features = (features-mean)/(var**.5)

    # 4. Finally we process it with DeepSpeech
    logits = DeepSpeech.BiRNN(features, length, [0]*10)

    return logits 
Example 21
Project: svviz   Author: svviz   File: kde.py    License: MIT License 5 votes vote down vote up
def evaluate(self, points):
        points = atleast_2d(points)

        d, m = points.shape
        if d != self.d:
            if d == 1 and m == self.d:
                # points was passed in as a row vector
                points = reshape(points, (self.d, 1))
                m = 1
            else:
                msg = "points have dimension %s, dataset has dimension %s" % (d,
                    self.d)
                raise ValueError(msg)

        result = zeros((m,), dtype=np.float)

        if m >= self.n:
            # there are more points than data, so loop over data
            for i in range(self.n):
                diff = self.dataset[:, i, newaxis] - points
                tdiff = dot(self.inv_cov, diff)
                energy = sum(diff*tdiff,axis=0) / 2.0
                result = result + exp(-energy)
        else:
            # loop over points
            for i in range(m):
                diff = self.dataset - points[:, i, newaxis]
                tdiff = dot(self.inv_cov, diff)
                energy = sum(diff * tdiff, axis=0) / 2.0
                result[i] = sum(exp(-energy), axis=0)

        result = result / self._norm_factor

        return result 
Example 22
Project: libTLDA   Author: wmkouw   File: util.py    License: MIT License 5 votes vote down vote up
def one_hot(y, fill_k=False, one_not=False):
    """Map to one-hot encoding."""
    # Check labels
    labels = np.unique(y)

    # Number of classes
    K = len(labels)

    # Number of samples
    N = y.shape[0]

    # Preallocate array
    if one_not:
        Y = -np.ones((N, K))
    else:
        Y = np.zeros((N, K))

    # Set k-th column to 1 for n-th sample
    for n in range(N):

        # Map current class to index label
        y_n = (y[n] == labels)

        if fill_k:
            Y[n, y_n] = y_n
        else:
            Y[n, y_n] = 1

    return Y, labels 
Example 23
Project: libTLDA   Author: wmkouw   File: rba.py    License: MIT License 5 votes vote down vote up
def psi(self, X, theta, w, K=2):
        """
        Compute psi function.

        Parameters
        ----------
        X : array
            data set (N samples by D features)
        theta : array
            classifier parameters (D features by 1)
        w : array
            importance-weights (N samples by 1)
        K : int
            number of classes (def: 2)

        Returns
        -------
        psi : array
            array with psi function values (N samples by K classes)

        """
        # Number of samples
        N = X.shape[0]

        # Preallocate psi array
        psi = np.zeros((N, K))

        # Loop over classes
        for k in range(K):
            # Compute feature statistics
            Xk = self.feature_stats(X, k*np.ones((N, 1)))

            # Compute psi function
            psi[:, k] = (w*np.dot(Xk, theta))[:, 0]

        return psi 
Example 24
Project: libTLDA   Author: wmkouw   File: rba.py    License: MIT License 5 votes vote down vote up
def posterior(self, psi):
        """
        Class-posterior estimation.

        Parameters
        ----------
        psi : array
            weighted data-classifier output (N samples by K classes)

        Returns
        -------
        pyx : array
            class-posterior estimation (N samples by K classes)

        """
        # Data shape
        N, K = psi.shape

        # Preallocate array
        pyx = np.zeros((N, K))

        # Subtract maximum value for numerical stability
        psi = (psi.T - np.max(psi, axis=1).T).T

        # Loop over classes
        for k in range(K):

            # Estimate posterior p^(Y=y | x_i)
            pyx[:, k] = np.exp(psi[:, k]) / np.sum(np.exp(psi), axis=1)

        return pyx 
Example 25
Project: libTLDA   Author: wmkouw   File: test_suba.py    License: MIT License 5 votes vote down vote up
def test_fit_semi():
    """Test for fitting the model."""
    X = rnd.randn(10, 2)
    y = np.hstack((np.zeros((5,)), np.ones((5,))))
    Z = rnd.randn(10, 2) + 1
    u = np.array([[0, 0], [9, 1]])
    clf = SemiSubspaceAlignedClassifier()
    clf.fit(X, y, Z, u)
    assert clf.is_trained 
Example 26
Project: libTLDA   Author: wmkouw   File: test_suba.py    License: MIT License 5 votes vote down vote up
def test_predict_semi():
    """Test for making predictions."""
    X = rnd.randn(10, 2)
    y = np.hstack((np.zeros((5,)), np.ones((5,))))
    Z = rnd.randn(10, 2) + 1
    u = np.array([[0, 0], [9, 1]])
    clf = SemiSubspaceAlignedClassifier()
    clf.fit(X, y, Z, u)
    u_pred = clf.predict(Z)
    labels = np.unique(y)
    assert len(np.setdiff1d(np.unique(u_pred), labels)) == 0 
Example 27
Project: libTLDA   Author: wmkouw   File: test_rba.py    License: MIT License 5 votes vote down vote up
def test_fit():
    """Test for fitting the model."""
    X = rnd.randn(10, 2)
    y = np.hstack((np.zeros((5,)), np.ones((5,))))
    Z = rnd.randn(10, 2) + 1
    clf = RobustBiasAwareClassifier()
    clf.fit(X, y, Z)
    assert clf.is_trained 
Example 28
Project: libTLDA   Author: wmkouw   File: test_rba.py    License: MIT License 5 votes vote down vote up
def test_predict():
    """Test for making predictions."""
    X = rnd.randn(10, 2)
    y = np.hstack((np.zeros((5,)), np.ones((5,))))
    Z = rnd.randn(10, 2) + 1
    clf = RobustBiasAwareClassifier()
    clf.fit(X, y, Z)
    u_pred = clf.predict(Z)
    labels = np.unique(y)
    assert len(np.setdiff1d(np.unique(u_pred), labels)) == 0 
Example 29
Project: libTLDA   Author: wmkouw   File: test_tcpr.py    License: MIT License 5 votes vote down vote up
def test_fit():
    """Test for fitting the model."""
    X = np.vstack((rnd.randn(5, 2), rnd.randn(5, 2)+1))
    y = np.hstack((np.zeros((5,)), np.ones((5,))))
    Z = np.vstack((rnd.randn(5, 2)-1, rnd.randn(5, 2)+2))
    clf = TargetContrastivePessimisticClassifier(l2=0.1)
    clf.fit(X, y, Z)
    assert clf.is_trained 
Example 30
Project: libTLDA   Author: wmkouw   File: test_scl.py    License: MIT License 5 votes vote down vote up
def test_init():
    """Test for object type."""
    clf = StructuralCorrespondenceClassifier()
    assert type(clf) == StructuralCorrespondenceClassifier
    assert not clf.is_trained


# def test_fit():
#     """Test for fitting the model."""
#     X = np.vstack((rnd.randn(5, 2), rnd.randn(5, 2)+1))
#     y = np.hstack((np.zeros((5,)), np.ones((5,))))
#     Z = np.vstack((rnd.randn(5, 2)-1, rnd.randn(5, 2)+2))
#     clf = StructuralCorrespondenceClassifier(l2=1.0)
#     clf.fit(X, y, Z)
#     assert clf.is_trained


# def test_predict():
#     """Test for making predictions."""
#     X = np.vstack((rnd.randn(5, 2), rnd.randn(5, 2)+1))
#     y = np.hstack((np.zeros((5,)), np.ones((5,))))
#     Z = np.vstack((rnd.randn(5, 2)-1, rnd.randn(5, 2)+2))
#     clf = StructuralCorrespondenceClassifier(l2=1.0)
#     clf.fit(X, y, Z)
#     u_pred = clf.predict(Z)
#     labels = np.unique(y)
#     assert len(np.setdiff1d(np.unique(u_pred), labels)) == 0