在R
中,我有一个2xn
数据矩阵,其中包含所有整数。
第一列表示项目的大小。其中一些尺寸是由于合并造成的,因此第二列表示达到该尺寸的项目数(包括1)(称为“索引”)。索引的总和表明原始数据中实际有多少个项目。
我现在需要创建一个新数据集,该数据集将根据索引中的数字将所有合并后的大小切回,从而产生一个2xn
向量(根据总数,新长度为n
索引数),第二列全为1。
我需要这种分裂以两种方式发生。
6
的大小3
将为c(2,2,2)
。 重要,所有数字都必须为整数,因此应类似于c(1,2)或c(2,1)。不能是c(1.5,1.5)。1
分配给索引中除包含提醒之外的所有位置以外的所有位置。例如,大小为6
,索引为3
,现在将是c(1,1,4)
或1、1和4的任意组合。下面,我将提供一些示例数据,以举例说明我所拥有的,我想要的和尝试过的东西。
#Example data that I have
Y.have<-cbind(c(19,1,1,1,1,4,3,1,1,8),c(3,1,1,1,1,2,1,1,1,3))
数据显示第一行有3个项目的大小为19,第二列有一个项目的大小为19,依此类推。重要的是,这些数据中最初有15个项目(即sum(Y.have[,2])
),其中一些已合并,因此最终数据的长度必须为15。
我希望数据显示为:
####Homogenous separation - split values evenly as possible
#' The value of 19 in row 1 is now a vector of c(6,6,7) (or any combination thereof, i.e. c(6,7,6) is fine) since the position in the second column is a 3
#' Rows 2-5 are unchanged since they have a 1 in the second column
#' The value of 4 in row 6 is now a vecttor of c(2,2) since the position of the second column is a 2
#' Rows 7-9 are unchanged since they have a 1 in the second column
#' The value of 8 in row 10 is now a vector of c(3,3,2) (or any combination thereof) since the position in the second column is a 3
Y.want.hom<-cbind(c(c(6,6,7),1,1,1,1,c(2,2),3,1,1,c(3,3,2)),c(rep(1,times=sum(Y.have[,2]))))
####Heterogenous separation - split values with as many singles as possible,
#' The value of 19 in row 1 is now a vector of c(1,1,17) (or any combination thereof, i.e. c(1,17,1) is fine) since the position in the second column is a 3
#' Rows 2-5 are unchanged since they have a 1 in the second column
#' The value of 4 in row 6 is now a vecttor of c(1,3) since the position of the second column is a 2
#' Rows 7-9 are unchanged since they have a 1 in the second column
#' The value of 8 in row 10 is now a vector of c(1,1,6) (or any combination thereof) since the position in the second column is a 3
Y.want.het<-cbind(c(c(1,1,17),1,1,1,1,c(1,3),3,1,1,c(1,1,6)),c(rep(1,times=sum(Y.have[,2]))))
请注意,最终数据中整数的位置无关紧要,因为它们都具有一个索引大小写。
我尝试根据索引大小写拆分数据(split
)。这将创建一个列表,该列表的长度取决于唯一索引值的数量。然后,我遍历该列表中的各个位置,然后除以该位置。
a<-split(Y.have[,1],Y.have[,2]) #Split into a list according to the index
b<-list() #initiate new list
for (i in 1:length(a)){
b[[i]]<-a[[i]]/i #get homogenous values
b[[i]]<-rep(b[i],times=i) #repeat the values based on the number of indicies
}
Y.test<-cbind(unlist(b),rep(1,times=length(unlist(c)))) #create new dataset
这是一个可怕的方法。首先,它将产生小数。其次,列表中的位置不一定等于索引号(即,如果索引号为2,则第二个位置将是倒数第二个最低索引,但会除以2)。
但是,它至少允许我按索引分离出数据,对其进行处理,然后将其重组为适当的长度。现在,我需要在中间部分提供帮助-处理同构和异种重新分配的数据。我希望使用base r,但是任何方法都可以!预先谢谢你!
答案 0 :(得分:1)
# Toy data (dataset size 250)
# X1 = np.random.randint(0,100,size=(250,80))
# X2 = np.random.randint(0,100,size=(250,80))
# y = np.random.choice([0,1,2], size=(250,))
X = [dataset_processed['sentence_1_padded'], dataset_processed['sentence_2_padded']]
inputs1 = Input(shape=(80,))
inputs2 = Input(shape=(80,))
embedding_layer = Embedding(vocab_size, 128)
emb_out1 = embedding_layer(inputs1)
emb_out2 = embedding_layer(inputs2)
lstm_layer = LSTM(64)
x1 = lstm_layer(emb_out1)
x2 = lstm_layer(emb_out2)
dense = Dense(32, activation='relu')
x1 = dense(x1)
x2 = dense(x2)
x = Concatenate(axis=-1)([x1,x2])
predictions = Dense(3, activation='softmax')(x)
model = Model(inputs=[inputs1, inputs2], outputs=predictions)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['acc'])
model.fit(X, to_categorical(y), epochs=5, batch_size=32, validation_split=0.25)
是针对此类需求创建的,请检出。
将以下逻辑应用于您的代码,即可正常使用
例如:
library(partitions)
答案 1 :(得分:1)
这可能是一种方法。
为均质和异质拆分创建两个函数:
switch (selectedClass) {
case "Bank":
switch (selectedDiv) {
case "2nd Floor CEO, 2nd Floor Executive Lounge":
i = 1;
break;
case "1st Floor Open Area, 1st Floor Staff Room":
i = 2;
break;
case "1st Floor Tellers, 1st Floor Archives":
i = 3;
break;
case "Basement CCTV, Basement Lockers":
i = 4;
break;
}
break;
case "Border":
i = 5;
break;
case "Chalet":
i = 6;
break;
}
switch (i) {
case 1:
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), B1.class);
this.startActivity(myIntent);
}
private void startActivity(Intent myIntent) {
}
});
break;
然后使用get_hom_ints <- function(M, N) {
vec <- rep(floor(M/N), N)
for (i in seq_len(M - sum(vec))) {
vec[i] <- vec[i] + 1
}
vec
}
get_het_ints <- function(M, N) {
vec <- rep(1, N)
vec[1] <- M - sum(vec) + 1
vec
}
浏览矩阵的每一行:
apply
输出
(异构)
het_vec <- unlist(apply(Y.have, 1, function(x) get_het_ints(x[1], x[2])))
unname(cbind(het_vec, rep(1, length(het_vec))))
hom_vec <- unlist(apply(Y.have, 1, function(x) get_hom_ints(x[1], x[2])))
unname(cbind(hom_vec, rep(1, length(het_vec))))
(同质)
[,1] [,2]
[1,] 17 1
[2,] 1 1
[3,] 1 1
[4,] 1 1
[5,] 1 1
[6,] 1 1
[7,] 1 1
[8,] 3 1
[9,] 1 1
[10,] 3 1
[11,] 1 1
[12,] 1 1
[13,] 6 1
[14,] 1 1
[15,] 1 1
答案 2 :(得分:0)
一种选择是使用整数除法(%/%)和模数(%%)。它可能无法给出您指定的确切结果,即。 8和3给出的是(2,2,4)而不是(3,3,2),但通常会按照您的描述进行操作。
Y.have<-cbind(c(19,1,1,1,1,4,3,1,1,8),c(3,1,1,1,1,2,1,1,1,3))
homoVec <- c()
for (i in 1:length(Y.have[,1])){
if (Y.have[i,2] == 1) {
a = Y.have[i,1]
homoVec <- append(homoVec, a)
} else {
quantNum <- Y.have[i,1]
indexNum <- Y.have[i,2]
b <- quantNum %/% indexNum
c <- quantNum %% indexNum
a <- c(rep(b, indexNum-1), b + c)
homoVec <- append(homoVec, a)
}
}
homoOut <- data.frame(homoVec, 1)
heteroVec <- c()
for (i in 1:length(Y.have[,1])){
if (Y.have[i,2] == 1) {
a = 1
heteroVec <- append(heteroVec, a)
} else {
quantNum <- Y.have[i,1]
indexNum <- Y.have[i,2]
firstNum <- quantNum - (indexNum - 1)
a <- c(firstNum, rep(1, indexNum - 1))
heteroVec <- append(heteroVec, a)
}
}
heteroOut <- data.frame(heteroVec, 1)
如果完全按照示例中的描述进行数学运算非常重要,那么这应该可以工作。
homoVec <- c()
for (i in 1:length(Y.have[,1])){
if (Y.have[i,2] == 1) {
a = Y.have[i,1]
homoVec <- append(homoVec, a)
} else {
quantNum <- Y.have[i,1]
indexNum <- Y.have[i,2]
b <- round(quantNum/indexNum)
roundSum <- b * (indexNum - 1)
c <- quantNum - roundSum
a <- c(rep(b, indexNum-1), c)
homoVec <- append(homoVec, a)
}
}
homoOut <- data.frame(homoVec, 1)