Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 关于数组操作的理解中的If语句_Python_Numpy_List Comprehension - Fatal编程技术网

Python 关于数组操作的理解中的If语句

Python 关于数组操作的理解中的If语句,python,numpy,list-comprehension,Python,Numpy,List Comprehension,我试图利用numpy优化来避免嵌套循环: 当前,我有一个表达式: pred = sum((estimator.predict(X) == classes).T * w1 + (estimator.predict(X) == classes).T * w2 for estimator, w1, w2 in zip(self.estimators_, self.estimator_w

我试图利用numpy优化来避免嵌套循环:

当前,我有一个表达式:

pred = sum((estimator.predict(X) == classes).T * w1 + (estimator.predict(X) == classes).T * w2
               for estimator, w1, w2 in zip(self.estimators_,
                                            self.estimator_weights_pos,
                                            self.estimator_weights_neg))
其中,estimator.predict(X)返回该矩阵:

[[True-True…True-False-False]
[假假假假。。。 假-真-真]]

这将导致另一个矩阵,该矩阵将所有乘法分数相加,例如

[2.53358017 3.96826913][2.53358017 3.96826913][4.17461936 2.32722994]…[4.60035727 1.90149203][3.77621808 2.72563123][4.60035727 1.90149203]

但是,我想拆分此操作。每当“estimator.predict(X)”运算的第一行有一个为真的元素时,我只希望w1用于乘法,当第二行的元素为真时,只希望w2用于乘法

举个例子,

self.estimator_weights_pos = [1, 2]
self.estimator_weights_neg = [0.1, 0.2]
self.estimators_[0].predict(X) = [[True, True, False][False, False, True]]
self.estimators_[1].predict(X) = [[True, False, False][False, True, True]]
因此,第一次迭代的结果是:

[10][10][0.1]]

第二次迭代的结果是:

[30][10.2][0.3]]

当前表达式将导致:

[1.10][1.10][01.1]]

[3.30][1.12.2][0.3]]

作为一种天真的方法,我已经做到了:

pred_pos =  sum((estimator.predict(X) == classes).T[:,0] * w1
                    for estimator, w1 in zip(self.estimators_,
                                            self.estimator_weights_pos))

pred_neg =  sum((estimator.predict(X) == classes).T[:,1] * w1
                    for estimator, w1 in zip(self.estimators_,
                                            self.estimator_weights_neg))

pred = np.column_stack((pred_pos, pred_neg))

在遵循逻辑方面仍然有点困难,但我认为这至少接近于您所寻找的:

pred = sum((estimator.predict(X) == classes).T * (w1 if estimator_[0].predict(x) else 0) + (estimator.predict(X) == classes).T * (w2 if estimator_[1].predict(x) else 0)
               for estimator, w1, w2 in zip(self.estimators_,
                                            self.estimator_weights_pos,
                                            self.estimator_weights_neg))
或者,根据您的意思,如果问题:

pred = sum((estimator.predict(X) == classes).T * w1 * estimator_[0].predict(x) + (estimator.predict(X) == classes).T * w2 * estimator_[1].predict(x)
                   for estimator, w1, w2 in zip(self.estimators_,
                                                self.estimator_weights_pos,
                                                self.estimator_weights_neg))

在遵循逻辑方面仍然有点困难,但我认为这至少接近于您所寻找的:

pred = sum((estimator.predict(X) == classes).T * (w1 if estimator_[0].predict(x) else 0) + (estimator.predict(X) == classes).T * (w2 if estimator_[1].predict(x) else 0)
               for estimator, w1, w2 in zip(self.estimators_,
                                            self.estimator_weights_pos,
                                            self.estimator_weights_neg))
或者,根据您的意思,如果问题:

pred = sum((estimator.predict(X) == classes).T * w1 * estimator_[0].predict(x) + (estimator.predict(X) == classes).T * w2 * estimator_[1].predict(x)
                   for estimator, w1, w2 in zip(self.estimators_,
                                                self.estimator_weights_pos,
                                                self.estimator_weights_neg))

我认为这是可能的,不需要对现有代码进行太多修改,但我对您的示例矩阵有点困惑。如果第一行中的任何元素为true,则仅使用w1;如果第2行中的任何元素为true,则仅使用w2。如您的示例所示,如果第1行和第2行都有一个true元素,该怎么办?如果两者都没有真元素呢?它们总是互补的(如果是正的,那么另一个将是负的)。我认为这是可能的,不需要对现有代码进行太多修改,但是我对您的示例矩阵有点困惑。如果第一行中的任何元素为true,则仅使用w1;如果第2行中的任何元素为true,则仅使用w2。如您的示例所示,如果第1行和第2行都有一个true元素,该怎么办?如果两者都没有真的元素呢?它们总是互补的(如果是正的,那么另一个将是负的)。在这种情况下,我将不得不使用estimator.predict()*4,如果X很大,这将非常昂贵。天真的方法使用it estimator.predict()*2好吧,您没有共享estimator.predict或X是什么或X来自何处(外部循环)。如果你想分享一个更完整的代码视图,一个更好的解决方案可能是显而易见的。否则,我建议尝试使用estimator.predict(X)作为zip语句。如果没有做到这一点,您可能需要研究numpy.vectorize()、Numba或Cython,如果性能是您所追求的。问题是,在这种情况下,我将不得不使用estimator.predict()*4,如果X很大,它的成本会高得让人望而却步。天真的方法使用it estimator.predict()*2好吧,您没有共享estimator.predict或X是什么或X来自何处(外部循环)。如果你想分享一个更完整的代码视图,一个更好的解决方案可能是显而易见的。否则,我建议尝试使用estimator.predict(X)作为zip语句。如果不能做到这一点,您可能需要研究numpy.vectorize()、Numba或Cython,以确定您所追求的性能。