removing none from an awkward array - awkward-array

I have an awkward array (1) which I obtained post-processing.
An array look like:
>>> ak.Array([96., 99., 67., 13., 3., None, 1., 1.,None])
I want to remove the None elements from this array. I could remove them using loop, but I want to avoid it to save some computing time. Or writing a function and compiling using Numba is only option?
Thanks.

I just realised that is_none exist and works like charm,
a[~ak.is_none(a)]

Related

How does Ruby's Combined Comparison Operator work?

First question on stackoverflow :)
I'm going through the Ruby course on Codecademy and I'm stuck on something.
fruits = ["orange", "apple", "banana", "pear", "grapes"]
fruits.sort! {|first, second| second <=> first}
print fruits
I don't know how to phrase this question. On Codecademy the assignment was to set up the array to be displayed in reverse on the console. After some research, I was able to figure it out. I understand how it works and the order to put it in the code not why. I'm aware that "<=>" compares two objects, but how do the items within the array become objects when we don't declare them as such?
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
First question: At various points in its operation the sort method has to compare pairs of objects to see what their relative ordering should be. It does the comparison by applying the block you pass to sort, i.e., {|first, second| second <=> first}. Not sure what you mean by "how do the items within the array become objects when we don't declare them as such?". All data in ruby is an object, so there's no declaration or conversion needed given that all variables are object references.
Second question: Yes, you could do fruits.sort.reverse, but that would require additional work after the sort to do the reverse operation. Also, reverse can't handle more complex sorting tasks, such as sorting people by multiple criteria such as gender & last name, or hair color, height, and weight. Writing your own comparator can handle quite complex orderings.
String literals can be used to create string objects in Ruby, there is no need to use the String class to create the object. The following two are equivalent:
"Hello, world!"
String.new("Hello, world!")
More information can be found here.
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
Please contact Codecademy about this, but I suspect it's for learning more about how <=> works.

Display data in 2d list in python

For an assignment in a my python class. I have to take the following list data:
animal = [
('cat', 'meow', 4),
('dog', 'bark', 10),
('bird', 'chirp', 0.5),
('snake', 'hiss', 3),
('cow', 'moo', 250),
('lion', 'roar', 500)
]
And create nested loop statements to display the following:
[1]: https://i.stack.imgur.com/KpPL4.png
I think it won't help you learn if we give you the complete answer, but here are some things that can help:
Do you know that each of those structures (the array, and the tuples in the array) is an iterable? That means you can loop over them without using indexes, the way one might in another language. Check out that link to get some ideas about how you could nest an iterable for your individual animals inside the iterable for the whole list.
If you're using Python 3.6 or newer, you can use f strings for your formatting, which will give you a tidy way to print the values with flexible spacing, so they all line up the way you'd like. There are good examples of the older format for formatting strings with whitespace padding in this SO question: Python spacing and aligning strings
I think you should be able to figure out your answer based on those two pointers.

Tensorflow : tf.gather doesn't work on big arrays

I'm having an issue with my numpy array, which is of size (29000,200,1024)(7Go). Its the features of the images of my dataset.
Once loaded, my function receives the indexes to build the current batch as a tensor.
Unfortunately, using :
tf.gather(array, indices)
freezes. Though printing for example array[0] work instantly.
I tried to transform my numpy array with convert_to_tensor so I can use directly array_tensor(indice) but again, convert_to_tensor leads to a memory limit error.
Any work around ?
Thank you very much
Passing numpy array directly into tf op construction API converts it to tf.constant op which contains data in the op definition, so you are inlining the whole thing into GraphDef, subject to 2GB GraphDef limit.
To avoid this, create var=tf.Variable(my_placeholder) and initialize this variable by running var.initializer, feed_dict={my_placeholder: np_array}. This puts numpy array data directly into variable store.

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

Why the array could not hold the keys of hash in the following perl script?

hash_test.pl
#a=("f","a","b");
$K{"f"}{"aa"}=1;
$K{"a"}{"aa"}=1;
$k{"b"}{"bb"}=1;
foreach(#a){
#c= sort keys %{$k{$_}};
}
print "#c\n";
foreach(#c) {...}
perl hash_test.pl
bb
I want to keep the keys of the hash into an array, so that I can use the array as an input for the following statements.
But it seemed that the assay #c just only hold the last element.
Could anyone tell me why or help me to improve the script?
You assign the array every time in the foreach, thus overwriting it every time. So you end up with only having the last thing assigned to it. If you move the print inside the foreach you'll see that they are all there.
To store those keys you need to add them to the array, not assign the array. I've corrected the typo $k to $K, and changed aa that goes with f to ff (expecting it to be a typo as well).
my #c;
foreach my $el (#a) {
push #c, sort keys %{$K{$el}};
}
print "#c\n";
This prints the line: ff aa bb. Every time through the loop all keys found in the hash for a particular array element are added to #c, each as a separate element. So #c will contain all bottom-level keys across the whole data structure.
However, there is more that I would like to suggest.
Always use strict; and use warnings; This is not pedantry but it directly helps. I never write code without them. The typo would be caught here, for example.
Use descriptive variable names. Specifically, single-letter variable names are just too easy to confuse, unless in very short loops or where it is crystal clear what they are. (For example, a typo like this couldn't really happen.) Most importantly, the code is going to be far nicer to work with. That generally results in better code.
Please use good indentation and spacing. It helps a lot, in many ways.
A useful core package for nested data structures is Data::Dumper, which can print the whole thing nicely formatted so we can see it. Try to add to the end of your code
use Data::Dumper;
print Dumper(\%K);
There are yet others that do the same or similar.
Here is another way to do what you ask.
my #lowest_keys = map { sort keys %{$K{$_}} } #a;
I call them lowest_keys to emphasize that these are the ones from the last hash in your data structure, the bottom of it. The map applies processing in the block { ... } to each element of #a in turn, returning a list with all these results. (If any one result itself is a list, with more elements than one, it gets merged into the overall output list. So this may create the output list with many more elements than the input.) This list can then be assigned to an array, as above, or passed on to another function that expects a list as input, or interated over.
The map is generally used to transform an array into another, by doing to each element what is in { ... } block. Its close cousin grep is used to filter, so passing through only the elements of the input list for which the condition in { ... } evaluates to true, forming the output list. For example, filter out undefined array elements: my #good = grep { defined } #all
Variable names are case sensitive, so %K... and %k... are not the same.
Always use
use strict;
use warnings;
and declare your variables with my. That prevents you from making this kind of mistakes.

Resources