将数组或参数传递给Rails中的hstore范围时出错(Errors when passing array or parameters to hstore scope in Rails)
我正在尝试在我的Rails应用程序中的hstore列上创建一个范围。 Product是一个模型,features是hstore类型的属性(使用Postgresql 9.2)。 我的范围类定义如下:
class Product < ActiveRecord::Base scope :with_features, lambda {|features| where("foo_id in (?)", features)
仅当您将单个值作为要素传递时,上述范围才有效。 数组抛出错误。 如下图所示:
Product.with_features('api') => [#<Product id: 1, name: "Sample">] # so great success # now with an array Product.with_features(['api','mobile']) => ActiveRecord::StatementInvalid: PG::Error: ERROR: argument of WHERE must be type boolean, not type record # so no good, this query will work as usual if features isn't of type hstore
在Rails 3.2中,似乎支持postgres hstore类型在涉及数组时是有限的(我正在使用https://github.com/softa/activerecord-postgres-hstore )。 我一直在尝试使用每个循环的一些解决方案将AND查询附加在一起,而不是运气。 有任何想法吗?
I'm trying to create a scope on an hstore column in my Rails app. Product is a model and features is an attribute of type hstore (using Postgresql 9.2). My class with scope is defined below:
class Product < ActiveRecord::Base scope :with_features, lambda {|features| where("foo_id in (?)", features)
The above scope only works if you pass single values in as features. Arrays throw errors. This is illustrated below:
Product.with_features('api') => [#<Product id: 1, name: "Sample">] # so great success # now with an array Product.with_features(['api','mobile']) => ActiveRecord::StatementInvalid: PG::Error: ERROR: argument of WHERE must be type boolean, not type record # so no good, this query will work as usual if features isn't of type hstore
In Rails 3.2, it seems support for postgres hstore type is limited when arrays are involved (I'm using https://github.com/softa/activerecord-postgres-hstore). I've been trying some solutions with a each loop to append AND queries together, not much luck. Any ideas?
原文:https://stackoverflow.com/questions/12657316
最满意答案
假设x和y是numpy数组,你的第三行有问题,你可以像这样重写它:
X_pos = np.array([np.array([X[i] for i in range(6)])[y[:6].astype('bool')]])
对于fasle valuse(y)使用:
y_n = numpy.logical_not(y) X_pos2 = np.array([np.array([X[i] for i in range(6)])[y_n[:6]]])
这是发生的事情:
你取
X
所有6个元素你为numpy数组应用
y
元素的布尔掩码。像你的问题一样将整个结果转换为numpy数组(出于某种原因)
assuming x and y are numpy arrays, your third line has the problem, you could rewrite it like that:
X_pos = np.array([np.array([X[i] for i in range(6)])[y[:6].astype('bool')]])
for the fasle valuse (in y) use:
y_n = numpy.logical_not(y) X_pos2 = np.array([np.array([X[i] for i in range(6)])[y_n[:6]]])
here's what happen:
you take all the 6 elements of
X
you apply a boolean mask of
y
elements for numpy array.converting the whole result to numpy array (for some reason) as in your question..
相关问答
更多-
使用蒙版: mask = a1 != 0 r[mask] = a1[mask] + a2[mask] 这假定r, a1, a2具有相同的形状。 Use a mask: mask = a1 != 0 r[mask] = a1[mask] + a2[mask] This assumes that r, a1, a2 have the same shapes.
-
重塑一个numpy数组(Reshaping a numpy array)[2023-10-06]
要做这种计算,你应该使用numpy。 假设a是你的起始数组: a.transpose() 会做的伎俩 To do this kind of calculations you should use numpy. Assuming that a is your starting array: a.transpose() would do the trick -
由于@Paul Panzer解决方案没有给出与你的函数相同的输出,我通过尝试布尔数组修改了他的工作。 希望它是你想要的。 码: def my_translate(arr, x=0): arr = arr.copy() if x == 0: return arr.copy() elif x > 0: replacement1 = np.zeros(arr.shape) replacement1[:-x] = arr[x:] els ...
-
Numpy:条件总和(Numpy: conditional sum)[2024-03-01]
您可以使用np.diff和np.add.reduceat的聪明应用程序在纯粹的numpy中执行此np.add.reduceat 。 np.diff将为您提供最右列更改的索引: d = np.diff(arr[:, -1]) np.where会将您的布尔索引d转换为np.add.reduceat期望的整数索引: d = np.where(d)[0] reduceat也期望看到零指数,一切都需要reduceat一: indices = np.r_[0, e + 1] 在这里使用np.r_比np.conca ... -
numpy中的数组操作(array manipulation in numpy)[2023-11-27]
给定my_array如上所定义 >>> my_array array([[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2]], [[3, 3, 3, 3, 3], [4, 4, 4, 4, 4]]]) 您只需要在第一个轴上取平均值,如下所示: >>> my_array.mean(axis=0) array([[ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.]]) 如果必须对 ... -
如何在numpy中向量化包含if语句的for循环?(How to vectorize a for loop containing if-statement in numpy?)[2022-06-17]
处理数组或计算中的2个或更多条件时的一般模式是构造一个布尔掩码,并对该掩码为真的元素采取一个动作,并在假的情况下采用不同的动作: res=np.zeros(S.shape[0],dtype=np.float64) mask - S-K>delS res[mask] = ...S[mask] res[~mask] = ...S[~mask] 一种变体是用np.where(mask)来识别索引。 但似乎使您的计算复杂化的是测试不断变化。 也就是说, j+1 K来自j计算。 for j in range(res ... -
您可以使用numpy.column_stack将列表中的所有numpy.column_stack连接到2D数组中。 all_rects = [np.arange(4), np.arange(5, 9)] rect_array = np.column_stack(all_rects) array([[0, 5], [1, 6], [2, 7], [3, 8]]) 获取您尝试获取的值(通过将元素0添加到元素2,将元素1添加到元素4)。 ...
-
假设x和y是numpy数组,你的第三行有问题,你可以像这样重写它: X_pos = np.array([np.array([X[i] for i in range(6)])[y[:6].astype('bool')]]) 对于fasle valuse(y)使用: y_n = numpy.logical_not(y) X_pos2 = np.array([np.array([X[i] for i in range(6)])[y_n[:6]]]) 这是发生的事情: 你取X所有6个元素 你为numpy数组 ...
-
for循环比numpy平均值快,也有点不同的结果(for loop is faster than numpy average, also a bit different result)[2022-06-20]
一个更好的建议 我认为更好的矢量化方法是切片 - (series[slen:2*slen] - series[:slen]).sum()/float(slen**2) 运行时测试和验证 - In [139]: series = np.random.randint(11,999,(200)) ...: slen= 66 ...: # Original app In [140]: %timeit get_trend(series, slen) 100000 loops, best o ... -
我np.where一下如何使用np.where来实现它只是一个X / Y问题 。 所以我将尝试解释如何优化此功能。 我的第一直觉是摆脱for循环,这无论如何都是痛点: import numpy as np from scipy.stats import logistic def func1(y, X, thresholds): ll = 0.0 for row in zip(y, X): if row[0] == 0: ll += logistic. ...