reshape

R Reshape data by combining common value of two variables

◇◆丶佛笑我妖孽 提交于 2019-12-11 01:47:31
问题 I want to reshape a data frame by combining two variables. For example: Here is a new data: dat = data.frame( var1 = c("a", "a", "a", "Emily", "b", "Bob", "c"), var2 = c("Jhon", "Emily", "Julie", "Angela", "Bob", "Paul", "Paul"), stringsAsFactors = F ) Excepted output: # var1 var2 var3 var4 var5 # 1 a Jhon Emily Julie Angela # 2 b Bob Paul c <NA> 回答1: Using base R you can do: relation=function(dat){ .relation=function(x){ k = unique(sort(c(dat[dat[, 1] %in% x, 2], x, dat[dat[, 2] %in% x, 1]))

Reshaping a 3 dimensional array to 2 dimensions

谁说胖子不能爱 提交于 2019-12-11 00:47:43
问题 I have a 3-dimensional matrix of dimensions: 427x470x48 I want to reshape this into a 2-dimensional matrix of dimensions: 48x200690 This would mean that old(1, 1, :) would correspond to new(:, 1) Additionally, old(1,2,:) would correspond to new(:,2) and so on and so forth. Thank You 回答1: Do: new = reshape(permute(old, [3 2 1]), 48, []); Also you can roughly check that they are equal by: numel(intersect(old(1,1,:),new(:,1))) == 48; 来源: https://stackoverflow.com/questions/18004989/reshaping-a-3

caffe reshape / upsample fully connected layer

懵懂的女人 提交于 2019-12-11 00:16:12
问题 Assuming we have a layer like this: layer { name: "fully-connected" type: "InnerProduct" bottom: "bottom" top: "top" inner_product_param { num_output: 1 } } The output is batch_size x 1. In several papers (for exmaple link1 page 3 picture on the top, or link2 page 4 on top)I have seen that they used such a layer in the end to come up with a 2D image for pixel-wise prediction. How is it possible to transform this into a 2D image? I was thinking of reshape or deconvolution, but I cannot figure

add rows in a data.table but not when certain columns take same values

早过忘川 提交于 2019-12-10 23:58:03
问题 I have a data.table dat with 4 columns, say ( col1 , col2 , col3 , col4 ). Input data: structure(list(col1 = c(5.1, 5.1, 4.7, 4.6, 5, 5.1, 5.1, 4.7, 4.6, 5), col2 = c(3.5, 3.5, 3.2, 3.1, 3.6, 3.5, 3.5, 3.2, 3.1, 3.6), col3 = c(1.4, 1.4, 1.3, 1.5, 1.4, 3.4, 3.4, 1.3, 1.5, 1.4 ), col4 = structure(c(1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L), .Label = c("setosa", "versicolor", "virginica", "eer"), class = "factor")), .Names = c("col1", "col2", "col3", "col4"), row.names = c(NA, -10L), class = c(

Correct use of a fold or reduce function to long-to-wide data in python or javascript?

为君一笑 提交于 2019-12-10 22:31:45
问题 Trying to learn to think like a functional programmer a little more---I'd like to transform a data set with what I think is either a fold or a reduce operation. In R, I would think of this as a reshape operation, but I'm not sure how to translate that thinking. My data is a json string that looks like this: s = '[ {"query":"Q1", "detail" : "cool", "rank":1,"url":"awesome1"}, {"query":"Q1", "detail" : "cool", "rank":2,"url":"awesome2"}, {"query":"Q1", "detail" : "cool", "rank":3,"url":

Fortran reshape - N-dimensional transpose

自闭症网瘾萝莉.ら 提交于 2019-12-10 20:34:39
问题 I'm trying to write some code in Fortran which requires the re-ordering of an n-dimensional array. I thought the reshape intrinsic combined with the order argument should allow this, however I'm running into difficulties. Consider the following minimal example program test implicit none real, dimension(:,:,:,:,:), allocatable :: matA, matB integer, parameter :: n1=3, n2=5, n3=7, n4=11, n5=13 integer :: i1, i2, i3, i4, i5 allocate(matA(n1,n2,n3,n4,n5)) !Source array allocate(matB(n3,n2,n4,n1

Combining multiple columns into one

◇◆丶佛笑我妖孽 提交于 2019-12-10 20:12:10
问题 I have a dataset as follows: ` GN Datum Land AG..GKV. MTCR..GKV. NSGI..GKV. NSGII..GKV. WA..GKV. ML..GKV. Wert 8 11693 2012-01-05 Kenia 5A001f 159700 9 11710 2012-01-05 Indien 1C350.43 18752 10 11749 2012-01-05 Taiwan, Provinz von China 2B350g2 24000 11 11750 2012-01-05 Indien 2B201a1 2B001b2 370296 12 11751 2012-01-05 Serbien 2B201a 7200 13 11752 2012-01-05 Indien 4c 6037 14 11753 2012-01-05 Rumänien 2B001d 7830 15 11755 2012-01-06 China 2B201a1 215987 16 11756 2012-01-06 China 2B201a1

reshape error - invalid 'row.names' length

℡╲_俬逩灬. 提交于 2019-12-10 18:57:18
问题 I have the following database (in wide form), "st_all", where I have got two variables I wish to reshape ("P" and "PLC"). The id for the subjects is "g_id". g_id study condition sample PLC1 PLC2 PLC3 PLC4 PLC5 PLC6 PLC7 PLC8 PLC9 PLC10 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 1 1 1 1 1 20 20 20 50 50 20 30 20 50 50 1 2 2 1 2 2 1 1 1 1 2 2 1 1 1 60 70 50 70 60 60 60 70 60 50 1 2 1 1 2 2 1 1 1 1 3 3 1 1 1 80 50 55 58 70 50 80 80 60 65 1 2 2 1 2 2 1 1 1 1 4 4 1 1 1 89 51 59 62 72 60 86 80 61 54 1 1 2 1 2

Converting 3D matrix to cascaded 2D Matrices

烈酒焚心 提交于 2019-12-10 18:52:26
问题 I have a 3D matrix in python as the following: import numpy as np a = np.ones((2,2,3)) a[0,0,0] = 2 a[0,0,1] = 3 a[0,0,2] = 4 I want to convert this 3D matrix to a set of 2D matrices. I have tried np.reshape but it did not solve my problem. The final shape I am interested in is the following cascaded vesrsion: [[ 2. 1. 3. 1. 4. 1.] [ 1. 1. 1. 1. 1. 1.]] However, np.reshape gives me the following [[ 2. 3. 4. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.]] How can I solve this? 回答1: Use transpose alongwith

【tensorflow】使用convolution处理图片

筅森魡賤 提交于 2019-12-10 18:46:35
import numpy as np import matplotlib . pyplot as plt % matplotlib inline import tensorflow as tf # plt.figure(figsize=(12,9)) image = plt . imread ( './caise.jpg' ) plt . imshow ( image ) image . shape (388, 690, 3) data = image . reshape ( 1 , 388 , 690 , 3 ) . astype ( np . float32 ) # and a filter / kernel tensor of shape # `[filter_height, filter_width, in_channels, out_channels] filter_ = np . array ( [ [ 1 / 27 ] * 81 ] ) . reshape ( 3 , 3 , 3 , 3 ) conv = tf . nn . conv2d ( data , filter_ , [ 1 , 1 , 1 , 1 ] , 'SAME' ) with tf . Session ( ) as sess : image = sess . run ( conv ) .