I have a data pair:
[{:mobile=>21, :web=>43},{:mobile=>23, :web=>543},{:mobile=>23, :web=>430},{:mobile=>34, :web=>13},{:mobile=>26, :web=>893}]
How can I make this into:
[{mobile: [21, 23, 34, 26]}, {web: [43, 543, 430, 13, 893]}
When I look at the provide scenario I see the following solution:
data = [{:mobile=>21, :web=>43},{:mobile=>23, :web=>543},{:mobile=>23, :web=>430},{:mobile=>34, :web=>13},{:mobile=>26, :web=>893}]
keys = [:mobile, :web]
result = keys.zip(data.map { |hash| hash.values_at(*keys) }.transpose).to_h
#=> {:mobile=>[21, 23, 23, 34, 26], :web=>[43, 543, 430, 13, 893]}
This first extracts the values of the keys from each hash, then transposes the the resulting array. This changes [[21, 43], [23, 543], [23, 430], ...] into [[21, 23, 23, ...], [43, 543, 430, ...]]. This result can be zipped back to the keys and converted into a hash.
To get rid of duplicates you could add .each(&:uniq!) after the transpose call, or map the collections to a set .map(&:to_set) (you need to require 'set') if you don't mind the values being sets instead of arrays.
result = keys.zip(data.map { |hash| hash.values_at(*keys) }.transpose.each(&:uniq!)).to_h
#=> {:mobile=>[21, 23, 34, 26], :web=>[43, 543, 430, 13, 893]}
require 'set'
result = keys.zip(data.map { |hash| hash.values_at(*keys) }.transpose.map(&:to_set)).to_h
#=> {:mobile=>#<Set: {21, 23, 34, 26}>, :web=>#<Set: {43, 543, 430, 13, 893}>}
References:
Array#map
Hash#values_at
Splat operator * (in hash.values_at(*keys))
Array#zip
Array#transpose
Array#to_h
Array#each
Array#uniq!
Enumerable#to_set
Related
I'm trying to decode this Bech32 address into a hex.
When given cosmos1qpjrq625nglf3xx9chdkq953nhrd3nygte44rt. It breaks it down into it's head which is 'cosmos' and the remainder is represented as a List of 8-bit unsigned integers (Uint8List).
When this is encoded to hexadecimal (HEX.Encode), i get a value of 00011203001a0a1413081f091106060518170d16000514111317030d11130408.
However, it is meant to be getting me 00643069549a3e9898c5c5db6016919dc6d8cc88 instead.
You can check this if you go to https://slowli.github.io/bech32-buffer/ -> and decode cosmos1qpjrq625nglf3xx9chdkq953nhrd3nygte44rt which gives 00643069549a3e9898c5c5db6016919dc6d8cc88.
I can't figure out the issue, is it perhaps
-The formatting is wrong, different bases? or am i doing this completely wrong.
Thanks and i appreciate any replies
Here is a snippet of code;
import 'package:bech32/bech32.dart';
import 'package:hex/hex.dart';
Bech32Codec bech32codec = Bech32Codec();
// target address : 00643069549a3e9898c5c5db6016919dc6d8cc88 -> to get to this address
String address = 'cosmos1qpjrq625nglf3xx9chdkq953nhrd3nygte44rt';
Bech32 bech32 = bech32codec.decode(address);
print(bech32.data);
// this returns [0, 1, 18, 3, 0, 26, 10, 20, 19, 8, 31, 9, 17, 6, 6, 5, 24, 23, 13, 22, 0, 5, 20, 17, 19, 23, 3, 13, 17, 19, 4, 8]
print(bech32.hrp);
print(bech32codec.encode(Bech32("cosmos", bech32.data)));
var answer2 = HEX.encode(bech32.data);
print(answer2);
var decode = HEX.decode('00643069549a3e9898c5c5db6016919dc6d8cc88');
print(decode);
// this returns [0, 100, 48, 105, 84, 154, 62, 152, 152, 197, 197, 219, 96, 22, 145, 157, 198, 216, 204, 136]
I have a specific order in which I want to show a number of my blog posts, but I'd also like to display regular blog posts after those selected ones.
So let's say I have posts [1, 7, 35, 36, 48] which I want to go first:
#selected_posts = Post.find([1, 7, 35, 36, 48])
But now I need to query for every other post, excluding those above:
#other_posts = Post.where.not(id: [1, 7, 35, 36, 48])
And now I need to combine those to maintain that order:
Post.find([1, 7, 35, 36, 48]) + Post.where.not(id: [1, 7, 35, 36, 48])
I'm using Postgres. Is it possible to do this in one query?
You can do order by case...
priority_ids = [1, 7, 35, 36, 48]
#all_posts = Post.all.order("CASE WHEN id IN (#{priority_ids.join(',')}) THEN 1 ELSE 2 END")
(thanks to #DeepakMahakale for correction)
I'm trying to train an SVC classifier for image data. Yet, when I run this code:
classifier = svm.SVC(gamma=0.001)
classifier.fit(train_set, train_set_labels)
I get this error:
ValueError: setting an array element with a sequence.
I produced the images into an array with Matplotlib: plt.imread(image).
The error seems like it's not in an array, yet when I check the types of the data and the labels they're both lists (I manually add to a list for the labels data):
print(type(train_set))
print(type(train_set_labels))
<class 'list'>
<class 'list'>
If I do a plt.imshow(items[0]) then the image shows correctly in the output.
I also called train_test_split from scikit-learn:
train_set, test_set = train_test_split(items, test_size=0.2, random_state=42)
Example input:
train_set[0]
array([[[212, 134, 34],
[221, 140, 48],
[240, 154, 71],
...,
[245, 182, 51],
[235, 175, 43],
[242, 182, 50]],
[[230, 152, 51],
[222, 139, 47],
[236, 147, 65],
...,
[246, 184, 49],
[238, 179, 43],
[245, 186, 50]],
[[229, 150, 47],
[205, 122, 28],
[220, 129, 46],
...,
[232, 171, 28],
[237, 179, 35],
[244, 188, 43]],
...,
[[115, 112, 103],
[112, 109, 102],
[ 80, 77, 72],
...,
[ 34, 25, 28],
[ 55, 46, 49],
[ 80, 71, 74]],
[[ 59, 56, 47],
[ 66, 63, 56],
[ 48, 45, 40],
...,
[ 32, 23, 26],
[ 56, 47, 50],
[ 82, 73, 76]],
[[ 29, 26, 17],
[ 41, 38, 31],
[ 32, 29, 24],
...,
[ 56, 47, 50],
[ 59, 50, 53],
[ 84, 75, 78]]], dtype=uint8)
Example label:
train_set_labels[0]
'Picasso'
I'm not sure what step I'm missing to get the data in the form that the classifier needs in order to train it. Can anyone see what may be needed?
The error message you are receiving:
ValueError: setting an array element with a sequence,
normally results when you are trying to put a list somewhere that a single value is required. This would suggest to me that your train_set is made up of a list of multidimensional elements, although you do state that your inputs are lists. Would you be able to post an example of your inputs and labels?
UPDATE
Yes, it's as I thought. The first element of your training data, train_set[0], corresponds to a long list (I can't tell how long), each element of which consists of a list of 3 elements. You are therefore calling the classifier on a list of lists of lists, when the classifier requires a list of lists (m rows corresponding to the number of training examples with each row made up of a list of n features). What else is in your train_set array? Is the full data set in train_set[0]? If so, you would need to create a new array with each element corresponding to each of the subelements of train_set[0], and then I believe your code should run, although I am not too familiar with that classifier. Alternatively you could try running the classifier with train_set[0].
UPDATE 2
I don't have experience with scikit-learn.svc so I wouldn't be able to tell you what the best way of preprocessing the data in order for it to be acceptable to the algorithm, but one method would be to do as I said previously and for each element of train_set, which is composed of lists of lists, would be to recurse through and place all the elements of sublist into the list above. For example
new_train_set = []
for i in range(len(train_set)):
for j in range(len(train_set[i]):
new_train_set.append([train_set[i,j])
I would then train with new_train_set and the training labels.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array, whose elements are arrays of different sizes, say:
[[45, 96, 0.0, 96, 96, 96, 0.0], [04, 55, 06, 55, 04, 04, 02, 55]]
I want to find the sum of the two arrays, i.e.,
[49, 151, ...]
You can use something like this:
a.flat_map{|x| x.in_groups_of(a.max_by(&:size).size, 0)}.transpose.map(&:sum)
Or this:
a.max_by(&:size).map.with_index{|_, i| a.sum{|x| x[i]||0}}
Not very pretty, but works:
>> a = [[45, 96, 0.0, 96, 96, 96, 0.0], [04, 55, 06, 55, 04, 04, 02, 55]]
=> [[45, 96, 0.0, 96, 96, 96, 0.0], [4, 55, 6, 55, 4, 4, 2, 55]]
>> sorted_a = a.sort_by(&:size).reverse
=> [[4, 55, 6, 55, 4, 4, 2, 55], [45, 96, 0.0, 96, 96, 96, 0.0]]
>> zipped_a = sorted_a.first.zip(sorted_a.last)
=> [[4, 45], [55, 96], [6, 0.0], [55, 96], [4, 96], [4, 96], [2, 0.0], [55, nil]]
>> zipped_a.map{ |arr| arr.map{ |v| v || 0 } }.map(&:sum)
=> [49, 151, 6.0, 151, 100, 100, 2.0, 55]
First you have to sort the array starting the longest for zip to work properly. Zipping will then create nil values in the redundant values of the shorter arrays. So the next step is to replace these nils to zeroes (using the nested map) and finally you can sum the values.
You can try this way also
k =[]
for i in 0..ar.max_by(&:size).length-1 do
k << ar.map { |x| [x[i]] }
end
k.map(&:flatten).map{|a| a.compact.sum}
=> [49, 151, 6.0, 151, 100, 100, 2.0, 55]
a = [[45, 96, 0, 96, 96, 96, 0],
[ 4, 55, 6, 55, 4, 4, 2, 55]]
Array.new(a.max_by(&:size).size) { |i| a.reduce(0) { |t,e| t+e[i].to_i } }
#=>[49, 151, 6, 151, 100, 100, 2, 55]
Note that nil.to_i #=> 0 (ref).
Another example:
a = [[1], [2,3,4], [5,6]]
Array.new(a.max_by(&:size).size) { |i| a.reduce(0) { |t,e| t+e[i].to_i } }
#=> [8,9,4]
I am learning rails from rails tutorial. I got a problem in the Section 11.3.1, the code in the Listing 11.44:
def self.from_users_followed_by(user)
followed_user_ids = user.followed_user_ids.join(', ')
where("user_id IN (?) OR user_id = ?", followed_user_ids, user)
end
will not return user_id in followed_user_ids.
After searching at stackoverflow, I found the solution here, which said that just remove the "join" method and it works for me.
But I am curious about why there is no error when I run RSPEC before remove the "join" method.
In Listing 11.41:
subject { Micropost.from_users_followed_by(user) }
it { should include(own_post) }
it { should include(followed_post) }
If the "join" method will make the SQL statement wrong, why RSPEC passed?
Does anyone have the same problem?
Update 2012.04.03
I used rails console to check the problem.
user = User.first
ids = ids = user.following_users.map(&:id).join(', ')
Which got
User Load (2.6ms) SELECT "users".* FROM "users" INNER JOIN "follows" ON
"follows"."followable_id" = "users"."id" AND "follows"."followable_type" = 'User' WHERE
"follows"."blocked" = 'f' AND "follows"."follower_id" = 1 AND "follows"."follower_type"
= 'User' AND "follows"."followable_type" = 'User'
=> "6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 55, 56, 5"
Then the SQL
Collection.where("user_id IN (?)", ids)
The result is
Collection Load (0.7ms) SELECT "collections".* FROM "collections" WHERE (user_id IN
('6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 55, 56, 5')) ORDER BY collections.created_at DESC
=> []
Which return an empty array.
But I got all my rspec passed. Still no idea.
Assuming the join creates a string '1,2,3', then perhaps you want to join("','") to have ('1','2','3'), but I don't think this will work - I'm sure PostgreSQL doesn't like IDs as strings.
Alternatively:
Squeel is a good SQL syntax gem where you could use:
user = User.first
ids = user.following_users.map(&:id)
Collection.where{(user_id.like_any ids)}
This will allow you to pass in the array of integers and Squeel will setup the query where:
SELECT "collections".* FROM "collections" WHERE (("collections"."user_id" LIKE 1 OR "collections"."user_id" LIKE 2 OR "collections"."user_id" LIKE 3))
EDIT: You can also use the syntax:
ids = User.first.following_users.pluck(:id)
Collection.find_by_user_id(ids)