I've got two tensor objects repenting vectors:
(0110010),
(0111011)
I would like to compare between the two and create a new tensor vector:
(0110010)
Iterating over them in a loop is very slow, I know there is a solution for this in Matlab so I assume there is one for tensors as well.
To do a logical and operation for tensors containing only 1 and 0 elements you could use the :cmul() member function (element-wise multiplication).
th> torch.Tensor({0,1,1,0,0,1,0}):cmul(torch.Tensor({0,1,1,1,0,1,1}))
0
1
1
0
0
1
0
To compare two tensors element-wise you can use :eq():
th> torch.Tensor({0,1,1,0,0,1,0}):eq(torch.Tensor({0,1,1,1,0,1,1}))
1
1
1
0
1
1
0
Related
I have a data frame with 10 columns and 4 row and a vector of the length 10.
My vector contains 7x 0 and 3x 1. I want to make a sample of my data frame that picks exactly the 20 variables where in my vector a 1 appears.
E.g. if my vector goes like that: 0 0 1 0 1 1 0 0 0 0 then I want the 3rd, 5th and 6th row to be picked by the sample.
How can I do that?
Can I add anything to this command to make it work? s <- sample_n(df$col1, 3)
I have been continuing to learn r to transition away from excel and I am wondering what the best way to approach the following problem is, or at least what tools are available to me:
I have a large data set (100K+ rows) and several columns that I could generate a signal off of and each value in the vectors can range between 0 and 3.
sig1 sig2 sig3 sig4
1 1 1 1
1 1 1 1
1 0 1 1
1 0 1 1
0 0 1 1
0 1 2 2
0 1 2 2
0 1 1 2
0 1 1 2
I want to generate composite signals using the state of each cell in the four columns then see what each of the composite signals tell me about the returns in a time series. For this question the scope is only generating the combinations.
So for example, one composite signal would be when all four cells in the vectors = 0. I could generate a new column that reads TRUE when that case is true and false in each other case, then go on to figure out how that effects the returns from the rest of the data frame.
The thing is I want to check all combinations of the four columns, so 0000, 0001, 0002, 0003 and on and on, which is quite a few. With the extent of my knowledge of r, I only know how to do that by using mutate() for each combination and explicitly entering the condition to check. I assume there is a better way to do this, but I haven't found it yet.
Thanks for the help!
I think that you could paste the columns together to get unique combinations, then just turn this to dummy variables:
library(dplyr)
library(dummies)
# Create sample data
data <- data.frame(sig1 = c(1,1,1,1,0,0,0),
sig2 = c(1,1,0,0,0,1,1),
sig3 = c(2,2,0,1,1,2,1))
# Paste together
data <- data %>% mutate(sig_tot = paste0(sig1,sig2,sig3))
# Generate dummmies
data <- cbind(data, dummy(data$sig_tot, sep = "_"))
# Turn to logical if needed
data <- data %>% mutate_at(vars(contains("data_")), as.logical)
data
i am stuck with a boolean expression help me solve what x.y'+x'.y =?
i have exam today and i don't know how do solve this type. And in addition can someone recreate the boolean laws that involve two element instead of one for me? Thank you
There are only two inputs to the expression, so write out a truth table with the values of the inputs and for each term until you get the result.
x y x' y' x'.y x.y' x'.y+x.y'
0 0 1 1 0 ...
0 1 1 0 1 ...
1 1 0 0 0 ...
1 0 0 1 0 ...
When you have done that, look for patterns in the last column. You should then recognise the pattern as being the same as a single operator.
The pattern for the inputs is usually a Gray code so that the output column reflects changes due to only one input changing, which usually can help show up the pattern.
Alternatively, when you have your result, plot it in a grid and spot the pattern that way, e.g. for x+y you'd get
x\y 0 1
0 0 1
0 1 1
I have performed non-metric multidimensional scaling (NMDS) on two data frames, each containing different variables but for the same sites. I am using the vegan package:
> head (ResponsesS3)
R1_S3 R10_S3 R11_S3 R12_S3 R2_S3 R3_S3 R4_S3 R6_S3 R7_S3 R8_S3 R9_S3
4 0 0 0 0 0 1 0 0 0 0 0
5 0 0 0 0 0 1 0 0 0 0 0
7 1 0 0 1 0 0 0 0 0 0 0
12 0 0 0 0 0 1 0 0 0 0 0
14 2 2 0 0 0 0 2 0 0 0 0
16 0 0 1 0 0 0 0 0 0 1 0
> head (EnvtS3)
Dep_Mark Dep_Work Dep_Ext Use_For Use_Fish Use_Ag Div_Prod
4 0.06222836 1.0852315 0.8367309 1.1415929 1.644670 0.1006964 0.566474
5 0.25946808 1.3342266 0.0000000 1.7123894 0.822335 0.0000000 0.283237
7 2.20668862 0.0000000 0.8769881 0.4280973 0.822335 0.5244603 0.849711
12 2.26323697 0.0000000 0.8090991 1.1415929 0.000000 1.4957609 1.416185
14 1.65107675 0.5195901 0.2921132 0.5707965 0.822335 1.7873609 0.849711
16 1.82230225 0.4760163 0.1915366 2.2831858 0.000000 1.6614904 0.849711
> ResponsesS3.mds = metaMDS (ResponsesS3, k =2, trymax = 100)
> EnvtS3.mds = metaMDS (EnvtS3, k =2, trymax = 100)
I fit the results using a procrustean superimposition
> pro.ResponsesS3.EnvtS3.mds <- procrustes(ResponsesS3.mds,EnvtS3.mds)
I am most interested in understanding how the variables from each dataset fit together. I would like to use the plot() function to return a graph of the variables from ResponsesS3 and from EnvtS3, rather than the sites (which is what the plot function returns by default).
Is this possible?
No, this is not possible. The problem you'll find you have is that there will be different numbers of variables in the two datasets which causes the procrustes() method to fail if you try procrustes(..., scores = "species").
Even if you fit with procrustes(..., score = "sites") (the default), who do you propose to draw the plot if we could extract the species information? The current plot joins rows from one matric with the rows of other; this works in the default setting because the datasets are assumed to be measurements on the same locations/sites. But this is not possible with species/variables. More fundamentally, how should we pair up species with environmental variables?
Finally, you are trying to look at how the variables compare yet have used a method that essentially throws this information away once dissimilarities are computed.
I would look at the method of coinertia analysis, of which there is a crude interface in my cocorresp package and a fuller one in the ade4 package. If you find yourself wanting to compare two sets of species data, try cocorrespondence analysis, which cocorresp fits.
Like Gav said, the points must match each other one to one for Procrustes rotation. However, once you have a Procrustes rotation, you can naturally apply it to other matrices with the same number of columns. The number of columns is crucial: If you have 2-dim NMDS, your variables also must be mapped into these 2 dim. Function metaMDS() will get you such column scores corresponding to your ordination of row scores, but I don't know how adequate these are in your case. The easiest way to rotate those scores in vegan is to use predict method with newdata. Continuing with your example:
predict(pro.ResponsesS3.EnvtS3.mds, newdata=scores(EnvtS3.mds, "species"))
This will only rotate your column scores ("species") similarly as is rotated your row scores.
We do not know what you try to achieve, and indeed there may be better ways to achieve your goal (check Gavin's answer for a starter). However, this will do the rotation.
I have a dataframe,df, with many columns cola,colb etc each consisting of a sequence of integers 0 or 1
df$cola
[1] 1 0 0 1 0 1 0 0 etc.
I am using the subSeq function in the doBy package to obtain some sequences
and want to apply this to all columns
I have tried putting the columns into a vector
cols <- colnames(df) # "cola" "colb" etc.
and have tried without success this approach
subSeq(get(paste0("df$",cols[1]))) # error object 'df$cola' not found
Could not easily find an equivalent on site via search
I think you are looking for df[[cols[1]]].
Note that df[["foo"]] is the same as df$foo.