我想在列表中总结NumPy向量列表。 (在这个例子中,它是一个包含2个项目的列表,但在我的情况下,列表可以是任何大小。)如何将它们加到一个新的向量中?
<table class="table table-datatable table-bordered" id="tableID">
<thead>
<tr>
<th class="nosort"><input type="checkbox" id="checkAllreInvitation" /></th>
<th class="sort-alpha">Employee name</th>
<th class="sort-alpha">Send Date</th>
<th class="sort-alpha">Sender</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="userUid[]" value="{user.uid}" id="checkAllreInvitation" class="checkItemre validate[required]" /></td>
<td>Alexander Schwartz</td>
<td>27.12.2015</td>
<td>dummy@email.com</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
$('#tableID').DataTable({
'iDisplayLength':100,
"aaSorting": [[ 0, "asc" ]],
'aoColumnDefs': [{
'bSortable': false,
'aTargets': ['nosort']
}]
});
});
</script>
a = np.array([100, 100])
b = np.array([200, 200])
my_list = [a, b]
ab = np.add(my_list)
有效,但不是列表。我已经尝试了np.add(a, b)
和np.add(*my_list)
以及np.add(np.array(my_list))
,但没有成功。这样做的正确方法是什么?谢谢!
答案 0 :(得分:5)
np.add.reduce()
您可以使用reduce
的{{1}}属性:
np.add
结果:
a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c]
np.add.reduce(L)
所有带有两个参数的通用函数都有一个array([1300, 2300])
属性,它适用于reduce
这个函数,即:
reduce
变为:
np.add.reduce(L)
如果列表np.add(np.add(L[0], L[1]), L[2])
变大,请添加更多括号和相应的np.add
调用。
来自文档:
文档字符串:
L
通过沿一个轴应用ufunc,将
reduce(a, axis=0, dtype=None, out=None, keepdims=False)
的维度减一。
a
或者,您可以沿第一轴使用np.sum()
:
np.sum
两者的表现似乎都是一样的。
对于小型阵列:
>>> np.sum(L, axis=0)
array([1300, 2300
a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c, a, b, c, a, b, c]
加快了一点:
reduce
对于大型阵列:
%timeit np.sum(L, axis=0)
10000 loops, best of 3: 20.7 µs per loop
%timeit np.add.reduce(L)
100000 loops, best of 3: 15.7 µs per loop
没有区别:
size = int(1e6)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
L = [a, b, c, a, b, c, a, b, c]
答案 1 :(得分:1)
这是你的意思吗?
import numpy as np
a = np.array([100, 100])
b = np.array([200, 200])
my_list = [a, b]
# add them up "vertically"
print np.vstack(my_list).sum(axis=0)
print np.vstack(tuple(my_list)).sum(axis=0) # I thought it had to be a tuple but apparently not!
[300 300]
[300 300]
答案 2 :(得分:0)
您可以使用import UIKit
class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet weak var myImageVie2: UIImageView!
let picker = UIImagePickerController()
func noCamera(){
let alertVC = UIAlertController(title: "No Camera",message: "Sorry, this device has no camera",preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK",style:.Default,handler: nil)
alertVC.addAction(okAction)
presentViewController(alertVC,animated: true,completion: nil)
}
@IBAction func photoFromLibrary(sender: UIBarButtonItem) {
picker.allowsEditing = false //2
picker.sourceType = .PhotoLibrary //3
picker.modalPresentationStyle = .Popover
presentViewController(picker,animated: true,completion: nil)//4
picker.popoverPresentationController?.barButtonItem = sender
}
//take a picture, check if we have a camera first.
@IBAction func shootPhoto(sender: UIBarButtonItem) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraCaptureMode = .Photo
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,animated: true,completion: nil)
} else {
noCamera()
}
}
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject])
{
var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
myImageView.contentMode = .ScaleAspectFit
myImageView.image = chosenImage
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true,completion: nil)}
}
或np.hstack
:
np.concatenate
答案 3 :(得分:0)
可能是减少的理想候选人
>>> a = np.array([100, 100])
>>> b = np.array([200, 200])
>>> c = np.array([300, 300])
>>> reduce(lambda x,y: np.add(x,y), [a,b,c])
array([600, 600])