Exemplo n.º 1
0
    def fit(self, X, y):

        if self.__unsupervised_thresh is not None:

            # user can pass in a negative to let us decide
            if self.__unsupervised_thresh <= 0:
                self.__unsupervised_thresh = 20

            qb = dipy.segment.clustering.QuickBundles(
                threshold=self.__unsupervised_thresh, metric=dipy.segment.metric.AveragePointwiseEuclideanMetric())
            clusters = qb.cluster(numpy.vsplit(numpy.reshape(X, (-1, 3)), X.shape[0]))
            cluster_labels = list()
            for cluster in clusters:
                current_labels, current_label_counts = numpy.unique(y[cluster.indices], return_counts=True)
                cluster_labels.append(current_labels[numpy.argmax(current_label_counts)])
            self.__centroids = map(lambda cl: cl.centroid, clusters)
            self.__centroid_labels = numpy.array(cluster_labels)

        else:

            unique_labels = numpy.unique(y)
            centroids = numpy.full((len(unique_labels), X.shape[1]), numpy.nan)
            for index_label, unique_label in enumerate(unique_labels):
                centroids[index_label] = numpy.mean(X[y == unique_label], axis=0)
            self.__centroids = numpy.vsplit(centroids.reshape((-1, 3)), centroids.shape[0])
            self.__centroid_labels = unique_labels
Exemplo n.º 2
0
def fun_find_pairwise_distances(x, y):
    """
    A functional implementation of the same function
    """
    return np.array([[np.linalg.norm(x_elem - y_elem)
                      for y_elem in np.vsplit(y, y.shape[0])]
                     for x_elem in np.vsplit(x, x.shape[0])])
Exemplo n.º 3
0
Arquivo: odds.py Projeto: snexus/ODDS
    def ChangeSize(self,n_nodes):
        self.masses.resize((1,n_nodes),refcheck=False)
        #self.masses=np.resize(self.masses,(1,n_nodes))
        #self.masses[0][-1] #bug in resize??
        self.initDisp.resize((1,n_nodes),refcheck=False)
        self.initVel.resize((1,n_nodes),refcheck=False)
        #self.initDisp=np.resize(self.initDisp,(1,n_nodes))
        #self.initVel=np.resize(self.initVel,(1,n_nodes))
        

        if n_nodes>self.n_nodes:
            #Take care of 2D array manipulation
            delta=n_nodes-self.n_nodes
            hor=np.zeros((self.n_nodes,delta))
            ver=np.zeros((delta,n_nodes))
            self.springs=np.vstack((np.hstack((self.springs,hor)),ver))
            self.dampers=np.vstack((np.hstack((self.dampers,hor)),ver))
            # Take care of displacement and forces list
            print self.n_nodes,n_nodes
            for i in range(0,n_nodes-self.n_nodes):
                #print i
                self.displacements.append(-1)
                self.forces.append(-1)
            #addArray=[0 for x in range(self.syst.n_nodes,n_nodes)]
        elif n_nodes<self.n_nodes:
            self.springs=np.hsplit(np.vsplit(self.springs,(n_nodes,n_nodes))[0],(n_nodes,n_nodes))[0]
            self.dampers=np.hsplit(np.vsplit(self.dampers,(n_nodes,n_nodes))[0],(n_nodes,n_nodes))[0]
            self.displacements=self.displacements[0:n_nodes]
            self.forces=self.forces[0:n_nodes]
        self.n_nodes=n_nodes
Exemplo n.º 4
0
def cross_validation(trndata, folds=3, **kwargs):
    """
        kwargs are parameters for the model
    """
    input = np.vsplit(trndata['input'], folds)
    target = np.vsplit(trndata['target'], folds)

    zipped = zip(input, target)

    accuracy_sum = 0
    for i in len(zipped):
        new_train = ClassificationDataSet(attributes, nb_classes=classes_number)
        new_test = ClassificationDataSet(attributes, nb_classes=classes_number)
        test_zipped = zipped[i]
        train_zipped = zipped[:i] + zipped[(i+1):]

        new_train.setField('input', np.vstack[train_zipped[0]])
        new_train.setField('target', np.vstack[train_zipped[1]])

        new_test.setField('input', test_zipped[0])
        new_test.setField('target', train_zipped[1])

        model = FNNClassifier()
        model.train(new_train, new_test, kwargs)
        out, targ = model.predict(new_test)
        accuracy_sum += accuracy(out, targ)

    return accuracy_sum / folds
Exemplo n.º 5
0
def valid():
    rows = np.vsplit(puzzle, 9)
    cols = np.hsplit(puzzle, 9)
    grids = [grid for h in np.hsplit(puzzle, 3) for grid in np.vsplit(h, 3)]

    units = rows + cols + grids
    return all(np.max(np.bincount(unit[unit != 0])) == 1 for unit in units)
Exemplo n.º 6
0
def test_validation_curve_cv_splits_consistency():
    n_samples = 100
    n_splits = 5
    X, y = make_classification(n_samples=100, random_state=0)

    scores1 = validation_curve(SVC(kernel='linear', random_state=0), X, y,
                               'C', [0.1, 0.1, 0.2, 0.2],
                               cv=OneTimeSplitter(n_splits=n_splits,
                                                  n_samples=n_samples))
    # The OneTimeSplitter is a non-re-entrant cv splitter. Unless, the
    # `split` is called for each parameter, the following should produce
    # identical results for param setting 1 and param setting 2 as both have
    # the same C value.
    assert_array_almost_equal(*np.vsplit(np.hstack(scores1)[(0, 2, 1, 3), :],
                                         2))

    scores2 = validation_curve(SVC(kernel='linear', random_state=0), X, y,
                               'C', [0.1, 0.1, 0.2, 0.2],
                               cv=KFold(n_splits=n_splits, shuffle=True))

    # For scores2, compare the 1st and 2nd parameter's scores
    # (Since the C value for 1st two param setting is 0.1, they must be
    # consistent unless the train test folds differ between the param settings)
    assert_array_almost_equal(*np.vsplit(np.hstack(scores2)[(0, 2, 1, 3), :],
                                         2))

    scores3 = validation_curve(SVC(kernel='linear', random_state=0), X, y,
                               'C', [0.1, 0.1, 0.2, 0.2],
                               cv=KFold(n_splits=n_splits))

    # OneTimeSplitter is basically unshuffled KFold(n_splits=5). Sanity check.
    assert_array_almost_equal(np.array(scores3), np.array(scores1))
Exemplo n.º 7
0
    def create_batches(self):
        self.num_batches = self.tensor.size / (self.batch_size * self.seq_length)

        newline = self.vocab['\n']
        nextnew = np.where(self.tensor == newline)
        n_tunes = nextnew[0].size

        print "preparing chunks..."
        pos = 0
        n_tune = 0
        x_chunks = np.array([], dtype=int)
        y_chunks = np.array([], dtype=int)
        for nl in nextnew[0]:
            while (pos + self.seq_length < nl):
                x_chunks = np.append(x_chunks, self.tensor[pos : pos + self.seq_length])
                y_chunks = np.append(y_chunks, self.tensor[pos + 1 : pos + self.seq_length + 1])
                pos += 1
            n_tune += 1
            print "done tune ", n_tune, "/", n_tunes
            pos += self.seq_length

        print "reshaping to seq_length..."
        x_chunks = np.copy(np.reshape(x_chunks, (-1, self.seq_length)))
        y_chunks = np.copy(np.reshape(y_chunks, (-1, self.seq_length)))

        print "truncating to match batch_size"
        x, y = x_chunks.shape
        self.num_batches = x / self.batch_size
        print "%i batches" % self.num_batches
        x_chunks.resize((self.num_batches * self.batch_size, self.seq_length))
        y_chunks.resize((self.num_batches * self.batch_size, self.seq_length))

        self.x_batches = np.vsplit(x_chunks, self.num_batches)
        self.y_batches = np.vsplit(y_chunks, self.num_batches)
        print "batches ready!"
Exemplo n.º 8
0
def mult_leave_out (random, percent):
    i = 1
    J = [[(5)]]
    for row in random:
        test,train = np.vsplit(random, np.array([i]))
        if i == 1:
            y_all, new_feature_number = ypred_leave_one_out(train, test, percent)
        if i != 1:
            if i < sample_n:
                train2,test = np.vsplit(test, np.array([i-1])) 
                train = np.vstack((train2,train))
                y_all, new_feature_number= ypred_leave_one_out(train, test, percent)
            if i > training_n:
                train,test = np.vsplit(random, np.array([training_n]))
                y_all, new_feature_number = ypred_leave_one_out(train, test, percent)
    
        J = np.append(J, np.array(y_all))
        i = i + 1
    J = np.delete(J,0,0)
    ground_truth = random[:,0]
    
    #print J, ground_truth
    slope, intercept, r_value, p_value, std_err = stats.linregress(ground_truth,J)
    #print r_value
    #print np.square(r_value)
    return r_value, std_err, p_value, slope, intercept, ground_truth, J, new_feature_number
Exemplo n.º 9
0
Arquivo: pyh2.py Projeto: slindal/ana
    def fold_eta(self):
        crap, self.ybins = np.split(self.ybins, 2)

        negeta, poseta = np.vsplit(self.Z, 2)
        negeta = negeta[::-1]
        self.Z = (negeta + poseta)/2

        negeta, poseta = np.vsplit(self.err2, 2)
        negeta = negeta[::-1]
        self.err2 = (negeta + poseta)/4
def kernelLDA(trainFile, validFile, labelsFile, gamma):

	trainData = np.loadtxt(open(trainFile))
	validationData = np.loadtxt(open(validFile))
	labels = np.loadtxt(open(labelsFile))

	print "Read training and validation data from files..."

	class1 = []
	class2 = []

	for i in range(len(labels)):
		if labels[i] == 1:
			class1.append(trainData[i])
		else:
			class2.append(trainData[i])

	class1 = np.vstack(tuple(class1))
	class2 = np.vstack(tuple(class2))

	data = np.concatenate((class1, class2))

	kernel_mat, kernelCentered_mat = getKernelMatrix(data, gamma)

	print "Kernel matrix obtained."

	K1 = np.vsplit(kernelCentered_mat, [class1.shape[0], data.shape[0]])[0]
	K2 = np.vsplit(kernelCentered_mat, [class1.shape[0], data.shape[0]])[1]

	mat_N1 = K1.T.dot( \
	 		(np.identity(K1.shape[0]) - (np.ones((K1.shape[0], K1.shape[0])) / K1.shape[0]))).dot( \
	 		K1)

	mat_N2 = K2.T.dot( \
	 		(np.identity(K2.shape[0]) - (np.ones((K2.shape[0], K2.shape[0])) / K2.shape[0]))).dot( \
	 		K2)

	N = mat_N1 + mat_N2

	M1 = np.average(K1, axis = 0)
	M2 = np.average(K2, axis = 0)

	#alpha = np.linalg.inv(N).dot(M2 - M1)
	alpha = (M2 - M1)

	getDimensionReducedData(alpha, kernelCentered_mat, 1, 'train', trainFile.split('_')[0])

	sq_distances_valid = cdist(data, validationData, 'sqeuclidean')
	kernel_mat_valid = np.exp(-1 * gamma * sq_distances_valid)

	getDimensionReducedData(alpha, kernel_mat_valid, 1, 'valid', trainFile.split('_')[0])

	print "Finished."
Exemplo n.º 11
0
def get_batches(X, Y, batch_size):
    '''
    Return a random data sample of size n from data X
    and their respective labels from Y.
    '''
    n_data, m_data = X.shape
    shuffled_inds = np.random.permutation(n_data)
    shuffled_data = X[shuffled_inds]
    shuffled_targ = Y[shuffled_inds]
    partitioned_data = np.vsplit(shuffled_data, n_data//batch_size)
    partitioned_targ = np.vsplit(shuffled_targ, n_data//batch_size)
    return partitioned_data, partitioned_targ
Exemplo n.º 12
0
def selectSubsample(matrixX, matrixY, splits):
    separateX = numpy.vsplit(matrixX, splits)
    separateY = numpy.vsplit(matrixY, splits)
    trainXFinal = []
    trainYFinal = []
    testXFinal = []
    testYFinal = []
    for i in range(len(separateX)):
        x_train, x_test, y_train, y_test = train_test_split(
            separateX[i], separateY[i], train_size=trainPercent, random_state=finalSeed)
        trainXFinal.append(x_train)
        trainYFinal.append(y_train)
        testXFinal.append(x_test)
        testYFinal.append(y_test)
    return [trainXFinal, trainYFinal, testXFinal, testYFinal]
Exemplo n.º 13
0
def cross_validate_spam():
  spam_filename = data_dir + "spambase/spambase.data"
  data = read_csv_as_numpy_matrix(spam_filename)[:4600,:]

  num_crosses = 10
  crosses = np.vsplit(data, 10)
  total_error = 0

  for i in xrange(num_crosses):
    train = None
    for j in xrange(num_crosses):
      if i != j:
        if train == None:
          train = deepcopy(crosses[j])
        else:
          train = np.vstack((train, crosses[j]))

    test = crosses[i]
    #pprint(test.shape)
    #pprint(train.shape)

    features = train[:,:56]
    truth = train[:,57]
    regression = get_linear_reg_function(features, truth)

    features = test[:,:56]
    truth = test[:,57]
    error = least_squares_error(regression, features, truth)
    total_error += error / truth.size

    pprint("cv: " + str(i))
    pprint(error / truth.size)

  pprint("avg error")
  pprint(total_error / 10.0)
Exemplo n.º 14
0
    def _process_annotations(self):
        # files from which annotations were extracted
        self._files = self._pd_annotations[0].as_matrix()
        self._n_files = len(self._files)

        # number of rectangles in each file
        self._n_objects = self._pd_annotations[1][0]
        rect_frame = self._pd_annotations.ix[:, 2:].as_matrix()
        rows = rect_frame.shape[0]
        rect_frame = np.vsplit(rect_frame, rows)

        # convert number-by-number columns into rectangles
        for row in rect_frame:
            row = row.reshape(-1)
            rects = np.array([], dtype=('i4, i4, i4, i4'))
            for i in range(0, self._n_objects):
                idx = i * 4
                # images are loaded by OpenCV with rows and columns mapped to y and x coordinates
                # rectangles are stored by annoations tool as x, y, dx, dy -> col, row, dcol, drow
                rect = row[idx + 1], row[idx], row[idx + 1] + row[idx + 3], row[idx] + row[idx + 2]
                if rects.size == 0:
                    rects = np.array(rect, dtype=('i4, i4, i4, i4'))
                else:
                    rects = np.append(rects, np.array(rect, dtype=('i4, i4, i4, i4')))
            
            self._rects = append_to_arr(self._rects, rects)                            
Exemplo n.º 15
0
def _least_square_evoked(data, events, event_id, tmin, tmax, sfreq):
    """Least square estimation of evoked response from data.

    Parameters
    ----------
    data : ndarray, shape (n_channels, n_times)
        The data to estimates evoked
    events : ndarray, shape (n_events, 3)
        The events typically returned by the read_events function.
        If some events don't match the events of interest as specified
        by event_id, they will be ignored.
    event_id : dict
        The id of the events to consider
    tmin : float
        Start time before event.
    tmax : float
        End time after event.
    sfreq : float
        Sampling frequency.

    Returns
    -------
    evokeds_data : dict of ndarray
        A dict of evoked data for each event type in event_id.
    toeplitz : dict of ndarray
        A dict of toeplitz matrix for each event type in event_id.
    """
    nmin = int(tmin * sfreq)
    nmax = int(tmax * sfreq)

    window = nmax - nmin
    n_samples = data.shape[1]
    toeplitz_mat = dict()
    full_toep = list()
    for eid in event_id:
        # select events by type
        ix_ev = events[:, -1] == event_id[eid]

        # build toeplitz matrix
        trig = np.zeros((n_samples, 1))
        ix_trig = (events[ix_ev, 0]) + nmin
        trig[ix_trig] = 1
        toep_mat = linalg.toeplitz(trig[0:window], trig)
        toeplitz_mat[eid] = toep_mat
        full_toep.append(toep_mat)

    # Concatenate toeplitz
    full_toep = np.concatenate(full_toep)

    # least square estimation
    predictor = np.dot(linalg.pinv(np.dot(full_toep, full_toep.T)), full_toep)
    all_evokeds = np.dot(predictor, data.T)
    all_evokeds = np.vsplit(all_evokeds, len(event_id))

    # parse evoked response
    evoked_data = dict()
    for idx, eid in enumerate(event_id):
        evoked_data[eid] = all_evokeds[idx].T

    return evoked_data, toeplitz_mat
Exemplo n.º 16
0
def R_z(x, y, z, alpha=0):
    """
    Rotation matrix for rotation about the z axis

    Parameters
    ----------
    x : `np.ndarray` with dims (1, N)
    y : `np.ndarray` with dims (1, N)
    z : `np.ndarray` with dims (1, N)
    alpha : float
        angle [radians] to rotate about the `z` axis counterclockwise

    Returns
    -------
    x2 : `np.ndarray` with dims (1, N)
        Rotated x positions
    y2 : `np.ndarray` with dims (1, N)
        Rotated y positions
    z2 : `np.ndarray` with dims (1, N)
        Rotated z positions
    """
    original_shape = x.shape
    xyz = np.vstack([x.ravel(), y.ravel(), z.ravel()])
    r_z = np.array([[np.cos(alpha), np.sin(alpha), 0],
                    [-np.sin(alpha), np.cos(alpha), 0],
                    [0, 0, 1]])
    new_xyz = np.dot(r_z, xyz)
    x2, y2, z2 = np.vsplit(new_xyz, 3)
    x2.resize(original_shape)
    y2.resize(original_shape)
    z2.resize(original_shape)
    return x2, y2, z2
Exemplo n.º 17
0
def measure_e1e2R2_cube(cube, sigma=None, xc_guess=None, yc_guess=None, info=False,
                        warn=False, tol=1.e-4, maxnits=100 ,skysub=True ,multi=False):
    """Apply measure_e1e2R2() to a cube of spot images.

    Outputs (e1, e2, R^2, xcentroid, ycentroid, dxcentroid, dycentroid), each as numpy arrays
    of length N where N is the number of spots in the cube.
    """
    from functools import partial
    worker_func = partial(measure_e1e2R2, sigma=sigma, xc_guess=xc_guess, yc_guess=yc_guess,
                          info=info, warn=warn, tol=tol, maxnits=maxnits ,skysub=skysub)

    if multi:
        from multiprocessing import Pool
        p = Pool()
        mymap = p.map
    else:
        mymap = map

    output = np.array(mymap(worker_func, np.vsplit(cube, cube.shape[0])))

    e1, e2, R2, xc, yc, dxc, dyc = (output[:, 0], output[:, 1], output[:, 2], output[:, 3],
                                    output[:, 4], output[:, 5], output[:, 6])

    if multi:
        p.close()
        p.join()

    return e1, e2, R2, xc, yc, dxc, dyc
Exemplo n.º 18
0
def predict_old(test):
    img = cv2.imread('digits.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # now we split the image to 5000 cells, each 20*20 size
    cells = [np.hsplit(row, 100) for row in np.vsplit(gray,50)]

    # Make it into a Numpy array. It size will be (50,100,20,20)
    x = np.array(cells)

    # now we prepare train_data and test_data
    train = x[:,:50].reshape(-1,400).astype(np.float32)     #Gives a new shape to an array without changing its data.
    if test==None: test = x[:, 50:100].reshape(-1,400).astype(np.float32)
    # create labels for train and test data
    k = np.arange(10)

    train_labels = np.repeat(k,250)[:, np.newaxis]  # Repeat elements of an array.
    test_labels = train_labels.copy()

    #cv2.KNearest.find_nearest(samples, k[, results[, neighborResponses[, dists]]]) → retval, results, neighborResponses, dists
    ret,result,neighbours,dist = knn.findNearest(test.reshape(-1,400).astype(np.float32),k=5)
    print '2-----:',result


    # Now we check the accuracy of classification
    # For that, compare the result with test_labels and check which are wrong
    matches = result==test_labels
    correct = np.count_nonzero(matches) # Counts the number of non-zero values in the array a.

    accuracy = correct*100.0/result.size
    print 'accuracy:',accuracy

    np.savez('knn_data.npz',train=train,train_labels=train_labels)
Exemplo n.º 19
0
    def evaluate_local_remainder_at(self, grid, position, diagonal_component=None, entry=None):
        r"""Numerically evaluate the non-quadratic remainder :math:`W(x)` of the quadratic
        approximation :math:`U(x)` of the potential's eigenvalue :math:`\lambda(x)` at the
        given nodes :math:`\Gamma`.

        :param grid: The grid nodes :math:`\Gamma` the remainder :math:`W` gets evaluated at.
        :param position: The point :math:`q \in \mathbb{R}^D` where the Taylor series is computed.
        :param diagonal_component: Dummy parameter that has no effect here.
        :keyword entry: Dummy parameter that has no effect here.
        :return: A list with a single entry consisting of an ``ndarray`` containing the
                 values of :math:`W(\Gamma)`. The array is of shape :math:`(1,|\Gamma|)`.
        """
        grid = self._grid_wrap(grid)
        position = numpy.atleast_2d(position)

        # Evaluate the remainder at the given nodes
        args = grid.get_nodes(split=True) + numpy.vsplit(position, position.shape[0])
        values = self._remainder_n[0](*args)

        # Test for potential being constant
        if numpy.atleast_1d(values).shape == (1,):
            values = values * numpy.ones(grid.get_number_nodes(), dtype=numpy.complexfloating)

        # Put the result in correct shape (1, #gridnodes)
        N = grid.get_number_nodes(overall=True)
        result = [ values.reshape((1,N)) ]

        # TODO: Consider unpacking single ndarray iff entry != None
        if entry is not None:
            result = result[0]

        return result
Exemplo n.º 20
0
    def genindices(self, arrayin):
        """Generate indices for splitting array."""
        retval = dict()
        # run the 'trim' program
        # need to split if it's too long!
        splitlist = tuple([x for x in xrange(CLIDT.indexmaxsize, arrayin.shape[0], CLIDT.indexmaxsize)])
        indexinc = 0
        for chunk in np.vsplit(arrayin, splitlist):
            chunkarr = cla.to_device(self.queue, np.asarray(chunk, dtype=np.int32))
            template = cla.empty_like(chunkarr)
            event = self.program.trim(
                self.queue, chunkarr.shape, None, chunkarr.data, template.data, np.int32(self.split)
            )
            try:
                event.wait()
            except cl.RuntimeError, inst:
                errstr = inst.__str__()
                if errstr == "clWaitForEvents failed: out of resources":
                    print "OpenCL timed out, probably due to the display manager."
                    print "Disable your display manager and try again!"
                    print "If that does not work, rerun with OpenCL disabled."
                else:
                    raise cl.RuntimeError, inst
                sys.exit(1)

            for index, elem in enumerate(template.get()):
                splitkey = tuple([x for x in elem])
                try:
                    retval[splitkey]
                except KeyError:
                    retval[splitkey] = []
                retval[splitkey].append(index + indexinc)
            indexinc += CLIDT.indexmaxsize
Exemplo n.º 21
0
 def deconcatenate(self, array):
     sizes = [np.size(x, 0) for x in self.dat]
     indeces = np.cumsum(sizes)
     if np.ndim(array) == 1:
         return np.array(np.split(array, indeces[:-1]))
     else:
         return np.array(np.vsplit(array, indeces[:-1]))
Exemplo n.º 22
0
 def reduceMean(self, factor=3, ceil=False):
  """
  for a square matrix
  """
  if self.shape[0] != self.shape[1]:
   print 'Warning: the matrix is not a square matrix'
   return self.matrix
  else:
   s = self.shape[0]/factor
   hs = numpy.hsplit(self.matrix, s)
   vhs = []
   for e in hs:
    vhs.append(numpy.vsplit(e,s))
   means = []
   for m in vhs:
    for e in m:
     if ceil:
      means.append(int(math.ceil(numpy.mean(e))))
     else:
      means.append(numpy.mean(e))
   rm = numpy.zeros((s,s))
   ii = itertools.cycle(range(s))
   ij = itertools.cycle(range(s))
   for e in means:
    i = ii.next()
    if i == 0:
     j = ij.next()
    rm[i,j]=e
   return rm
Exemplo n.º 23
0
def get_intra_design(mat_file, n_scans, contrasts, memory=Memory(None)):
    mat = memory.cache(load_matfile)(mat_file)['SPM']
    doc = {}

    design_matrix = mat.xX.X.tolist()           # xX: model
    conditions = [str(i) for i in mat.xX.name]

    n_sessions = len(n_scans)
    design_matrices = np.vsplit(design_matrix, np.cumsum(n_scans[:-1]))
    conditions = np.array(conditions)

    sessions_dm = []
    sessions_contrast = {}
    for i, dm in zip(range(n_sessions), design_matrices):
        mask = np.array(
            [cond.startswith('Sn(%s)' % (i + 1)) for cond in conditions])
        sessions_dm.append(dm[:, mask][:, :-1].tolist())

        for contrast_id in contrasts:
            sessions_contrast.setdefault(contrast_id, []).append(
                np.array(contrasts[contrast_id])[mask][:-1].tolist())

    doc['design_matrices'] = sessions_dm
    doc['contrasts'] = sessions_contrast
    return doc
Exemplo n.º 24
0
	def __init__(self, name, paneSize, colsrows) :
		fullsize = [a*b for a,b in zip(paneSize,colsrows)]
		pygame.init()
		pygame.display.set_caption(name)
		pygame.display.set_mode(fullsize,
#			pygame.DOUBLEBUF|
#			pygame.HWSURFACE|
#			pygame.FULLSCREEN|
#			pygame.OPENGL|
			0)
		self.screen = pygame.display.get_surface()
		self.colsrows = colsrows
		self.screenBuffer = pygame.surfarray.pixels2d(self.screen).swapaxes(0,1)
		self.splits = [np.hsplit(v,self.colsrows[0]) for v in np.vsplit(self.screenBuffer,self.colsrows[1])]
		self.videoSink = None
		self.letterR = np.array([[c!=" " for c in row] for row in [
			"******* ",
			"********",
			"**    **",
			"**   ***",
			"******* ",
			"******  ",
			"**  *** ",
			"**   ***",
			"**    **",
			"**    **",
		]], dtype=bool)
		self.blink = 0
Exemplo n.º 25
0
def dumpMinima(recipe):
    worker = recipe.getWorker("ThicknessModeller")
    simulation = worker.plugCalcAbsorption.getResult()
    worker = recipe.getWorker("AddColumn")
    table = worker.plugCompute.getResult(subscriber=TextSubscriber("Add Column"))
    index = table[u"pixel"]
    xPos = table[u"x-position"]
    yPos = table[u"y-position"]
    thickness = table[u"thickness"]
    cols = [index, xPos, yPos, thickness]
    worker = recipe.getWorker("MRA Exp")
    minimaPos = worker.plugMra.getResult()[r'\lambda_{min}'].inUnitsOf(simulation.dimensions[1])
    import numpy
    data = numpy.vstack([ c.data for c in cols]
                        + numpy.vsplit(minimaPos.data, len(minimaPos.data))).transpose()
    import fmfile.fmfgen
    factory = fmfile.fmfgen.gen_factory(out_coding='cp1252', eol='\r\n')
    fc = factory.gen_fmf()
    fc.add_reference_item('author', "Kristian")
    tab = factory.gen_table(data)
    for c in cols:
        tab.add_column_def(c.longname, c.shortname, str(c.unit))
    for i in xrange(len(minimaPos.data)):
        tab.add_column_def("minimaPos%i"%i, "p_%i"%i)
    fc.add_table(tab)
    print str(fc)
Exemplo n.º 26
0
 def __setitem__(self, indexes, change):
     pack = self.pack()
     pack.__setitem__(indexes, change)
     varstmp = np.vsplit(pack, pack.shape[0])
     for i in range(len(varstmp)):
         self.variables[i][:] = varstmp[i]
     self.sync()
    def __init__(self):
        # box with note
        self.xbox = 100
        self.ybox = 100
        # number pattern and count
        self.xcount = 250   # max 250! (more => memory overflow, maybe more possible, but...)
        self.yvector = ['#', '1', '2', '4', '8', '16', 'b', 'k', 'o', 'p', 'p4', 'p8', 'p16', 'pnt', 't', 't2', 't3', 't4', 't6', 't8', 't22', 't24', 't34', 't44', 't68', 'tc', 'tnc']

        self.ycount = len(self.yvector)
        self.knn = cv2.KNearest()
        # train from knn_data.npz prepared file
        if os.path.exists('knn/knn_data.npz'):
            with np.load('knn/knn_data.npz') as data:
                train = data['train']
                train_labels = data['train_labels']
                self.knn.train(train, train_labels)
        elif os.path.exists('knn/dataset.png'):
            # train from image dataset.png
            gray = cv2.imread('knn/dataset.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
            cells = [np.hsplit(row, self.xcount) for row in np.vsplit(gray, self.ycount)]
            x = np.array(cells)
            train = x[:, :self.xcount].reshape(-1, self.xbox * self.ybox).astype(
                np.float32)

            k = np.arange(self.ycount)
            train_labels = np.repeat(k,  self.xcount )[:, np.newaxis]

            self.knn.train(train, train_labels)

        else:
            print "ERROR: not found files knn_data.npz or dataset.png need for classifier!"
Exemplo n.º 28
0
Arquivo: other.py Projeto: jfhu/kamera
 def process_mirror(cls, img, mode):
     if mode == 'Vertical':
         org = numpy.asarray(img)
         flip = numpy.fliplr(org)
         data1 = numpy.hsplit(org, 2)
         data2 = numpy.hsplit(flip, 2)
         data = numpy.hstack((data1[0], data2[1]))
         img = Image.fromarray(data)
     elif mode == 'Horizontal':
         org = numpy.asarray(img)
         flip = numpy.flipud(org)
         data1 = numpy.vsplit(org, 2)
         data2 = numpy.vsplit(flip, 2)
         data = numpy.vstack((data1[0], data2[1]))
         img = Image.fromarray(data)
     return img
Exemplo n.º 29
0
def validate_sentence(session, model, validation_batch, encoder_state, current_step):
    encoder_inputs, single_decoder_inputs, decoder_weights = validation_batch.next()
    print(BatchGenerator.batches2string(encoder_inputs))
    print(BatchGenerator.batches2string(single_decoder_inputs))
    # replicate to full batch size so we have multiple results agains the whole state
    encoder_inputs = [np.repeat(x, BATCH_SIZE, axis=0) for x in encoder_inputs]
    decoder_inputs = [np.repeat(x, BATCH_SIZE, axis=0) for x in single_decoder_inputs]
    decoder_weights = [np.repeat(x, BATCH_SIZE, axis=0) for x in decoder_weights]
    # _, eval_loss, prediction = model.step(sess, current_step - 1, encoder_inputs, decoder_inputs,
    #                                      decoder_weights, enc_state[-1:], 1.0, True)
    _, eval_loss, prediction = model.step(session, current_step - 1, encoder_inputs, decoder_inputs,
                                          decoder_weights, encoder_state, 1.0, True)
    # split into 'no of batches' list then average across batches
    reshaped = np.reshape(prediction, (prediction.shape[0] / BATCH_SIZE, BATCH_SIZE, prediction.shape[1]))
    averaged = np.mean(reshaped, axis=1)
    # now roll as in case of single batch
    rolled = np.rollaxis(np.asarray([averaged]), 1, 0)
    splitted = np.vsplit(rolled, rolled.shape[0])
    squeezed = [np.squeeze(e,0) for e in splitted]
    print(BatchGenerator.batches2string(squeezed))
    # compute character to character perplexity
    val_perp = float(np.exp(BatchGenerator.logprob(np.concatenate(squeezed),
                                                   np.concatenate(single_decoder_inputs[1:]))))
    print('--validation perp.: %.2f' % val_perp)
    return val_perp
Exemplo n.º 30
0
def _least_square_evoked(epochs_data, events, tmin, sfreq):
    """Least square estimation of evoked response from epochs data.

    Parameters
    ----------
    epochs_data : array, shape (n_channels, n_times)
        The epochs data to estimate evoked.
    events : array, shape (n_events, 3)
        The events typically returned by the read_events function.
        If some events don't match the events of interest as specified
        by event_id, they will be ignored.
    tmin : float
        Start time before event.
    sfreq : float
        Sampling frequency.

    Returns
    -------
    evokeds : array, shape (n_class, n_components, n_times)
        An concatenated array of evoked data for each event type.
    toeplitz : array, shape (n_class * n_components, n_channels)
        An concatenated array of toeplitz matrix for each event type.
    """

    n_epochs, n_channels, n_times = epochs_data.shape
    tmax = tmin + n_times / float(sfreq)

    # Deal with shuffled epochs
    events = events.copy()
    events[:, 0] -= events[0, 0] + int(tmin * sfreq)

    # Contruct raw signal
    raw = _construct_signal_from_epochs(epochs_data, events, sfreq, tmin)

    # Compute the independent evoked responses per condition, while correcting
    # for event overlaps.
    n_min, n_max = int(tmin * sfreq), int(tmax * sfreq)
    window = n_max - n_min
    n_samples = raw.shape[1]
    toeplitz = list()
    classes = np.unique(events[:, 2])
    for ii, this_class in enumerate(classes):
        # select events by type
        sel = events[:, 2] == this_class

        # build toeplitz matrix
        trig = np.zeros((n_samples, 1))
        ix_trig = (events[sel, 0]) + n_min
        trig[ix_trig] = 1
        toeplitz.append(linalg.toeplitz(trig[0:window], trig))

    # Concatenate toeplitz
    toeplitz = np.array(toeplitz)
    X = np.concatenate(toeplitz)

    # least square estimation
    predictor = np.dot(linalg.pinv(np.dot(X, X.T)), X)
    evokeds = np.dot(predictor, raw.T)
    evokeds = np.transpose(np.vsplit(evokeds, len(classes)), (0, 2, 1))
    return evokeds, toeplitz
Exemplo n.º 31
0
    [6, 0, 0, 1, 9, 5, 0, 0, 0],
    [0, 9, 8, 0, 0, 0, 0, 6, 0],
    [8, 0, 0, 0, 6, 0, 0, 0, 3],
    [4, 0, 0, 8, 0, 3, 0, 0, 1],
    [7, 0, 0, 0, 2, 0, 0, 0, 6],
    [0, 6, 0, 0, 0, 0, 2, 8, 0],
    [0, 0, 0, 4, 1, 9, 0, 0, 5],
    [0, 0, 0, 0, 8, 0, 0, 7, 9],
]


if __name__ == "__main__":
    p = np.array(puzzle)
    puzzle_groups = []

    for split_row_thirds in np.vsplit(p, 3):
        for split_col_thirds in np.hsplit(split_row_thirds, 3):
            puzzle_groups.append(split_col_thirds.flatten())

    print(puzzle_groups)

"""
[array([5, 3, 0, 6, 0, 0, 0, 9, 8]),
array([0, 7, 0, 1, 9, 5, 0, 0, 0]),
array([0, 0, 0, 0, 0, 0, 0, 6, 0]),
array([8, 0, 0, 4, 0, 0, 7, 0, 0]),
array([0, 6, 0, 8, 0, 3, 0, 2, 0]),
array([0, 0, 3, 0, 0, 1, 0, 0, 6]),
array([0, 6, 0, 0, 0, 0, 0, 0, 0]),
array([0, 0, 0, 4, 1, 9, 0, 8, 0]),
array([2, 8, 0, 0, 0, 5, 0, 7, 9])]
Exemplo n.º 32
0
c = [11, 12]

# stacking columns
print("\nColumn stacking:\n", np.column_stack((a, c)))

# concatenation method
print("\nConcatenating to 2nd axis:\n", np.concatenate((a, b), 1))

a = np.array([[1, 3, 5, 7, 9, 11], [2, 4, 6, 8, 10, 12]])

# horizontal splitting
print("Splitting along horizontal axis into 2 parts:\n", np.hsplit(a, 3))

# vertical splitting
print("\nSplitting along vertical axis into 2 parts:\n", np.vsplit(a, 2))

import numpy as np

a = np.array([1.0, 2.0, 3.0])

# Example 1
b = 2.0
print(a * b)

# Example 2
c = [2.0, 2.0, 2.0]
print(a * c)

a = np.array([0.0, 10.0, 20.0, 30.0])
b = np.array([0.0, 1.0, 2.0])
Exemplo n.º 33
0
#将a平铺
print('a平铺展开', a.flatten())
print(np.floor(a))

#将啊a,b横,纵向拼接
print('a,b横向拼接', np.hstack((a, b)))
print('a,b纵向拼接', np.vstack((a, b)))

#多个矩阵横纵向拼接
c = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]])
print('a,b,c横向拼接', np.concatenate((a, b, c), axis=1))
print('a,b,c纵向拼接', np.concatenate((a, b, c), axis=0))

#分隔矩阵,value必须要求均匀分隔
print('a矩阵横向分隔', np.hsplit(a, 3))
print('a矩阵纵向分隔', np.vsplit(a, 3))

#不均匀分隔
print('a矩阵横向不均匀分隔', np.array_split(a, 2, axis=1))
print('a矩阵纵向不均匀分隔', np.array_split(a, 2, axis=0))

#深拷贝与浅拷贝
#浅拷贝,由于array是复杂对象,直接赋值,相当于对整个复杂对象添加了一个指针,并未开辟新的内存
c = a
a[1, :] = [0, 0, 0]
print('a改变后', a)
print('c', c)

#在array这里,利用copy是实现深拷贝,但不推荐,因为其他地方copy则为浅拷贝
import copy
c = copy.copy(a)
Exemplo n.º 34
0
def run(params):
    "Business logic"
    input_shape = [
        int(k) for k in params.get('image_size', '300,300').split(',') if k
    ]
    model_name = params.get('model', 'resnet18')
    optimizer = params.get('optimizer', 'adam')
    epochs = int(params.get('epochs', 100))
    batch_size = int(params.get('batch_size', 32))
    fdir = params.get('fdir', os.getcwd())
    flabels = params.get('flabels', 'labels.csv')
    augmentation = params.get('augmentation', False)
    ext = params.get('image_ext', 'png')
    split = float(params.get('split', 0.7))
    mdir = params.get('mdir', '')
    quantize = params.get('quantize', False)

    lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=5,
                                   min_lr=0.5e-6)
    early_stopper = EarlyStopping(min_delta=0.001, patience=10)
    csv_logger = CSVLogger('%s_hep.csv' % model_name)

    # load data and split it into train/validataion/test samples
    x_data, y_data, nb_classes = load_data(fdir, flabels, ext)
    y_data = np_utils.to_categorical(y_data, nb_classes)

    # split input DF into train/test sets
    sval = int(round(split *
                     np.shape(x_data)[0]))  # take split percentage value
    x_train, x_rest = np.vsplit(
        x_data, [sval])  # split into x_train and rest to x_rest
    y_train = y_data[:sval]
    y_rest = y_data[sval:]
    # split input test DF into valid/test sets
    sval = int(round(0.5 * np.shape(x_rest)[0]))  # take split percentage value
    x_valid, x_test = np.vsplit(x_rest, [sval])  # split into two equal sets
    y_valid = y_rest[:sval]
    y_test = y_rest[sval:]

    ishape = (3, input_shape[0], input_shape[1])  # RGB image NxM size
    model = get_model(model_name, ishape, nb_classes)

    # useful printouts
    print("Input set: %s shape" % str(np.shape(x_data)))
    print("Train set: %s shape" % str(np.shape(x_train)))
    print("Test  set: %s shape" % str(np.shape(x_test)))
    print("Valid set: %s shape" % str(np.shape(x_valid)))
    print("# classes: %s" % nb_classes)
    print("y_train  : %s" % str(np.shape(y_train)))
    print("y_test   : %s" % str(np.shape(y_test)))
    print("y_valid  : %s" % str(np.shape(y_valid)))
    print("model    : %s" % model)
    for layer in model.layers:
        print("layer    : %s, %s" % (layer.name, layer.input_spec))
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
    if not augmentation:
        print('Not using data augmentation.')
        model.fit(x_train,
                  y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_data=(x_valid, y_valid),
                  shuffle=True,
                  callbacks=[lr_reducer, early_stopper, csv_logger])
    else:
        print('Using real-time data augmentation.')
        # This will do preprocessing and realtime data augmentation:
        datagen = ImageDataGenerator(
            featurewise_center=False,  # set input mean to 0 over the dataset
            samplewise_center=False,  # set each sample mean to 0
            featurewise_std_normalization=
            False,  # divide inputs by std of the dataset
            samplewise_std_normalization=False,  # divide each input by its std
            zca_whitening=False,  # apply ZCA whitening
            rotation_range=
            0,  # randomly rotate images in the range (degrees, 0 to 180)
            width_shift_range=
            0.1,  # randomly shift images horizontally (fraction of total width)
            height_shift_range=
            0.1,  # randomly shift images vertically (fraction of total height)
            horizontal_flip=True,  # randomly flip images
            vertical_flip=False)  # randomly flip images

        # Compute quantities required for featurewise normalization
        # (std, mean, and principal components if ZCA whitening is applied).
        datagen.fit(x_train)

        # Fit the model on the batches generated by datagen.flow().
        model.fit_generator(datagen.flow(x_train,
                                         y_train,
                                         batch_size=batch_size),
                            steps_per_epoch=x_train.shape[0] // batch_size,
                            validation_data=(x_valid, y_valid),
                            epochs=epochs,
                            verbose=1,
                            max_q_size=100,
                            callbacks=[lr_reducer, early_stopper, csv_logger])
    if mdir:
        try:
            os.makedirs(mdir)
        except:
            pass
        # save keras module
        tstamp = time.strftime('%Y%m%d', time.gmtime())
        kfile = os.path.join(mdir, 'keras_model_%s.h5' % tstamp)
        try:
            import h5py
            model.save(kfile)
            mfile = 'tf_model_%s.pb' % tstamp
            gfile = 'tf_graph_%s.pb' % tstamp
            save_tf_image(kfile, mdir, mfile, gfile, quantize)
        except ImportError:
            pass
Exemplo n.º 35
0
print(matrix_1 < matrix_2)
print(matrix_1 == matrix_2)
print(matrix_1 > matrix_2)

# Verify if all/any of elements are true for the given predicate:
print(np.all(matrix_1 <= matrix_2))
print(np.any(matrix_1 <= matrix_2))

# Divide and concatenate matrices:
matrix_1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix_2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Concatenate two matrices (add new rows):
print(np.concatenate((matrix_1, matrix_2)))
print(np.concatenate((matrix_1, matrix_2), axis=0))
# Concatenate two matrices (add new cols):
print(np.concatenate((matrix_1, matrix_2), axis=1))
# Concatenate vertically
print(np.vstack((matrix_1, matrix_2)))
# Concatenate horizontally
print(np.hstack((matrix_1, matrix_2)))
# Split matrix horizontally:
# Into 3 equal pieces
print(np.hsplit(matrix_1, 3))
# Over specified indexes: 0, n1, n2 ,..., nk, length-1
print(np.hsplit(matrix_1, [1, 3]))
# Split matrix vertically:
# Into 3 equal pieces
print(np.vsplit(matrix_1, 3))
# Over specified indexes: 0, n1, n2 ,..., nk, length-1
print(np.vsplit(matrix_1, [1, 3]))
Exemplo n.º 36
0
# merge

a = np.array([1, 1, 1])
b = np.array([2, 2, 2])
c = np.vstack((a, b))
c.shape
d = np.hstack((a, b))
d.shape

a[np.newaxis, :].shape
a[:, np.newaxis].shape
e = np.concatenate((a, b, a, b), axis=0)
A = np.arange(1, 25).reshape((4, 6))
np.split(A, 2, axis=0)
np.array_split(A, 4, axis=1)
np.vsplit(A, 2)
np.hsplit(A, 3)

# 数组数据处理
points1 = np.arange(-5, 5, 1)
points2 = np.arange(0, 10, 1)
xs, ys = np.meshgrid(points1, points2)
xs.shape

import matplotlib.pyplot as plt

z = np.sqrt(xs**2 + ys**2)
plt.imshow(z, cmap=plt.cm.gray)
plt.colorbar()

arr = np.random.randn(4, 4)
Exemplo n.º 37
0
    def simulateCallback(frame):
        global g_initFlag
        global forceShowFrame
        global forceApplyFrame
        global JsysPre
        global JsupPreL
        global JsupPreR
        global JsupPre
        global softConstPoint
        global stage

        motionModel.update(motion[frame])

        Kt, Kk, Kl, Kh, Ksc, Bt, Bl, Bh, Bsc = viewer.GetParam()
        
        Dt = 2*(Kt**.5)
        Dk = 2*(Kk**.5)
        Dl = 2*(Kl**.5)
        Dh = 2*(Kh**.5)
        Dsc = 2*(Ksc**.5)
                
        if Bsc == 0.0 :
            viewer.doc.showRenderer('softConstraint', False)
            viewer.motionViewWnd.update(1, viewer.doc)
        else:
            viewer.doc.showRenderer('softConstraint', True)
            renderer1 = viewer.doc.getRenderer('softConstraint')
            renderer1.rc.setLineWidth(0.1+Bsc*3)
            viewer.motionViewWnd.update(1, viewer.doc)
                        
        # tracking
        th_r = motion.getDOFPositions(frame)
        th = controlModel.getDOFPositions()
        dth_r = motion.getDOFVelocities(frame)
        dth = controlModel.getDOFVelocities()
        ddth_r = motion.getDOFAccelerations(frame)
        ddth_des = yct.getDesiredDOFAccelerations(th_r, th, dth_r, dth, ddth_r, Kt, Dt)
        ddth_c = controlModel.getDOFAccelerations()

        ype.flatten(ddth_des, ddth_des_flat)
        ype.flatten(dth, dth_flat)

        ype.flatten(ddth_c, ddth_c_flat)
        
        # jacobian 
        footCenterL = controlModel.getBodyPositionGlobal(supL)        
        footCenterR = controlModel.getBodyPositionGlobal(supR)
                        
        refFootL = motionModel.getBodyPositionGlobal(supL)        
        refFootR = motionModel.getBodyPositionGlobal(supR)
        
        footCenter = footCenterL + (footCenterR - footCenterL)/2.0
        footCenter[1] = 0.     
        
        footCenter_ref = refFootL + (refFootR - refFootL)/2.0
        #footCenter_ref[1] = 0.      
        
        positionFootL = [None]*footPartNum
        positionFootR = [None]*footPartNum
        for i in range(footPartNum):
            positionFootL[i] = controlModel.getBodyPositionGlobal(indexFootL[i])
            positionFootR[i] = controlModel.getBodyPositionGlobal(indexFootR[i])
        
        linkPositions = controlModel.getBodyPositionsGlobal()
        linkVelocities = controlModel.getBodyVelocitiesGlobal()
        linkAngVelocities = controlModel.getBodyAngVelocitiesGlobal()
        linkInertias = controlModel.getBodyInertiasGlobal()

        jointPositions = controlModel.getJointPositionsGlobal()
        jointAxeses = controlModel.getDOFAxeses()
        
        CM = yrp.getCM(linkPositions, linkMasses, totalMass)
        dCM = yrp.getCM(linkVelocities, linkMasses, totalMass)
        CM_plane = copy.copy(CM); CM_plane[1]=0.
        dCM_plane = copy.copy(dCM); dCM_plane[1]=0.
                
        linkPositions_ref = motionModel.getBodyPositionsGlobal()
        CM_ref = yrp.getCM(linkPositions_ref, linkMasses, totalMass)
        CM_plane_ref = copy.copy(CM_ref)
        CM_plane_ref[1] = 0.
        
        P = ymt.getPureInertiaMatrix(TO, linkMasses, linkPositions, CM, linkInertias)
        dP = ymt.getPureInertiaMatrixDerivative(dTO, linkMasses, linkVelocities, dCM, linkAngVelocities, linkInertias)

        yjc.computeJacobian2(Jsys, DOFs, jointPositions, jointAxeses, linkPositions, allLinkJointMasks)       
        yjc.computeJacobianDerivative2(dJsys, DOFs, jointPositions, jointAxeses, linkAngVelocities, linkPositions, allLinkJointMasks)
        
        if g_initFlag == 0:
            softConstPoint = controlModel.getBodyPositionGlobal(constBody)
            softConstPoint[1] -= .3
            g_initFlag = 1

        yjc.computeJacobian2(jFootL[0], DOFs, jointPositions, jointAxeses, [positionFootL[0]], jointMasksFootL[0])
        yjc.computeJacobianDerivative2(dJFootL[0], DOFs, jointPositions, jointAxeses, linkAngVelocities, [positionFootL[0]], jointMasksFootL[0], False)
        
        yjc.computeJacobian2(jFootR[0], DOFs, jointPositions, jointAxeses, [positionFootR[0]], jointMasksFootR[0])
        yjc.computeJacobianDerivative2(dJFootR[0], DOFs, jointPositions, jointAxeses, linkAngVelocities, [positionFootR[0]], jointMasksFootR[0], False)
        
        bodyIDs, contactPositions, contactPositionLocals, contactForces = vpWorld.calcPenaltyForce(bodyIDsToCheck, mus, Ks, Ds)
        CP = yrp.getCP(contactPositions, contactForces)

        for i in range(len(bodyIDsToCheck)) :
            controlModel.SetBodyColor(bodyIDsToCheck[i], 0, 0, 0)

        contactFlagFootL = [0]*footPartNum
        contactFlagFootR = [0]*footPartNum

        for i in range(len(bodyIDs)) :
            controlModel.SetBodyColor(bodyIDs[i], 255, 105, 105)
            index = controlModel.id2index(bodyIDs[i])
            for j in range(len(indexFootL)):
                if index == indexFootL[j]:
                    contactFlagFootL[j] = 1
                    if j != 0:
                        yjc.computeJacobian2(jFootL[j], DOFs, jointPositions, jointAxeses, [positionFootL[j]], jointMasksFootL[j])
                        yjc.computeJacobianDerivative2(dJFootL[j], DOFs, jointPositions, jointAxeses, linkAngVelocities, [positionFootL[j]], jointMasksFootL[j], False)
                    break
            for j in range(len(indexFootR)):
                if index == indexFootR[j]:
                    contactFlagFootR[j] = 1
                    if j != 0:
                        yjc.computeJacobian2(jFootR[j], DOFs, jointPositions, jointAxeses, [positionFootR[j]], jointMasksFootR[j])
                        yjc.computeJacobianDerivative2(dJFootR[j], DOFs, jointPositions, jointAxeses, linkAngVelocities, [positionFootR[j]], jointMasksFootR[j], False)
                    break

        #
        if checkAll(contactFlagFootL, 0) == 1 and checkAll(contactFlagFootR, 0) == 1:
            footCenter = footCenter
        elif checkAll(contactFlagFootL, 0) == 1 :
            footCenter = footCenterR
        elif checkAll(contactFlagFootR, 0) == 1 :
            footCenter = footCenterL
        footCenter[1] = 0.  

        desForeSupLAcc = [0,0,0]
        desForeSupRAcc = [0,0,0]
                
        totalNormalForce = [0,0,0]
    
        for i in range(len(contactForces)):
            totalNormalForce[0] += contactForces[i][0]
            totalNormalForce[1] += contactForces[i][1]
            totalNormalForce[2] += contactForces[i][2]
                                   
        # linear momentum
        CM_ref_plane = footCenter
        dL_des_plane = Kl*totalMass*(CM_ref_plane - CM_plane) - Dl*totalMass*dCM_plane
    
        # angular momentum
        CP_ref = footCenter

        timeStep = 30.
        if CP_old[0]==None or CP==None:
            dCP = None
        else:
            dCP = (CP - CP_old[0])/(1/timeStep)
        CP_old[0] = CP            
        
        if CP!=None and dCP!=None:
            ddCP_des = Kh*(CP_ref - CP) - Dh*(dCP)
            CP_des = CP + dCP*(1/timeStep) + .5*ddCP_des*((1/timeStep)**2)
            dH_des = np.cross((CP_des - CM), (dL_des_plane + totalMass*mm.s2v(wcfg.gravity)))            
            #dH_des = np.cross((CP_des - CM_plane), (dL_des_plane + totalMass*mm.s2v(wcfg.gravity)))
        else:
            dH_des = None
 
        CMP = yrp.getCMP(contactForces, CM)
        r = [0,0,0]
        if CP!= None and np.any(np.isnan(CMP))!=True :
            r = CP - CMP

        # momentum matrix
        RS = np.dot(P, Jsys)
        R, S = np.vsplit(RS, 2)
        
        rs = np.dot((np.dot(dP, Jsys) + np.dot(P, dJsys)), dth_flat)
        r_bias, s_bias = np.hsplit(rs, 2)


        ##############################
        # soft point constraint
               
        P_des = softConstPoint
        P_cur = controlModel.getBodyPositionGlobal(constBody)
        dP_des = [0, 0, 0]
        dP_cur = controlModel.getBodyVelocityGlobal(constBody)
        ddP_des1 = Ksc*(P_des - P_cur) - Dsc*(dP_cur - dP_des)

        r = P_des - P_cur
        I = np.vstack(([1,0,0],[0,1,0],[0,0,1]))
        Z = np.hstack((I, mm.getCrossMatrixForm(-r)))
          
        yjc.computeJacobian2(Jconst, DOFs, jointPositions, jointAxeses, [softConstPoint], constJointMasks)
        JL, JA = np.vsplit(Jconst, 2)
        Q1 = np.dot(Z, Jconst)
                  
        q1 = np.dot(JA, dth_flat)
        q2 = np.dot(mm.getCrossMatrixForm(q1), np.dot(mm.getCrossMatrixForm(q1), r))
        
        yjc.computeJacobianDerivative2(dJconst, DOFs, jointPositions, jointAxeses, linkAngVelocities, [softConstPoint], constJointMasks, False)
        q_bias1 = np.dot(np.dot(Z, dJconst), dth_flat) + q2
        
        ##############################
        
         
        flagContact = True
        if dH_des==None or np.any(np.isnan(dH_des)) == True:
            flagContact = False 
            viewer.doc.showRenderer('rd_grf_des', False)
            viewer.motionViewWnd.update(1, viewer.doc)
        else:
            viewer.doc.showRenderer('rd_grf_des', True)
            viewer.motionViewWnd.update(1, viewer.doc)
        '''
        0 : initial
        1 : contact
        2 : fly
        3 : landing
        '''

        #MOTION = FORWARD_JUMP
        if mit.MOTION == mit.FORWARD_JUMP :
            frame_index = [136, 100]
            #frame_index = [100000, 100000]
        elif mit.MOTION == mit.TAEKWONDO:
            #frame_index = [130, 100]
            frame_index = [100000, 100000]
        else :
            frame_index = [1000000, 1000000]
        
        #MOTION = TAEKWONDO 
        #frame_index = [135, 100]

        '''
        if frame > 300 :
            if stage != DYNAMIC_BALANCING:
                print("#", frame,"-DYNAMIC_BALANCING")
            stage = DYNAMIC_BALANCING
            Kk = Kk*1
            Dk = 2*(Kk**.5)        
        '''
        if frame > frame_index[0] :
            if stage != POWERFUL_BALANCING:
                print("#", frame,"-POWERFUL_BALANCING")
            stage = POWERFUL_BALANCING
            Kk = Kk*2
            Dk = 2*(Kk**.5)            
        elif frame > frame_index[1]:
            if stage != MOTION_TRACKING:
                print("#", frame,"-MOTION_TRACKING")
            stage = MOTION_TRACKING

        trackingW = w

        if stage == MOTION_TRACKING:
            trackingW = w2
            Bt = Bt*2

        # optimization
                
        mot.addTrackingTerms(problem, totalDOF, Bt, trackingW, ddth_des_flat)
                
        mot.addSoftPointConstraintTerms(problem, totalDOF, Bsc, ddP_des1, Q1, q_bias1)

        if flagContact == True:
            if stage != MOTION_TRACKING:
                mot.addLinearTerms(problem, totalDOF, Bl, dL_des_plane, R, r_bias) 
                mot.addAngularTerms(problem, totalDOF, Bh, dH_des, S, s_bias)
            
        a_sup_2 = [None]
        Jsup_2 = [None]
        dJsup_2 = [None]
        ##############################
        # Hard constraint        
        if stage != MOTION_TRACKING:
            Kk2 = Kk * 2.0
        else :
            Kk2 = Kk * 5.
                    
        Dk2 = 2*(Kk2**.5)

        desLinearAccL, desPosL = getDesFootLinearAcc(motionModel, controlModel, supL, ModelOffset, CM_ref, CM, Kk2, Dk2) 
        desLinearAccR, desPosR = getDesFootLinearAcc(motionModel, controlModel, supR, ModelOffset, CM_ref, CM, Kk2, Dk2) 

        desAngularAccL = getDesFootAngularAcc(motionModel, controlModel, supL, Kk2, Dk2)
        desAngularAccR = getDesFootAngularAcc(motionModel, controlModel, supR, Kk2, Dk2)
        
        a_sup_2 = np.hstack(( np.hstack((desLinearAccL, desAngularAccL)), np.hstack((desLinearAccR, desAngularAccR)) ))
 
        Jsup_2 = np.vstack((jFootL[0], jFootR[0]))
        dJsup_2 = np.vstack((dJFootL[0], dJFootR[0]))   
                    
        rd_DesPosL[0] = desPosL.copy()
        rd_DesPosR[0] = desPosR.copy()

        ##############################
        
        ##############################
        # Additional constraint          

        if stage != MOTION_TRACKING:
            Kk2 = Kk * 1.5
            Dk2 = 2*(Kk2**.5)
            desForePosL = [0,0,0]
            desForePosR = [0,0,0]
            desRearPosL = [0,0,0]
            desRearPosR = [0,0,0]

            for i in range(1, footPartNum) :
                if contactFlagFootL[i] == 1:
                    desLinearAccL, desForePosL = getDesFootLinearAcc(motionModel, controlModel, indexFootL[i], ModelOffset, CM_ref, CM, Kk2, Dk2) 
                    desAngularAccL = getDesFootAngularAcc(motionModel, controlModel, indexFootL[i], Kk2, Dk2)
                    a_sup_2 = np.hstack(( a_sup_2, np.hstack((desLinearAccL, desAngularAccL)) ))
                    Jsup_2 = np.vstack(( Jsup_2, jFootL[i] ))
                    dJsup_2 = np.vstack(( dJsup_2, dJFootL[i] ))                
                if contactFlagFootR[i] == 1:
                    desLinearAccR, desForePosR = getDesFootLinearAcc(motionModel, controlModel, indexFootR[i], ModelOffset, CM_ref, CM, Kk2, Dk2) 
                    desAngularAccR = getDesFootAngularAcc(motionModel, controlModel, indexFootR[i], Kk2, Dk2)
                    a_sup_2 = np.hstack(( a_sup_2, np.hstack((desLinearAccR, desAngularAccR)) ))            
                    Jsup_2 = np.vstack(( Jsup_2, jFootR[i] ))
                    dJsup_2 = np.vstack(( dJsup_2, dJFootR[i] ))
            
            rd_DesForePosL[0] = desForePosL
            rd_DesForePosR[0] = desForePosR
            rd_DesRearPosL[0] = desRearPosL
            rd_DesRearPosR[0] = desRearPosR
        ##############################
        

        mot.setConstraint(problem, totalDOF, Jsup_2, dJsup_2, dth_flat, a_sup_2)
        

        r = problem.solve()
        problem.clear()
        ype.nested(r['x'], ddth_sol)
                      
        rootPos[0] = controlModel.getBodyPositionGlobal(selectedBody)
        localPos = [[0, 0, 0]]   
                
        for i in range(stepsPerFrame):
            # apply penalty force
            bodyIDs, contactPositions, contactPositionLocals, contactForces = vpWorld.calcPenaltyForce(bodyIDsToCheck, mus, Ks, Ds)
     
            vpWorld.applyPenaltyForce(bodyIDs, contactPositionLocals, contactForces)                      
            
            extraForce[0] = viewer.GetForce()
            if (extraForce[0][0] != 0 or extraForce[0][1] != 0 or extraForce[0][2] != 0) :
                forceApplyFrame += 1
                #vpWorld.applyPenaltyForce(selectedBodyId, localPos, extraForce)
                controlModel.applyBodyForceGlobal(selectedBody, extraForce[0])
                applyedExtraForce[0] = extraForce[0]
            
            if forceApplyFrame*wcfg.timeStep > 0.1:
                viewer.ResetForce()
                forceApplyFrame = 0            

            controlModel.setDOFAccelerations(ddth_sol)
            
            controlModel.solveHybridDynamics()
            '''
            extraForce[0] = viewer.GetForce()
            if (extraForce[0][0] != 0 or extraForce[0][1] != 0 or extraForce[0][2] != 0) :
                forceApplyFrame += 1
                vpWorld.applyPenaltyForce(selectedBodyId, localPos, extraForce)
                applyedExtraForce[0] = extraForce[0]
            
            if forceApplyFrame*wcfg.timeStep > 0.1:
                viewer.ResetForce()
                forceApplyFrame = 0            
            '''
            vpWorld.step()                    
            
        # rendering
        rd_footCenter[0] = footCenter
        
        rd_CM[0] = CM.copy()
        
        rd_CM_plane[0] = CM_plane.copy()
        
        rd_footCenter_ref[0] = footCenter_ref
        rd_CM_plane_ref[0] = CM_ref.copy()
        rd_CM_ref[0] = CM_ref.copy()
        rd_CM_ref_vec[0] = (CM_ref - footCenter_ref)*3.
        rd_CM_vec[0] = (CM - footCenter)*3

        #rd_CM_plane[0][1] = 0.
        
        if CP!=None and dCP!=None:
            rd_CP[0] = CP
            rd_CP_des[0] = CP_des
        
        rd_dL_des_plane[0] = dL_des_plane
        rd_dH_des[0] = dH_des
        
        rd_grf_des[0] = totalNormalForce - totalMass*mm.s2v(wcfg.gravity)#dL_des_plane - totalMass*mm.s2v(wcfg.gravity)
                
        rd_exf_des[0] = applyedExtraForce[0]
        rd_root_des[0] = rootPos[0]

        rd_CMP[0] = softConstPoint

        rd_soft_const_vec[0] = controlModel.getBodyPositionGlobal(constBody)-softConstPoint
        
        if (forceApplyFrame == 0) :
            applyedExtraForce[0] = [0, 0, 0]
Exemplo n.º 38
0
a = np.floor(10 * np.random.random((2, 2)))
b = np.floor(10 * np.random.random((2, 2)))
print(a)
print(b)
print(np.vstack((a, b)))  #按行拼接  4*2
print(np.hstack((a, b)))  #按列拼接  2*4

a = np.floor(10 * np.random.random((2, 12)))
print(a)
print('===')
print(np.hsplit(a, 3))  #列分3份
print('---')
print(np.hsplit(a, (3, 4)))  #在第3列和第四列分别切开
a = np.floor(10 * np.random.random((12, 2)))
print('----')
print(a)
print(np.vsplit(a, 3))  #行分3份

data = np.sin(np.arange(20)).reshape(5, 4)
print(data)
ind = data.argmax(axis=0)  #0是按列定义维度,argmax找最大的标签
print(ind)
data_max = data[ind, range(data.shape[1])]
print(data_max)

a = np.array([[4, 3, 5], [1, 2, 1]])
print(a)
print('------')
b = np.sort(a, axis=1)  #按行排序
print(b)
Exemplo n.º 39
0
'''
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
'''

a = np.arange(1, 13).reshape(6, 2)
print(a)
'''
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]]
'''
b = np.split(a, 2)
b = np.vsplit(a, 2)
print(b)
'''
[array([[1, 2],
       [3, 4],
       [5, 6]]), 
array([[ 7,  8],
       [ 9, 10],
       [11, 12]])]
'''
b = np.split(a, 2, axis=1)
b = np.hsplit(a, 2)
print(b)
'''
[array([[ 1],
       [ 3],
Exemplo n.º 40
0
    freq = [0.0]*10
    #print (bins)
    for idx in range(len(freq_list)):
        bins_ = bins_list[idx]
        freq_ = freq_list[idx]
        for i in range(len(bins_)):
            if ( bins_[i] <= bins [i+1] and  bins_[i] >= bins [i]):
                freq[i] += freq_[i]
    repr_bins = [] # 10 bins for plotting, take average of each period
    for i in range(len(bins) - 1):
        repr_bins.append((bins[i] + bins[i+1])/2.0)

    return repr_bins, freq


epochs_d_1_1 = np.array(np.vsplit(d_1_1, 25))
epochs_d_1_2 = np.array(np.vsplit(d_1_2, 25))
epochs_d_2_1 = np.array(np.vsplit(d_2_1, 25))
epochs_d_2_2 = np.array(np.vsplit(d_2_2, 25))
epochs_g_1_1 = np.array(np.vsplit(g_1_1, 25))
epochs_g_1_2 = np.array(np.vsplit(g_1_2, 25))
bins_d_1 = []
freq_d_1 =  []
bins_d_2 = []
freq_d_2 = []
bins_g_1 = []
freq_g_1 = []
for i in range(25):
    bins,freq = merg_hist(epochs_d_1_1[i], epochs_d_1_2[i] )
    bins_d_1.append(bins)
    freq_d_1.append(freq)
Exemplo n.º 41
0
def _block_split(mat_in, n_v, n_h):
    """
    Returns a 4D array of matrices, splitting mat_in into
    n_v * n_h square sub-arrays.
    """
    return list(map(lambda x: hsplit(x, n_h), vsplit(mat_in, n_v)))
Exemplo n.º 42
0
# This is implemented by the functions np.split, np.hsplit, and np.vsplit

# In[51]:

x = [1, 2, 3, 99, 99, 3, 2, 1]
x1, x2, x3 = np.split(x, [3, 5])
print(x1, x2, x3)

# ### Notice that N split points lead to N + 1 subarrays.
# The related functions np.hsplit and np.vsplit are similar:
# Similarly, np.dsplit will split arrays along the third axis.

# In[52]:

grid = np.arange(16).reshape((4, 4))
upper, lower = np.vsplit(
    grid, [2])  # Splits the array into two. (hsplit splits horizontally)
print(upper)
print(lower)

# ## Absolute value
# Just as NumPy understands Python’s built-in arithmetic operators, it also understands Python’s built-in absolute value function:

# In[59]:

x = np.array([-2, -1, 0, 1, 2])
abs(x)

# In[60]:

# This ufunc can also handle complex data, in which the absolute value returns the magnitude:
x = np.array([3 - 4j, 4 - 3j, 2 + 0j, 0 + 1j])
Exemplo n.º 43
0
print( ar2*3
print( ar1*ar2   #普通乘法
print( np.dot(ar1,ar2)  #矩阵乘法
print( ar2.T    #转置
print( np.linalg.inv(ar2) #矩阵的逆
print( ar2.sum() #矩阵元素求和
print( ar2.max()    #矩阵最大的元素
ar3=np.array([[1,2],[3,4],[5,6]])
print( ar3.cumsum(1)     #按行累计总和
print( "**************"
print( ar2
ar4=np.array([1,8,9,0,5])
ar5=np.array([[1,8,9,0,5],[2,7,0,6,4],[3,0,6,5,9]])
print( ar4
print( np.nonzero(ar4) #返回数组非零元素的位置
print( np.nonzero(ar5) #第二个数组返回非零元素的位置
print( "**************NumPy通用函数******************"
print( np.exp(ar1)
print( np.sin(ar1)   #弧度制
print( np.sqrt(ar1)  #开方函数
print( np.add(ar1,ar2)
print( "*************NumPy 矩阵的合并和分割***************"
ar7=np.vstack((ar1,ar2)) #纵向合并矩阵
print( ar7
ar8=np.hstack((ar1,ar2))
print( ar8       #横向合并矩阵
print( "纵向分割"
print( np.vsplit(ar7,2)
print( "横向分割"
print( np.hsplit(ar8,2)
Exemplo n.º 44
0
    拓展资料:
        官方文档:
            cv::ml::StatModel Class Reference:https://docs.opencv.org/4.5.3/db/d7d/classcv_1_1ml_1_1StatModel.html
            cv::ml::DTrees Class Reference:https://docs.opencv.org/4.5.3/d8/d89/classcv_1_1ml_1_1DTrees.html
'''

import cv2 as cv
import numpy as np

# 图像一行有100个数字图像,每个数有500个,并且按数字顺序排列,即前五行为0,后面依次1-9。
img = cv.imread("../data/datasets/digits/digits.png")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

# 先将图像切分50行,再将每一行分别切成100列 # (50 x 100 x 20 x 20)
data = np.array([np.hsplit(row, 100) for row in np.vsplit(gray, 50)])

# 切分训练集以及测试集 8:2
train_data = data[:, :80].reshape((-1, 400)).astype(np.float32)  # (4000, 400)
test_data = data[:, 80:].reshape((-1, 400)).astype(np.float32)  # (1000, 400)
train_labels = np.vstack([np.full((400, 1), i)
                          for i in range(10)])  # (4000, 1)
test_labels = np.vstack([np.full((100, 1), i) for i in range(10)])  # (1000, 1)

# 创建随机森林(Random Trees)并保存训练结果
rt = cv.ml.RTrees_create()
rt.train(train_data, cv.ml.ROW_SAMPLE, train_labels)
# rt.save("../data/models/digit_rtrees.xml")
# 对测试集数据进行预测,计算正确率
# rt = cv.ml.RTrees_load("../data/models/digit_rtrees.xml")
ret, result = rt.predict(test_data)
    return img

def hog(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    return hist

img = cv2.imread('digits.png',0)

cells = [np.hsplit(row,100) for row in np.vsplit(img,50)]

# First half is trainData, remaining is testData
train_cells = [ i[:50] for i in cells ]
test_cells = [ i[50:] for i in cells]

######     Now training      ########################

deskewed = [map(deskew,row) for row in train_cells]
hogdata = [map(hog,row) for row in deskewed]
trainData = np.float32(hogdata).reshape(-1,64)
responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])

svm = cv2.SVM()
svm.train(trainData,responses, params=svm_params)
svm.save('svm_data.dat')
Exemplo n.º 46
0
    def get_batch(self, split, batch_size=None, seq_per_img=None):
        batch_size = batch_size or self.batch_size
        seq_per_img = seq_per_img or self.seq_per_img

        fc_batch = [] # np.ndarray((batch_size * seq_per_img, self.opt.fc_feat_size), dtype = 'float32')
        att_batch = [] # np.ndarray((batch_size * seq_per_img, 14, 14, self.opt.att_feat_size), dtype = 'float32')
        label_batch = np.zeros([batch_size * seq_per_img, self.seq_length + 2], dtype = 'int')
        mask_batch = np.zeros([batch_size * seq_per_img, self.seq_length + 2], dtype = 'float32')

        wrapped = False

        infos = []
        gts = []

        for i in range(batch_size):
            # fetch image
            tmp_fc, tmp_att,\
                ix, tmp_wrapped = self._prefetch_process[split].get()
            fc_batch.append(tmp_fc)
            att_batch.append(tmp_att)
            
            label_batch[i * seq_per_img : (i + 1) * seq_per_img, 1 : self.seq_length + 1] = self.get_captions(ix, seq_per_img)

            if tmp_wrapped:
                wrapped = True

            # Used for reward evaluation
            gts.append(self.h5_label_file['labels'][self.label_start_ix[ix] - 1: self.label_end_ix[ix]])
        
            # record associated info as well
            info_dict = {}
            info_dict['ix'] = ix
            info_dict['id'] = self.info['images'][ix]['id']
            info_dict['file_path'] = self.info['images'][ix]['file_path']
            infos.append(info_dict)

        # #sort by att_feat length
        # fc_batch, att_batch, label_batch, gts, infos = \
        #     zip(*sorted(zip(fc_batch, att_batch, np.vsplit(label_batch, batch_size), gts, infos), key=lambda x: len(x[1]), reverse=True))
        fc_batch, att_batch, label_batch, gts, infos = \
            zip(*sorted(zip(fc_batch, att_batch, np.vsplit(label_batch, batch_size), gts, infos), key=lambda x: 0, reverse=True))
        data = {}
        data['fc_feats'] = np.stack(sum([[_]*seq_per_img for _ in fc_batch], []))
        # merge att_feats
        max_att_len = max([_.shape[0] for _ in att_batch])
        data['att_feats'] = np.zeros([len(att_batch)*seq_per_img, max_att_len, att_batch[0].shape[1]], dtype = 'float32')
        for i in range(len(att_batch)):
            data['att_feats'][i*seq_per_img:(i+1)*seq_per_img, :att_batch[i].shape[0]] = att_batch[i]
        data['att_masks'] = np.zeros(data['att_feats'].shape[:2], dtype='float32')
        for i in range(len(att_batch)):
            data['att_masks'][i*seq_per_img:(i+1)*seq_per_img, :att_batch[i].shape[0]] = 1
        # set att_masks to None if attention features have same length
        if data['att_masks'].sum() == data['att_masks'].size:
            data['att_masks'] = None

        data['labels'] = np.vstack(label_batch)
        # generate mask
        nonzeros = np.array(list(map(lambda x: (x != 0).sum()+2, data['labels'])))
        for ix, row in enumerate(mask_batch):
            row[:nonzeros[ix]] = 1
        data['masks'] = mask_batch

        data['gts'] = gts # all ground truth captions of each images
        data['bounds'] = {'it_pos_now': self.iterators[split], 'it_max': len(self.split_ix[split]), 'wrapped': wrapped}
        data['infos'] = infos

        return data
Exemplo n.º 47
0
print(a.sum())
print(a.min())
print(np.mean(a))
print("********")
print(e, f, g, h)

#view and original object share same data, but create another way of viewing the data
x = a[:]
print(x)
x.resize(3, 2)
print(x)
print(a)
np.concatenate((a, b), axis=0)
np.transpose(a)
print(np.hsplit(a, 3))  # split at horizontal dimention into 3 parts
print(np.vsplit(a, 2))

np.savez('my_array.npz', a=a, b=b, c=c, d=d)
np.save('my_array1', a)
y = np.load("my_array.npz")
print(y['b'])
print(np.load('my_array1.npy'))

#scipy
print("------------")
print(a, b)
print(np.inner(
    a, b))  # [[sum(a1i*b1i), sum(a2i*b1i)],[sum(a2i*b1i), sum(a2i*b2i)]]
print(np.outer(a, b))
print("------------")
Exemplo n.º 48
0
        label = os.path.split(path)[1]
        all_labels.append(label)
        for file in files:
            # if 'Accelerometer-2012-06-06-09-04-41-walk-m5.txt' in file or \
            #         'Accelerometer-2011-05-30-20-57-19-walk-f1.txt' in file:
            #     continue
            print(os.path.join(path, file))
            file_data = pd.read_csv(os.path.join(path, file),
                                    names=None,
                                    na_values=['?'],
                                    delimiter=r"\s+").astype(float)

            no_of_split = int(file_data.iloc[:, 0].shape[0] / split)
            remainder = file_data.iloc[:, 0].shape[0] % split
            clean_split_data = file_data.iloc[:file_data.shape[0] - remainder]
            split_all_data = np.vsplit(clean_split_data, no_of_split)
            # split_all_data.append(file_data.iloc[-32:])
            class_data = None
            for i in range(0, len(split_all_data)):
                data = pd.concat([
                    split_all_data[i].iloc[:, 0].T,
                    split_all_data[i].iloc[:, 1].T, split_all_data[i].iloc[:,
                                                                           2].T
                ],
                                 axis=0)
                if class_data is None:
                    class_data = data.to_frame().T
                else:
                    class_data.loc[i] = data.values
            class_file_data[(label, file)] = class_data
Exemplo n.º 49
0
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Load the data, converters convert the letter to a number
data = np.loadtxt(
    'C:\\Users\\Harshit\\Desktop\\Machine Learning\\Image Analysis\\letter-recognition.data',
    dtype='float32',
    delimiter=',',
    converters={0: lambda ch: ord(ch) - ord('A')})

# split the data to two, 10000 each for train and test
train, test = np.vsplit(data, 2)

# split trainData and testData to features and responses
responses, trainData = np.hsplit(train, [1])
labels, testData = np.hsplit(test, [1])

# Initiate the kNN, classify, measure accuracy.

knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, responses)
ret, result, neighbours, dist = knn.findNearest(test, k=5)
'''
knn = cv2.KNearest()
knn.train(trainData, responses)
ret, result, neighbours, dist = knn.find_nearest(testData, k=5)
'''

correct = np.count_nonzero(result == labels)
accuracy = correct * 100.0 / 10000
Exemplo n.º 50
0
 def array_split():
     a = np.floor(10 * np.random.random((2, 12)))
     print(np.hsplit(a, 3))  # 沿水平轴进行分割
     print(np.vsplit(a, 2))  # 沿垂直轴进行分割
"""
    多维数组的组合与拆分
"""

import numpy as np

ary01 = np.arange(1, 7).reshape(2, 3)
ary02 = np.arange(10, 16).reshape(2, 3)

# 垂直方向完成组合操作,生成新数组
ary03 = np.vstack((ary01, ary02))
print(ary03, ary03.shape)
# 垂直方向完成拆分操作,生成两个数组
a, b, c, d = np.vsplit(ary03, 4)
print(a, b, c, d)

# 水平方向完成组合操作,生成新数组
ary04 = np.hstack((ary01, ary02))
print(ary04, ary04.shape)
# 水平方向完成拆分操作,生成两个数组
e, f = np.hsplit(ary04, 2)
print(e, '\n', f)

a = np.arange(1, 7).reshape(2, 3)
b = np.arange(7, 13).reshape(2, 3)
# 深度方向(3维)完成组合操作,生成新数组
i = np.dstack((a, b))
print(i.shape)
# 深度方向(3维)完成拆分操作,生成两个数组
k, l = np.dsplit(i, 2)
print("==========")
Exemplo n.º 52
0
HIDDEN_UNIT_SIZE3 = 45 # 隠れ層3のユニット数 45
TRAIN_DATA_SIZE = 70000 # 訓練データ 70000 + テストデータ 8000 = 78000 行_csvファイル
BATCH_SIZE = 100 # バッチサイズ 100
​
# 処理時間計測
import time
start = time.time()
​
# 入力データ形成
raw_input = np.loadtxt(open("input.csv"), delimiter=",")
​
# 列(縦)分割 (...299 300 || ここで分割 || 301 302...)
[log, label, wolf1, wolf2, wolf3]  = np.hsplit(raw_input, [300,315,330,345])
​
# 行(横)分割 (上から [TRAIN_DATA_SIZE] までを訓練データ、それ以下をテストデータとする)
[log_train, log_test] = np.vsplit(log, [TRAIN_DATA_SIZE])
[label_train, label_test] = np.vsplit(label, [TRAIN_DATA_SIZE])
[wolf1_train, wolf1_test] = np.vsplit(wolf1, [TRAIN_DATA_SIZE])
[wolf2_train, wolf2_test] = np.vsplit(wolf2, [TRAIN_DATA_SIZE])
[wolf3_train, wolf3_test] = np.vsplit(wolf3, [TRAIN_DATA_SIZE])
​
def inference(log_placeholder, dropout_rates):
# 隠れ層 1
  with tf.name_scope('hidden1') as scope:
    hidden1_weight = tf.Variable(tf.truncated_normal([LOG_SIZE, HIDDEN_UNIT_SIZE1], stddev=0.1), name="hidden1_weight")
    hidden1_bias = tf.Variable(tf.constant(0.1, shape=[HIDDEN_UNIT_SIZE1]), name="hidden1_bias")
    hidden1_output = tf.nn.relu(tf.matmul(log_placeholder, hidden1_weight) + hidden1_bias)
# 隠れ層 2
  with tf.name_scope('hidden2') as scope:
    hidden2_weight = tf.Variable(tf.truncated_normal([HIDDEN_UNIT_SIZE1, HIDDEN_UNIT_SIZE2], stddev=0.1), name="hidden2_weight")
    hidden2_bias = tf.Variable(tf.constant(0.1, shape=[HIDDEN_UNIT_SIZE2]), name="hidden2_bias")
Exemplo n.º 53
0
c = np.hstack((a, b))
print(c)
print()

# you can separate them horizontally as well
a = np.arange(9).reshape(3, 3)
print(a)
c = np.hsplit(a, 3)
for row in c:
    print(row)
print()

# you can separate them vertically as well
a = np.arange(9).reshape(3, 3)
print(a)
c = np.vsplit(a, 3)
for row in c:
    print(row)
print()

# boolean array
a = np.arange(12).reshape(4, 3)
print(a)
b = a < 6
print(b)
print(
    a[b]
)  # it will get index of where True is found in b, and it will return a for that matching index
a[b] = -1  # True element index are assigned -1 in a
print(a)
np.vstack([x,grid])
y = np.array([[7],
              [8]])
np.hstack([grid,y])

### Splitting arrays ###

x = [1,2,3, 22, 23, 7, 8, 9]
x1, x2, x3 = np.split(x, [3, 5]) #split x with split points at indices 3 & 5 or ('4').
print(x1, x2, x3) #N splits => N+1 subarrays

#splits using np.hsplit and np.vsplit
grid = np.arange(16).reshape((4,4))
print(grid)
grid.shape
upper, lower = np.vsplit(grid,[2])
print(upper)
print(lower)
left, right = np.hsplit(grid,[2])
print(left)
print(right)

## Computation on NumPy Arrays: Universal Functions ##

# Key to making NumPy computations fast? Use VECTORIZED OPERATIONS. 
# (generally through Python's universal functions aka "ufuncs")

#ex)
np.arange(5)/np.arange(1,6)
x = np.arange(9).reshape((3,3))
x
Exemplo n.º 55
0
import numpy as np
import cv2
img = cv2.imread('dataset/digits.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Now we split the image to 5000 cells, each 20x20 size
cells = [np.hsplit(row, 100) for row in np.vsplit(gray, 50)]
# Make it into a Numpy array. It size will be (50,100,20,20)
x = np.array(cells)
# Now we prepare train_data and test_data.
train = x[:, :50].reshape(-1, 400).astype(np.float32)  # Size = (2500,400)
test = x[:, 50:100].reshape(-1, 400).astype(np.float32)  # Size = (2500,400)
# Create labels for train and test data
k = np.arange(10)
train_labels = np.repeat(k, 250)[:, np.newaxis]
test_labels = train_labels.copy()
# Initiate kNN, train the data, then test it with test data for k=1
knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
ret, result, neighbors, dist = knn.findNearest(test, k=5)
# Now we check the accuracy of classification
# For that, compare the result with test_labels and check which are wrong
matches = result == test_labels
correct = np.count_nonzero(matches)
accuracy = correct * 100.0 / result.size
print(accuracy)

#학습 데이터를 저장
np.savez('knn_data.npz', train=train, train_labels=train_labels)

#학습 데이터 로드
with np.load('knn_data.npz') as data:
Exemplo n.º 56
0

# In[18]:


np.hsplit(y,3)


# ### 3.vsplit

# vsplit splits along the vertical axis

# In[15]:


p_1,p_2 = np.vsplit(y, 2)


# In[16]:


print(p_1)


# In[17]:


print(p_2)


# ### Array Unpacking
    def get_batch(self, split, batch_size=None, seq_per_img=None):
        batch_size = batch_size or self.batch_size
        seq_per_img = seq_per_img or self.seq_per_img

        fc_batch = [
        ]  # np.ndarray((batch_size * seq_per_img, self.opt.fc_feat_size), dtype = 'float32')
        att_batch = [
        ]  # np.ndarray((batch_size * seq_per_img, 14, 14, self.opt.att_feat_size), dtype = 'float32')
        isg_rela_batch = []
        isg_obj_batch = []
        isg_attr_batch = []

        ssg_rela_batch = []
        ssg_obj_batch = []
        ssg_attr_batch = []

        label_batch = np.zeros([batch_size * seq_per_img, self.seq_length + 2],
                               dtype='int')
        mask_batch = np.zeros([batch_size * seq_per_img, self.seq_length + 2],
                              dtype='float32')

        wrapped = False

        infos = []
        gts = []

        for i in range(batch_size):
            # fetch image
            tmp_fc, tmp_att, tmp_isg, tmp_ssg, ix, tmp_wrapped = self._prefetch_process[
                split].get()
            # print('batch'+str(i))
            # print ('fc:')
            # print(tmp_fc.shape)
            # print(tmp_fc)
            # print ('att:')
            # print(tmp_att)
            # print ('isg:')
            # print (tmp_isg)
            # print ('ssg:')
            # print(tmp_ssg)
            # print ('wrapped:')
            # print (tmp_wrapped)
            fc_batch.append(tmp_fc)
            att_batch.append(tmp_att)

            isg_rela_batch.append(tmp_isg['ssg_rela_matrix'])
            isg_attr_batch.append(tmp_isg['ssg_attr'])
            isg_obj_batch.append(tmp_isg['ssg_obj'])

            ssg_rela_batch.append(tmp_ssg['ssg_rela_matrix'])
            ssg_attr_batch.append(tmp_ssg['ssg_attr'])
            ssg_obj_batch.append(tmp_ssg['ssg_obj'])

            # print('ssg_o')
            # print(tmp_ssg['ssg_obj'])
            # print('ssg_a')
            # print(tmp_ssg['ssg_attr'])
            # print('ssg_r')
            # print(tmp_ssg['ssg_rela_matrix'])

            label_batch[i * seq_per_img:(i + 1) * seq_per_img,
                        1:self.seq_length + 1] = self.get_captions(
                            ix, seq_per_img)

            if tmp_wrapped:
                wrapped = True

            # Used for reward evaluation
            gts.append(self.h5_label_file['labels'][self.label_start_ix[ix] -
                                                    1:self.label_end_ix[ix]])

            # record associated info as well
            info_dict = {}
            info_dict['ix'] = ix
            info_dict['id'] = self.info['images'][ix]['id']
            # info_dict['file_path'] = ''
            info_dict['file_path'] = self.info['images'][ix]['file_path']
            infos.append(info_dict)

        fc_batch, att_batch, label_batch, gts, infos = zip(
            *sorted(zip(fc_batch, att_batch, np.vsplit(
                label_batch, batch_size), gts, infos),
                    key=lambda x: 0,
                    reverse=True))
        data = {}
        data['fc_feats'] = np.stack(
            reduce(lambda x, y: x + y, [[_] * seq_per_img for _ in fc_batch]))
        max_att_len = max([_.shape[0] for _ in att_batch])

        # merge att_feats
        data['att_feats'] = np.zeros(
            [len(att_batch) * seq_per_img, max_att_len, att_batch[0].shape[1]],
            dtype='float32')
        for i in range(len(att_batch)):
            data['att_feats'][
                i * seq_per_img:(i + 1) *
                seq_per_img, :att_batch[i].shape[0]] = att_batch[i]
        data['att_masks'] = np.zeros(data['att_feats'].shape[:2],
                                     dtype='float32')
        for i in range(len(att_batch)):
            data['att_masks'][i * seq_per_img:(i + 1) *
                              seq_per_img, :att_batch[i].shape[0]] = 1
        # set att_masks to None if attention features have same length
        # if data['att_masks'].sum() == data['att_masks'].size:
        #     data['att_masks'] = None

        if self.use_isg:
            max_rela_len = max([_.shape[0] for _ in isg_rela_batch])
            data['isg_rela_matrix'] = np.ones(
                [len(att_batch) * seq_per_img, max_rela_len, 3]) * -1
            for i in range(len(isg_rela_batch)):
                data['isg_rela_matrix'][i * seq_per_img:(i + 1) * seq_per_img, 0:len(isg_rela_batch[i]), :] = \
                    isg_rela_batch[i]
            data['isg_rela_masks'] = np.zeros(
                data['isg_rela_matrix'].shape[:2], dtype='float32')
            for i in range(len(isg_rela_batch)):
                data['isg_rela_masks'][
                    i * seq_per_img:(i + 1) *
                    seq_per_img, :isg_rela_batch[i].shape[0]] = 1

            max_obj_len = max([_.shape[0] for _ in isg_obj_batch])
            data['isg_obj'] = np.ones(
                [len(att_batch) * seq_per_img, max_obj_len]) * -1
            for i in range(len(isg_obj_batch)):
                data['isg_obj'][i * seq_per_img:(i + 1) * seq_per_img,
                                0:len(isg_obj_batch[i])] = isg_obj_batch[i]
            data['isg_obj_masks'] = np.zeros(data['isg_obj'].shape,
                                             dtype='float32')
            for i in range(len(isg_obj_batch)):
                data['isg_obj_masks'][
                    i * seq_per_img:(i + 1) *
                    seq_per_img, :isg_obj_batch[i].shape[0]] = 1

            max_attr_len = max([_.shape[1] for _ in isg_attr_batch])
            data['isg_attr'] = np.ones(
                [len(att_batch) * seq_per_img, max_obj_len, max_attr_len]) * -1
            for i in range(len(isg_obj_batch)):
                data['isg_attr'][i * seq_per_img:(i + 1) * seq_per_img, 0:len(isg_obj_batch[i]),
                0:isg_attr_batch[i].shape[1]] = \
                    isg_attr_batch[i]
            data['isg_attr_masks'] = np.zeros(data['isg_attr'].shape,
                                              dtype='float32')
            for i in range(len(isg_attr_batch)):
                for j in range(len(isg_attr_batch[i])):
                    N_attr_temp = np.sum(isg_attr_batch[i][j, :] >= 0)
                    data['isg_attr_masks'][i * seq_per_img:(i + 1) *
                                           seq_per_img, j,
                                           0:int(N_attr_temp)] = 1

        if self.use_ssg:
            max_rela_len = max([_.shape[0] for _ in ssg_rela_batch])
            data['ssg_rela_matrix'] = np.ones(
                [len(att_batch) * seq_per_img, max_rela_len, 3]) * -1
            for i in range(len(ssg_rela_batch)):
                data['ssg_rela_matrix'][
                    i * seq_per_img:(i + 1) * seq_per_img,
                    0:len(ssg_rela_batch[i]), :] = ssg_rela_batch[i]
            data['ssg_rela_masks'] = np.zeros(
                data['ssg_rela_matrix'].shape[:2], dtype='float32')
            for i in range(len(ssg_rela_batch)):
                data['ssg_rela_masks'][
                    i * seq_per_img:(i + 1) *
                    seq_per_img, :ssg_rela_batch[i].shape[0]] = 1

            max_obj_len = max([_.shape[0] for _ in ssg_obj_batch])
            data['ssg_obj'] = np.ones(
                [len(att_batch) * seq_per_img, max_obj_len]) * -1
            for i in range(len(ssg_obj_batch)):
                data['ssg_obj'][i * seq_per_img:(i + 1) * seq_per_img,
                                0:len(ssg_obj_batch[i])] = ssg_obj_batch[i]
            data['ssg_obj_masks'] = np.zeros(data['ssg_obj'].shape,
                                             dtype='float32')
            for i in range(len(ssg_obj_batch)):
                data['ssg_obj_masks'][
                    i * seq_per_img:(i + 1) *
                    seq_per_img, :ssg_obj_batch[i].shape[0]] = 1

            max_attr_len = max([_.shape[1] for _ in ssg_attr_batch])
            data['ssg_attr'] = np.ones(
                [len(att_batch) * seq_per_img, max_obj_len, max_attr_len]) * -1
            for i in range(len(ssg_obj_batch)):
                data['ssg_attr'][
                    i * seq_per_img:(i + 1) * seq_per_img,
                    0:len(ssg_obj_batch[i]),
                    0:ssg_attr_batch[i].shape[1]] = ssg_attr_batch[i]
            data['ssg_attr_masks'] = np.zeros(data['ssg_attr'].shape,
                                              dtype='float32')
            for i in range(len(ssg_attr_batch)):
                for j in range(len(ssg_attr_batch[i])):
                    N_attr_temp = np.sum(ssg_attr_batch[i][j, :] >= 0)
                    data['ssg_attr_masks'][i * seq_per_img:(i + 1) *
                                           seq_per_img, j,
                                           0:int(N_attr_temp)] = 1

        data['labels'] = np.vstack(label_batch)
        # generate mask
        nonzeros = np.array(
            list(map(lambda x: (x != 0).sum() + 2, data['labels'])))
        for ix, row in enumerate(mask_batch):
            row[:nonzeros[ix]] = 1
        data['masks'] = mask_batch

        data['gts'] = gts  # all ground truth captions of each images
        data['bounds'] = {
            'it_pos_now': self.iterators[split],
            'it_max': len(self.split_ix[split]),
            'wrapped': wrapped
        }
        data['infos'] = infos

        # print('ssg_o_m')
        # print(data['ssg_obj_masks'])
        # print('ssg_a_m')
        # print(data['ssg_attr_masks'])
        # print('ssg_r_m')
        # print(data['ssg_rela_masks'])
        # sents = utils.decode_sequence(self.get_vocab(), data['labels'] )
        # print(sents)

        return data
Exemplo n.º 58
0
               256]
 tmpj = tmpj[:(tmpj.shape[0] / 256) * 256, :(tmpj.shape[1] / 256) * 256,
             1]  #Retain the green channel
 del (img)
 tmpm = cv2.medianBlur(tmp, 5)  #Median filtering with 5x5 kernel
 tmpg = cv2.GaussianBlur(
     tmp, (5, 5),
     0)  #Gaussian blur with default sigma=1.1 and kernel size 5x5
 awgn = 2.0 * np.random.randn(
     tmp.shape[0],
     tmp.shape[1])  #Additive While Gaussian Noise with sigma=2
 tmpw = (tmp + awgn)
 tmpw = np.clip(tmpw, 0,
                255)  #Keep image pixel values within [0,255] range
 tmpw = tmpw.astype(np.uint8)
 vblocks = np.vsplit(tmp, tmp.shape[0] /
                     256)  #split image patch into vertical blocks
 shuffle(vblocks)
 vblocks = vblocks[:len(vblocks)]
 imcount = 0
 for v in vblocks:
     hblocks = np.hsplit(
         v, v.shape[1] /
         256)  #split each vertical block into horizantal blocks
     shuffle(hblocks)
     hblocks = hblocks[:len(hblocks)]
     for h in hblocks:
         X[count - 1] = h.reshape((1, 1, 256, 256))
         y[count - 1] = 0
         imcount += 1
         if count == 50000:
             break
Exemplo n.º 59
0
    def makePhiT(self, div):
        if (self.dDim == irisD
            ):  #this is the bcd delete the first row as its identifier
            phi = self.dataPd[['0', '1', '2', '3']].as_matrix()
            t1 = self.dataPd[['4']].as_matrix()
            t = np.ones([phi.shape[0], 1])
            for i in range(len(t)):
                if t1[i] == 'Iris-setosa':
                    t[i] = int(0)
                elif t1[i] == 'Iris-versicolor':
                    t[i] = int(1)
                else:
                    t[i] = int(2)
                #print(t[i])
            #print(phi)
            #print("shape",t.shape,"phi shape",phi.shape)
            #exit()
        else:
            phi = self.dataPd[['0', '1']].as_matrix()
            t = self.dataPd[['2']].as_matrix()

        dataLen = t.shape[0]

        self.blockLen = t.shape[0] / self.tDiv
        delLen = t.shape[0] % self.tDiv
        #print(delLen)

        #print(t)
        #exit()

        #exit()

        if (self.dDim == irisD):  #delete 3 datapoints
            phiD = phi
            tD = t
        else:
            phiD = phi
            tD = t

        phiArr = np.vsplit(phiD, 5)
        tArr = np.vsplit(tD, 5)
        #print(tArr[0])

        if div == 0:
            self.Phi = phiArr[1]
            self.T = tArr[1]
            self.PhiTest = phiArr[0]
            self.TTest = tArr[0]
            self.PhiArrS = phiArr[0]
        else:
            self.Phi = phiArr[0]
            self.T = tArr[0]
            self.PhiTest = phiArr[1]
            self.TTest = tArr[1]
            self.PhiArrS = phiArr[1]

        for i in range(2, 5):
            if div == i:
                self.PhiTest = phiArr[i]
                self.TTest = tArr[i]
                self.PhiArrS = phiArr[i]
            else:
                self.Phi = np.concatenate((self.Phi, phiArr[i]))
                self.T = np.concatenate((self.T, tArr[i]))

        #print(self.T.shape,self.Phi.shape,self.PhiTest.shape,self.TTest.shape)

        ones = np.ones(self.Phi.shape[0])
        onesTest = np.ones(self.PhiTest.shape[0])
        a_2d = ones.reshape((self.Phi.shape[0], 1))
        a_2dTest = onesTest.reshape((self.PhiTest.shape[0], 1))
Exemplo n.º 60
0
import numpy as np

# array分割

# 一、均匀分割
a = np.arange(12).reshape((3, 4))
# 1、行分割
[m, n] = np.split(a, 2, axis=1)
# 2、列分割
[m, n, o] = np.split(a, 3, axis=0)

# 二、大概分割
a = np.arange(15).reshape((3, 5))
# 1、行分割
[m, n, o] = np.array_split(a, 3, axis=1)
# 2、列分割
[m, n] = np.array_split(a, 2, axis=0)

# 三、简单分割
a = np.arange(12).reshape((3, 4))
# 1、行分割
[m, n] = np.hsplit(a, 2)
# 2、列分割
[m, n, o] = np.vsplit(a, 3)