flatten

flatten a data frame

被刻印的时光 ゝ 提交于 2019-12-04 17:40:37
问题 I have this nested data frame test <- structure(list(id = c(13, 27), seq = structure(list( `1` = c("1997", "1997", "1997", "2007"), `2` = c("2007", "2007", "2007", "2007", "2007", "2007", "2007")), .Names = c("1", "2"))), .Names = c("penr", "seq"), row.names = c("1", "2"), class = "data.frame") I want a list of all values in the second column, namely result <- c("1997", "1997", "1997", "2007", "2007", "2007", "2007", "2007", "2007", "2007", "2007") Is there an easy way to achieve this? 回答1:

Flatten multidimensional associative array to one one-dimensional array of references in PHP

落爺英雄遲暮 提交于 2019-12-04 17:19:23
Given I have an array: $array = array( 'a' => array( 'b' => array( 'c' => 'hello', ), ), 'd' => array( 'e' => array( 'f' => 'world', ), ), ); I want to "flatten" it to a single dimension look-up of references, concatenating keys with a delimiter ( in the case of this example, a forward slash / ) Performing a var_dump() on a successful output would yield: ( note all the references ) array(6) { ["a"]=> &array(1) { ["b"]=> &array(1) { ["c"]=> &string(5) "hello" } } ["a/b"]=> &array(1) { ["c"]=> &string(5) "hello" } ["a/b/c"]=> &string(5) "hello" ["d"]=> &array(1) { ["e"]=> &array(1) { ["f"]=>

Recursively Flatten values of nested maps in Java 8

对着背影说爱祢 提交于 2019-12-04 10:29:38
问题 Given a Map<String, Object> , where the values are either a String or another Map<String, Object> , how would one, using Java 8, flatten the maps to a single list of values? Example: Map - "key1" -> "value1" - "key2" -> "value2" - "key3" -> Map - "key3.1" -> "value3.1" - "key3.2" -> "value3.2" - "key3.3" -> Map - "key3.3.1" -> "value3.3.1" - "key3.3.2" -> "value3.3.2" For the above example, I would like the following list: value1 value2 value3.1 value3.2 value3.3.1 value3.3.2 I know it can be

Flatten a dataset in TensorFlow

为君一笑 提交于 2019-12-04 04:10:10
问题 I am trying to convert a dataset in TensorFlow to have several single-valued tensors. The dataset currently looks like this: [12 43 64 34 45 2 13 54] [34 65 34 67 87 12 23 43] [23 53 23 1 5] ... After the transformation it should look like this: [12] [43] [64] [34] [45] [2] [13] [54] [34] [65] [34] [67] [87] [12] ... My initial idea was using flat_map on the data set and then converting each tensor to a list of tensors using reshape and unstack : output_labels = self.dataset.flat_map(convert

Ruby inner flatten (array of arrays)

北慕城南 提交于 2019-12-04 04:08:04
问题 I have an array like the following [ [[0, :a], [2, :b]], [3, :c], [4, :d], [[5, :e], [6, :f], [7, :g]] ] That is, an Array of elements that are either (1) 2-element Arrays, or (2) an Array of 2-element Arrays. I am trying to find an elegant way to "flatten" this array such that elements that are (2) get expanded out into root-level elements. In this example: [[0, :a], [2, :b], [3, :c], [4, :d], [5, :e], [6, :f], [7, :g]] This is almost like using Array#flatten(depth) , except depth needs to

Haskell FlatMap

荒凉一梦 提交于 2019-12-04 01:10:29
I am a beginner interested in Haskell, and I have been trying to implement the flatmap (>>=) on my own to better understand it. Currently I have flatmap :: (t -> a) -> [t] -> [a] flatmap _ [] = [] flatmap f (x:xs) = f x : flatmap f xs which implements the "map" part but not the "flat". Most of the modifications I make result in the disheartening and fairly informationless Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `flatmap' error. What am I missing? An error like this happens when the type signature you specify does not match the actual type of

Flatten a DataFrame in Scala with different DataTypes inside

纵然是瞬间 提交于 2019-12-03 21:06:13
As you may know, a DataFrame can contain fields which are complex types, like structures (StructType) or arrays (ArrayType). You may need, as in my case, to map all the DataFrame data to a Hive table, with simple type fields (String, Integer...). I've been struggling with this issue for a long time, and I've finally found a solution I want to share. Also, I'm sure it could be improved, so feel free to reply with your own suggestions. It's based on this thread , but also works for ArrayType elements, not only StructType ones. It is a tail recursive function which receives a DataFrame, and

How does data shape change during Conv2D and Dense in Keras?

六眼飞鱼酱① 提交于 2019-12-03 13:40:39
问题 Just as the title says. This code only works Using: x = Flatten()(x) Between the convolutional layer and the dense layer. import numpy as np import keras from keras.models import Sequential, Model from keras.layers import Dense, Dropout, Flatten, Input from keras.layers import Conv2D, MaxPooling2D from keras.optimizers import SGD # Generate dummy data x_train = np.random.random((100, 100, 100, 3)) y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

Why is itertools.chain faster than a flattening list comprehension?

﹥>﹥吖頭↗ 提交于 2019-12-03 11:11:00
In the context of a discussion in the comments of this question it was mentioned that while concatenating a sequence of strings simply takes ''.join([str1, str2, ...]) , concatenating a sequence of lists would be something like list(itertools.chain(lst1, lst2, ...)) , although you can also use a list comprehension like [x for y in [lst1, lst2, ...] for x in y] . What surprised me is that the first method is consistently faster than the second: import random import itertools random.seed(100) lsts = [[1] * random.randint(100, 1000) for i in range(1000)] %timeit [x for y in lsts for x in y] # 39

Flattening an Iterable<Iterable<T>> in Guava

喜欢而已 提交于 2019-12-03 10:22:01
Is there a flatten method in Guava - or an easy way to convert an Iterable<Iterable<T>> to an Iterable<T> ? I have a Multimap<K, V> [sourceMultimap] and I want to return all values where the key matches some predicate [keyPredicate]. So at the moment I have: Iterable<Collection<V>> vals = Maps.filterKeys(sourceMultimap.asMap(), keyPredicate).values(); Collection<V> retColl = ...; for (Collection<V> vs : vals) retColl.addAll(vs); return retColl; I've looked through the Guava docs, but nothing jumped out. I am just checking I've not missed anything. Otherwise, I'll extract my three lines into a