Python numpy.vstack() Examples

The following are 30 code examples of numpy.vstack(). 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 also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: BDA.py    From transferlearning with MIT License 7 votes vote down vote up
def proxy_a_distance(source_X, target_X):
    """
    Compute the Proxy-A-Distance of a source/target representation
    """
    nb_source = np.shape(source_X)[0]
    nb_target = np.shape(target_X)[0]

    train_X = np.vstack((source_X, target_X))
    train_Y = np.hstack((np.zeros(nb_source, dtype=int),
                         np.ones(nb_target, dtype=int)))

    clf = svm.LinearSVC(random_state=0)
    clf.fit(train_X, train_Y)
    y_pred = clf.predict(train_X)
    error = metrics.mean_absolute_error(train_Y, y_pred)
    dist = 2 * (1 - 2 * error)
    return dist 
Example #2
Source File: tools_fri_doa_plane.py    From FRIDA with MIT License 6 votes vote down vote up
def mtx_updated_G(phi_recon, M, mtx_amp2visi_ri, mtx_fri2visi_ri):
    """
    Update the linear transformation matrix that links the FRI sequence to the
    visibilities by using the reconstructed Dirac locations.
    :param phi_recon: the reconstructed Dirac locations (azimuths)
    :param M: the Fourier series expansion is between -M to M
    :param p_mic_x: a vector that contains microphones' x-coordinates
    :param p_mic_y: a vector that contains microphones' y-coordinates
    :param mtx_freq2visi: the linear mapping from Fourier series to visibilities
    :return:
    """
    L = 2 * M + 1
    ms_half = np.reshape(np.arange(-M, 1, step=1), (-1, 1), order='F')
    phi_recon = np.reshape(phi_recon, (1, -1), order='F')
    mtx_amp2freq = np.exp(-1j * ms_half * phi_recon)  # size: (M + 1) x K
    mtx_amp2freq_ri = np.vstack((mtx_amp2freq.real, mtx_amp2freq.imag[:-1, :]))  # size: (2M + 1) x K
    mtx_fri2amp_ri = linalg.lstsq(mtx_amp2freq_ri, np.eye(L))[0]
    # projection mtx_freq2visi to the null space of mtx_fri2amp
    mtx_null_proj = np.eye(L) - np.dot(mtx_fri2amp_ri.T,
                                       linalg.lstsq(mtx_fri2amp_ri.T, np.eye(L))[0])
    G_updated = np.dot(mtx_amp2visi_ri, mtx_fri2amp_ri) + \
                np.dot(mtx_fri2visi_ri, mtx_null_proj)
    return G_updated 
Example #3
Source File: TCA.py    From transferlearning with MIT License 6 votes vote down vote up
def fit(self, Xs, Xt):
        '''
        Transform Xs and Xt
        :param Xs: ns * n_feature, source feature
        :param Xt: nt * n_feature, target feature
        :return: Xs_new and Xt_new after TCA
        '''
        X = np.hstack((Xs.T, Xt.T))
        X /= np.linalg.norm(X, axis=0)
        m, n = X.shape
        ns, nt = len(Xs), len(Xt)
        e = np.vstack((1 / ns * np.ones((ns, 1)), -1 / nt * np.ones((nt, 1))))
        M = e * e.T
        M = M / np.linalg.norm(M, 'fro')
        H = np.eye(n) - 1 / n * np.ones((n, n))
        K = kernel(self.kernel_type, X, None, gamma=self.gamma)
        n_eye = m if self.kernel_type == 'primal' else n
        a, b = np.linalg.multi_dot([K, M, K.T]) + self.lamb * np.eye(n_eye), np.linalg.multi_dot([K, H, K.T])
        w, V = scipy.linalg.eig(a, b)
        ind = np.argsort(w)
        A = V[:, ind[:self.dim]]
        Z = np.dot(A.T, K)
        Z /= np.linalg.norm(Z, axis=0)
        Xs_new, Xt_new = Z[:, :ns].T, Z[:, ns:].T
        return Xs_new, Xt_new 
Example #4
Source File: snippets.py    From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License 6 votes vote down vote up
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)):
  """ A wrapper function to generate anchors given different scales
    Also return the number of anchors in variable 'length'
  """
  anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
  A = anchors.shape[0]
  shift_x = np.arange(0, width) * feat_stride
  shift_y = np.arange(0, height) * feat_stride
  shift_x, shift_y = np.meshgrid(shift_x, shift_y)
  shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
  K = shifts.shape[0]
  # width changes faster, so here it is H, W, C
  anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
  anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
  length = np.int32(anchors.shape[0])

  return anchors, length 
Example #5
Source File: coroOnlyScheduler.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scheduleRevisit(self, sInd, smin, det, pInds):
        """A Helper Method for scheduling revisits after observation detection
        Args:
            sInd - sInd of the star just detected
            smin - minimum separation of the planet to star of planet just detected
            det - 
            pInds - Indices of planets around target star
        Return:
            updates self.starRevisit attribute
        """

        TK = self.TimeKeeping

        t_rev = TK.currentTimeNorm.copy() + self.revisit_wait[sInd]
        # finally, populate the revisit list (NOTE: sInd becomes a float)
        revisit = np.array([sInd, t_rev.to('day').value])
        if self.starRevisit.size == 0:#If starRevisit has nothing in it
            self.starRevisit = np.array([revisit])#initialize sterRevisit
        else:
            revInd = np.where(self.starRevisit[:,0] == sInd)[0]#indices of the first column of the starRevisit list containing sInd 
            if revInd.size == 0:
                self.starRevisit = np.vstack((self.starRevisit, revisit))
            else:
                self.starRevisit[revInd,1] = revisit[1]#over 
Example #6
Source File: datasets.py    From discomll with Apache License 2.0 6 votes vote down vote up
def ex3(replication=2):
    f = open(path + "ex3.txt")
    train_data = np.loadtxt(f, delimiter=",")
    f = open(path + "ex3_test.txt")
    test_data = np.loadtxt(f, delimiter=",")

    x_train = np.insert(train_data[:, (0, 1)], 0, np.ones(len(train_data)), axis=1)
    y_train = train_data[:, 2]
    x_test = np.insert(test_data[:, (0, 1)], 0, np.ones(len(test_data)), axis=1)
    y_test = test_data[:, 2]

    for i in range(replication - 1):
        x_train = np.vstack((x_train, np.insert(train_data[:, (0, 1)], 0, np.ones(len(train_data)), axis=1)))
        y_train = np.hstack((y_train, train_data[:, 2]))

        x_test = np.vstack((x_test, np.insert(test_data[:, (0, 1)], 0, np.ones(len(test_data)), axis=1)))
        y_test = np.hstack((y_test, test_data[:, 2]))

    return x_train, y_train, x_test, y_test 
Example #7
Source File: datasets.py    From discomll with Apache License 2.0 6 votes vote down vote up
def regression_data():
    f = open(path + "regression_data1.txt")
    data = np.loadtxt(f, delimiter=",")
    x1 = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1)
    y1 = data[:, 1]
    f = open(path + "regression_data2.txt")
    data = np.loadtxt(f, delimiter=",")
    x2 = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1)
    y2 = data[:, 1]
    x1 = np.vstack((x1, x2))
    y1 = np.hstack((y1, y2))

    f = open(path + "regression_data_test1.txt")
    data = np.loadtxt(f, delimiter=",")
    x1_test = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1)
    y1_test = data[:, 1]
    f = open(path + "regression_data_test2.txt")
    data = np.loadtxt(f, delimiter=",")
    x2_test = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1)
    y2_test = data[:, 1]
    x1_test = np.vstack((x1_test, x2_test))
    y1_test = np.hstack((y1_test, y2_test))
    return x1, y1, x1_test, y1_test 
Example #8
Source File: datasets.py    From discomll with Apache License 2.0 6 votes vote down vote up
def iris(replication=2):
    f = open(path + "iris.txt")
    data = np.loadtxt(f, delimiter=",", dtype=np.string0)
    x_train = np.array(data[:, range(0, 4)], dtype=np.float)
    y_train = data[:, 4]

    for j in range(replication - 1):
        x_train = np.vstack([x_train, data[:, range(0, 4)]])
        y_train = np.hstack([y_train, data[:, 4]])
    x_train = np.array(x_train, dtype=np.float)

    f = open(path + "iris_test.txt")
    data = np.loadtxt(f, delimiter=",", dtype=np.string0)
    x_test = np.array(data[:, range(0, 4)], dtype=np.float)
    y_test = data[:, 4]

    for j in range(replication - 1):
        x_test = np.vstack([x_test, data[:, range(0, 4)]])
        y_test = np.hstack([y_test, data[:, 4]])
    x_test = np.array(x_test, dtype=np.float)

    return x_train, y_train, x_test, y_test 
Example #9
Source File: datasets.py    From discomll with Apache License 2.0 6 votes vote down vote up
def breastcancer_disc(replication=2):
    f = open(path + "breast_cancer_wisconsin_disc.txt")
    data = np.loadtxt(f, delimiter=",")
    x_train = data[:, range(1, 10)]
    y_train = data[:, 10]
    for j in range(replication - 1):
        x_train = np.vstack([x_train, data[:, range(1, 10)]])
        y_train = np.hstack([y_train, data[:, 10]])

    f = open(path + "breast_cancer_wisconsin_disc_test.txt")
    data = np.loadtxt(f, delimiter=",")
    x_test = data[:, range(1, 10)]
    y_test = data[:, 10]
    for j in range(replication - 1):
        x_test = np.vstack([x_test, data[:, range(1, 10)]])
        y_test = np.hstack([y_test, data[:, 10]])

    return x_train, y_train, x_test, y_test 
Example #10
Source File: datasets.py    From discomll with Apache License 2.0 6 votes vote down vote up
def breastcancer_cont(replication=2):
    f = open(path + "breast_cancer_wisconsin_cont.txt", "r")
    data = np.loadtxt(f, delimiter=",", dtype=np.string0)
    x_train = np.array(data[:, range(0, 9)])
    y_train = np.array(data[:, 9])
    for j in range(replication - 1):
        x_train = np.vstack([x_train, data[:, range(0, 9)]])
        y_train = np.hstack([y_train, data[:, 9]])
    x_train = np.array(x_train, dtype=np.float)

    f = open(path + "breast_cancer_wisconsin_cont_test.txt")
    data = np.loadtxt(f, delimiter=",", dtype=np.string0)
    x_test = np.array(data[:, range(0, 9)])
    y_test = np.array(data[:, 9])
    for j in range(replication - 1):
        x_test = np.vstack([x_test, data[:, range(0, 9)]])
        y_test = np.hstack([y_test, data[:, 9]])
    x_test = np.array(x_test, dtype=np.float)

    return x_train, y_train, x_test, y_test 
Example #11
Source File: images.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def image_reslice(image, spec, method=None, fill=0, dtype=None, weights=None, image_type=None):
    '''
    image_reslice(image, spec) yields a duplicate of the given image resliced to have the voxels
      indicated by the given image spec. Note that spec may be an image itself.

    Optional arguments that can be passed to image_interpolate() (asside from affine) are allowed
    here and are passed through.
    '''
    if image_type is None and is_image(image): image_type = to_image_type(image)
    spec = to_image_spec(spec)
    image = to_image(image)
    # we make a big mesh and interpolate at these points...
    imsh = spec['image_shape']
    (args, kw) = ([np.arange(n) for n in imsh[:3]], {'indexing': 'ij'})
    ijk = np.asarray([u.flatten() for u in np.meshgrid(*args, **kw)])
    ijk = np.dot(spec['affine'], np.vstack([ijk, np.ones([1,ijk.shape[1]])]))[:3]
    # interpolate here...
    u = image_interpolate(image, ijk, method=method, fill=fill, dtype=dtype, weights=weights)
    return to_image((np.reshape(u, imsh), spec), image_type=image_type) 
Example #12
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def apply_affine(aff, coords):
    '''
    apply_affine(affine, coords) yields the result of applying the given affine transformation to
      the given coordinate or coordinates.

    This function expects coords to be a (dims X n) matrix but if the first dimension is neither 2
    nor 3, coords.T is used; i.e.:
      apply_affine(affine3x3, coords2xN) ==> newcoords2xN
      apply_affine(affine4x4, coords3xN) ==> newcoords3xN
      apply_affine(affine3x3, coordsNx2) ==> newcoordsNx2 (for N != 2)
      apply_affine(affine4x4, coordsNx3) ==> newcoordsNx3 (for N != 3)
    '''
    if aff is None: return coords
    (coords,tr) = (np.asanyarray(coords), False)
    if len(coords.shape) == 1: return np.squeeze(apply_affine(np.reshape(coords, (-1,1)), aff))
    elif len(coords.shape) > 2: raise ValueError('cannot apply affine to ND-array for N > 2')
    if   len(coords) == 2: aff = to_affine(aff, 2)
    elif len(coords) == 3: aff = to_affine(aff, 3)
    else: (coords,aff,tr) = (coords.T, to_affine(aff, coords.shape[1]), True)
    r = np.dot(aff, np.vstack([coords, np.ones([1,coords.shape[1]])]))[:-1]
    return r.T if tr else r 
Example #13
Source File: core.py    From neuropythy with GNU Affero General Public License v3.0 6 votes vote down vote up
def subcurve(self, t0, t1):
        '''
        curve.subcurve(t0, t1) yields a curve-spline object that is equivalent to the given
          curve but that extends from curve(t0) to curve(t1) only.
        '''
        # if t1 is less than t0, then we want to actually do this in reverse...
        if t1 == t0: raise ValueError('Cannot take subcurve of a point')
        if t1 < t0:
            tt = self.curve_length()
            return self.reverse().subcurve(tt - t0, tt - t1)
        idx = [ii for (ii,t) in enumerate(self.t) if t0 < t and t < t1]
        pt0 = self(t0)
        pt1 = self(t1)
        coords = np.vstack([[pt0], self.coordinates.T[idx], [pt1]])
        ts = np.concatenate([[t0], self.t[idx], [t1]])
        dists  = None if self.distances is None else np.diff(ts)
        return CurveSpline(
            coords.T,
            order=self.order,
            smoothing=self.smoothing,
            periodic=False,
            distances=dists,
            meta_data=self.meta_data) 
Example #14
Source File: TensorFlowInterface.py    From IntroToDeepLearning with MIT License 5 votes vote down vote up
def plotFields(layer,fieldShape=None,channel=None,maxFields=25,figName='ReceptiveFields',cmap=None,padding=0.01):
	# Receptive Fields Summary
	W = layer.W
	wp = W.eval().transpose();
	if len(np.shape(wp)) < 4:		# Fully connected layer, has no shape
		fields = np.reshape(wp,list(wp.shape[0:-1])+fieldShape)
	else:			# Convolutional layer already has shape
		features, channels, iy, ix = np.shape(wp)
		if channel is not None:
			fields = wp[:,channel,:,:]
		else:
			fields = np.reshape(wp,[features*channels,iy,ix])

	fieldsN = min(fields.shape[0],maxFields)
	perRow = int(math.floor(math.sqrt(fieldsN)))
	perColumn = int(math.ceil(fieldsN/float(perRow)))

	fig = mpl.figure(figName); mpl.clf()

	# Using image grid
	from mpl_toolkits.axes_grid1 import ImageGrid
	grid = ImageGrid(fig,111,nrows_ncols=(perRow,perColumn),axes_pad=padding,cbar_mode='single')
	for i in range(0,fieldsN):
		im = grid[i].imshow(fields[i],cmap=cmap);

	grid.cbar_axes[0].colorbar(im)
	mpl.title('%s Receptive Fields' % layer.name)

	# old way
	# fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))])
	# tiled = []
	# for i in range(0,perColumn*perRow,perColumn):
	# 	tiled.append(np.hstack(fields2[i:i+perColumn]))
	#
	# tiled = np.vstack(tiled)
	# mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Receptive Fields' % layer.name); mpl.colorbar();
	mpl.figure(figName+' Total'); mpl.clf(); mpl.imshow(np.sum(np.abs(fields),0),cmap=cmap); mpl.title('%s Total Absolute Input Dependency' % layer.name); mpl.colorbar() 
Example #15
Source File: digit_data_loader.py    From transferlearning with MIT License 5 votes vote down vote up
def __getitem__(self, index):
        """
         Args:
             index (int): Index
         Returns:
             tuple: (image, target) where target is index of the target class.
         """

        img, target = self.data[index], self.labels[index]
        # doing this so that it is consistent with all other datasets
        # to return a PIL Image
        # print(img.shape)
        if img.shape[0] != 1:
            # print(img)
            img = Image.fromarray(np.uint8(np.asarray(img.transpose((1, 2, 0)))))
        #
        elif img.shape[0] == 1:
            im = np.uint8(np.asarray(img))
            # print(np.vstack([im,im,im]).shape)
            im = np.vstack([im, im, im]).transpose((1, 2, 0))
            img = Image.fromarray(im)

        if self.target_transform is not None:
            target = self.target_transform(target)
        if self.transform is not None:
            img = self.transform(img)
            #  return img, target
        return img, target 
Example #16
Source File: video_metrics.py    From fine-lm with MIT License 5 votes vote down vote up
def compute_all_metrics_statistics(all_results):
  """Computes statistics of metrics across multiple decodings."""
  statistics = {}
  for key in all_results[0].keys():
    values = [result[key] for result in all_results]
    values = np.vstack(values)
    statistics[key + "_MEAN"] = np.mean(values, axis=0)
    statistics[key + "_STD"] = np.std(values, axis=0)
    statistics[key + "_MIN"] = np.min(values, axis=0)
    statistics[key + "_MAX"] = np.max(values, axis=0)
  return statistics 
Example #17
Source File: TensorFlowInterface.py    From IntroToDeepLearning with MIT License 5 votes vote down vote up
def plotOutput(layer,feed_dict,fieldShape=None,channel=None,figOffset=1,cmap=None):
	# Output summary
	W = layer.output
	wp = W.eval(feed_dict=feed_dict);
	if len(np.shape(wp)) < 4:		# Fully connected layer, has no shape
		temp = np.zeros(np.product(fieldShape)); temp[0:np.shape(wp.ravel())[0]] = wp.ravel()
		fields = np.reshape(temp,[1]+fieldShape)
	else:			# Convolutional layer already has shape
		wp = np.rollaxis(wp,3,0)
		features, channels, iy,ix = np.shape(wp)
		if channel is not None:
			fields = wp[:,channel,:,:]
		else:
			fields = np.reshape(wp,[features*channels,iy,ix])

	perRow = int(math.floor(math.sqrt(fields.shape[0])))
	perColumn = int(math.ceil(fields.shape[0]/float(perRow)))
	fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))])
	tiled = []
	for i in range(0,perColumn*perRow,perColumn):
		tiled.append(np.hstack(fields2[i:i+perColumn]))

	tiled = np.vstack(tiled)
	if figOffset is not None:
		mpl.figure(figOffset); mpl.clf();

	mpl.imshow(tiled,cmap=cmap); mpl.title('%s Output' % layer.name); mpl.colorbar(); 
Example #18
Source File: generate_anchors.py    From cascade-rcnn_Pytorch with MIT License 5 votes vote down vote up
def generate_anchors(base_size=16, ratios=[0.5, 1, 2],
                     scales=2**np.arange(3, 6)):
    """
    Generate anchor (reference) windows by enumerating aspect ratios X
    scales wrt a reference (0, 0, 15, 15) window.
    """

    base_anchor = np.array([1, 1, base_size, base_size]) - 1
    ratio_anchors = _ratio_enum(base_anchor, ratios)
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                         for i in xrange(ratio_anchors.shape[0])])
    return anchors 
Example #19
Source File: intra_alignment.py    From transferlearning with MIT License 5 votes vote down vote up
def getGFKDim(Xs, Xt):
    Pss = PCA().fit(Xs).components_.T
    Pts = PCA().fit(Xt).components_.T
    Psstt = PCA().fit(np.vstack((Xs, Xt))).components_.T
    
    DIM = round(Xs.shape[1]*0.5)
    res = -1
    
    for d in range(1, DIM+1):
        Ps = Pss[:, :d]
        Pt = Pts[:, :d]
        Pst = Psstt[:, :d]
        alpha1 = getAngle(Ps, Pst, d)
        alpha2 = getAngle(Pt, Pst, d)
        D = (alpha1 + alpha2) * 0.5
        check = [round(D[1, dd]*100) == 100 for dd in range(d)]
        if True in check:
            res = list(map(lambda i: i == True, check)).index(True) 
            return res 
Example #20
Source File: intra_alignment.py    From transferlearning with MIT License 5 votes vote down vote up
def PCA_map(Xs, Xt):
    dim = getGFKDim(Xs, Xt)
    X = np.vstack((Xs, Xt))
    X_new = PCA().fit_transform(X)[:, :dim]
    Xs_new = X_new[:Xs.shape[0], :]
    Xt_new = X_new[Xs.shape[0]:, :]
    return Xs_new, Xt_new 
Example #21
Source File: bbox.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def bbox_transform(ex_rois, gt_rois, box_stds):
    """
    compute bounding box regression targets from ex_rois to gt_rois
    :param ex_rois: [N, 4]
    :param gt_rois: [N, 4]
    :return: [N, 4]
    """
    assert ex_rois.shape[0] == gt_rois.shape[0], 'inconsistent rois number'

    ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0
    ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0
    ex_ctr_x = ex_rois[:, 0] + 0.5 * (ex_widths - 1.0)
    ex_ctr_y = ex_rois[:, 1] + 0.5 * (ex_heights - 1.0)

    gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0
    gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0
    gt_ctr_x = gt_rois[:, 0] + 0.5 * (gt_widths - 1.0)
    gt_ctr_y = gt_rois[:, 1] + 0.5 * (gt_heights - 1.0)

    targets_dx = (gt_ctr_x - ex_ctr_x) / (ex_widths + 1e-14) / box_stds[0]
    targets_dy = (gt_ctr_y - ex_ctr_y) / (ex_heights + 1e-14) / box_stds[1]
    targets_dw = np.log(gt_widths / ex_widths) / box_stds[2]
    targets_dh = np.log(gt_heights / ex_heights) / box_stds[3]

    targets = np.vstack((targets_dx, targets_dy, targets_dw, targets_dh)).transpose()
    return targets 
Example #22
Source File: proxy_a_distance.py    From transferlearning with MIT License 5 votes vote down vote up
def proxy_a_distance(source_X, target_X, verbose=False):
    """
    Compute the Proxy-A-Distance of a source/target representation
    """
    nb_source = np.shape(source_X)[0]
    nb_target = np.shape(target_X)[0]

    if verbose:
        print('PAD on', (nb_source, nb_target), 'examples')

    C_list = np.logspace(-5, 4, 10)

    half_source, half_target = int(nb_source/2), int(nb_target/2)
    train_X = np.vstack((source_X[0:half_source, :], target_X[0:half_target, :]))
    train_Y = np.hstack((np.zeros(half_source, dtype=int), np.ones(half_target, dtype=int)))

    test_X = np.vstack((source_X[half_source:, :], target_X[half_target:, :]))
    test_Y = np.hstack((np.zeros(nb_source - half_source, dtype=int), np.ones(nb_target - half_target, dtype=int)))

    best_risk = 1.0
    for C in C_list:
        clf = svm.SVC(C=C, kernel='linear', verbose=False)
        clf.fit(train_X, train_Y)

        train_risk = np.mean(clf.predict(train_X) != train_Y)
        test_risk = np.mean(clf.predict(test_X) != test_Y)

        if verbose:
            print('[ PAD C = %f ] train risk: %f  test risk: %f' % (C, train_risk, test_risk))

        if test_risk > .5:
            test_risk = 1. - test_risk

        best_risk = min(best_risk, test_risk)

    return 2 * (1. - 2 * best_risk) 
Example #23
Source File: anchor.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def _generate_base_anchors(base_size, scales, ratios):
        """
        Generate anchor (reference) windows by enumerating aspect ratios X
        scales wrt a reference (0, 0, 15, 15) window.
        """
        base_anchor = np.array([1, 1, base_size, base_size]) - 1
        ratio_anchors = AnchorGenerator._ratio_enum(base_anchor, ratios)
        anchors = np.vstack([AnchorGenerator._scale_enum(ratio_anchors[i, :], scales)
                             for i in range(ratio_anchors.shape[0])])
        return anchors 
Example #24
Source File: anchor.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def generate(self, feat_height, feat_width):
        shift_x = np.arange(0, feat_width) * self._feat_stride
        shift_y = np.arange(0, feat_height) * self._feat_stride
        shift_x, shift_y = np.meshgrid(shift_x, shift_y)
        shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
        # add A anchors (1, A, 4) to
        # cell K shifts (K, 1, 4) to get
        # shift anchors (K, A, 4)
        # reshape to (K*A, 4) shifted anchors
        A = self._num_anchors
        K = shifts.shape[0]
        all_anchors = self._base_anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
        all_anchors = all_anchors.reshape((K * A, 4))
        return all_anchors 
Example #25
Source File: negativesample.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def _refill_queue(self):
        """Fetch another batch from the source, and shuffle it to make
        negative samples.
        """
        original = self._sourcedata.next().data  # List of NDArrays: one per input
        batch_size = original[0].shape[0]
        num_inputs = len(original)

        #Start with positive examples, copied straight
        outdata = [[o.asnumpy()] for o in original] # list of lists of numpy arrays
        outlabels = [np.ones(batch_size) * self.positive_label] # list of numpy arrays
        # The inner list of both is the set of samples.  We'll recombine later.

        # Construct negative samples.
        for _ in range(self.sample_ratio):
            shuffled = self._shuffle_batch(original)
            for i,d in enumerate(shuffled):
                outdata[i].append(d)
            outlabels.append(np.ones(batch_size) * self.negative_label)
        def stacker(x):
            if len(x[0].shape)==1:
                return np.hstack(x)
            else:
                return np.vstack(x)
        outdata = [stacker(x) for x in outdata] # Single tall vectors
        outlabels = stacker(outlabels)

        # Record-level shuffle so the negatives are mixed in.
        def shuffler(x, idx):
            return np.take(x,idx,0)
        shuf_idx = np.arange(len(outlabels))
        np.random.shuffle(shuf_idx)
        outdata = [shuffler(o,shuf_idx) for o in outdata]
        outlabels = shuffler(outlabels,shuf_idx)
        self._push_queue(outdata,outlabels) 
Example #26
Source File: malware.py    From trees with Apache License 2.0 5 votes vote down vote up
def graph_ROC(max_ACC, TP, FP, name="STD"):
    aTP = np.vstack(TP)
    n = len(TP)
    mean_TP = np.mean(aTP, axis=0)
    stderr_TP = np.std(aTP, axis=0) / (n ** 0.5)
    var_TP = np.var(aTP, axis=0)
    max_TP = mean_TP + 3 * stderr_TP
    min_TP = mean_TP - 3 * stderr_TP

    # sTP = sum(TP) / len(TP)
    sFP = FP[0]
    print len(sFP), len(mean_TP), len(TP[0])
    smax_ACC = np.mean(max_ACC)

    plt.cla()
    plt.clf()
    plt.close()

    plt.plot(sFP, mean_TP)
    plt.fill_between(sFP, min_TP, max_TP, color='black', alpha=0.2)
    plt.xlim((0,0.1))
    plt.ylim((0,1))
    plt.title('ROC Curve (accuracy=%.3f)' % smax_ACC)
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.savefig(r"../scratch/"+name+"_ROC_curve.pdf", bbox_inches='tight')

    # Write the data to the file
    f = file(r"../scratch/"+name+"_ROC_curve.csv", "w")
    f.write("FalsePositive,TruePositive,std_err, var, n\n")
    for fp, tp, err, var in zip(sFP, mean_TP, stderr_TP, var_TP):
        f.write("%s, %s, %s, %s, %s\n" % (fp, tp, err, var, n))
    f.close() 
Example #27
Source File: eval_metric.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def _insert(self, key, records, count):
        """ Insert records according to key """
        if key not in self.records:
            assert key not in self.counts
            self.records[key] = records
            self.counts[key] = count
        else:
            self.records[key] = np.vstack((self.records[key], records))
            assert key in self.counts
            self.counts[key] += count 
Example #28
Source File: input_from_history.py    From padasip with MIT License 5 votes vote down vote up
def input_from_history(a, n, bias=False):
    """
    This is function for creation of input matrix.

    **Args:**

    * `a` : series (1 dimensional array)

    * `n` : size of input matrix row (int). It means how many samples \
        of previous history you want to use \
        as the filter input. It also represents the filter length.

    **Kwargs:**

    * `bias` : decides if the bias is used (Boolean). If True, \
        array of all ones is appended as a last column to matrix `x`. \
        So matrix `x` has `n`+1 columns.

    **Returns:**

    * `x` : input matrix (2 dimensional array) \
        constructed from an array `a`. The length of `x` \
        is calculated as length of `a` - `n` + 1. \
        If the `bias` is used, then the amount of columns is `n` if not then \
        amount of columns is `n`+1).

    """
    if not type(n) == int:
        raise ValueError('The argument n must be int.')
    if not n > 0:
        raise ValueError('The argument n must be greater than 0')
    try:
        a = np.array(a, dtype="float64")
    except:
        raise ValueError('The argument a is not numpy array or similar.')
    x = np.array([a[i:i+n] for i in range(len(a)-n+1)]) 
    if bias:
        x = np.vstack((x.T, np.ones(len(x)))).T
    return x 
Example #29
Source File: test_tcpr.py    From libTLDA with MIT License 5 votes vote down vote up
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 = TargetContrastivePessimisticClassifier(l2=0.1)
    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 #30
Source File: label_prop.py    From transferlearning with MIT License 5 votes vote down vote up
def label_prop(C, nt, Dct, lp="linear"):
    
#Inputs:
#  C      :    Number of share classes between src and tar
#  nt     :    Number of target domain samples
#  Dct    :    All d_ct in matrix form, nt * C
#  lp     :    Type of linear programming: linear (default) | binary
#Outputs:
#  Mcj    :    all M_ct in matrix form, m * C

    intcon = C * nt
    Aeq = np.zeros([nt, intcon])
    Beq = np.ones([nt, 1])
    for i in range(nt):
        Aeq[i, i*C:(i+1)*C] = 1;
	
    D_vec = np.reshape(Dct, (1, intcon))
    CC = np.asarray(D_vec, dtype=np.double)
	
    A = np.array([])
    B = -1 * np.ones([C, 1])
    for i in range(C):
        all_zeros = np.zeros([1, intcon])
        for j in range(i, C * nt, C):
            all_zeros[0][j] = -1
        if i == 0:
            A = all_zeros
        else:
            A = np.vstack((A, all_zeros))
    
    if lp == "binary":
        print("not implemented yet!")
    else:
        res = linprog(CC,A,B,Aeq,Beq, bounds=tuple((0, 1) for _ in range(intcon)))
    Mct_vec = res.get("x")[0:C*nt]
    Mcj = Mct_vec.reshape((C,nt), order="F").T
  
    return Mcj