Rounding error in Python with non

I'm beginner in Python, and I have one question. Why does rounding a number like 5.5, 7.5, (anything).5 with odd integer part applying round(num) work correctly (rule 5/4), but rounding number like (anything).5 with non-odd integer part by the same function returns just an integer part? (But if we add a little number like 0.000000001 to that decimal number it works correctly) I mean the

使用非圆整Python中的错误

我是Python的初学者,我有一个问题。 为什么要舍入一个数字,如5.5,7.5,(任何).5与奇数整数部分申请round(num)正常工作(规则5/4),但舍入数字像(任何).5与非奇数整数部分相同的函数只返回一个整数部分? (但是,如果我们添加一个像0.000000001这样的小数字就可以正常工作) 我的意思是下一个: round(9.5) 返回10,它是正确的。 但 round(8.5) 返回8,并且不正确。 和 round(8.5 + 0.0000000000001) 返回9

Numpy Index values for each element in 3D array

I have a 3D array created using the numpy mgrid command so that each element has a certain value and the indexes retain the spatial information. For example, if one summed over the z-axis (3rd dimension) then the the resultant 2D array could be used in matplotlib with the function imshow() to obtain an image with different binned pixel values. My question is: How can I obtain the index values

3D数组中每个元素的Numpy索引值

我有一个使用numpy mgrid命令创建的3D数组,以便每个元素都有一个确定的值,并且索引保留空间信息。 例如,如果在z轴上进行求和(第三维),则可以在matplotlib中使用生成的二维数组和imshow()函数来获取具有不同像素值的图像。 我的问题是:如何获得此网格中每个元素的索引值(a,b,c)? 我需要使用索引值来计算每个点与网格原点的相对角度。 (例如theta = sin-1(sqrt(x ^ 2 + y ^ 2)/ sqrt(x ^ 2 + y ^ 2 + z ^

Dump a NumPy array into a csv file

Is there a way to dump a NumPy array into a CSV file? I have a 2D NumPy array and need to dump it in human-readable format. numpy.savetxt将数组保存到文本文件。 import numpy a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ]) numpy.savetxt("foo.csv", a, delimiter=",") 用熊猫轻松快捷import pandas as pd df = pd.DataFrame(np_array) df.to_csv("file_path.csv") tofile is a convenient function to do th

将NumPy数组转储到csv文件中

有没有办法将NumPy数组转换为CSV文件? 我有一个2D NumPy数组,需要将其转储为可读格式。 numpy.savetxt将数组保存到文本文件。 import numpy a = numpy.asarray([ [1,2,3], [4,5,6], [7,8,9] ]) numpy.savetxt("foo.csv", a, delimiter=",") 用熊猫轻松快捷import pandas as pd df = pd.DataFrame(np_array) df.to_csv("file_path.csv") tofile是一个方便的功能: import numpy as np a = np.asarray([ [1,2,3], [4,5,6],

How to print the full NumPy array?

When I print a numpy array, I get a truncated representation, but I want the full array. Is there any way to do this? Examples: >>> numpy.arange(10000) array([ 0, 1, 2, ..., 9997, 9998, 9999]) >>> numpy.arange(10000).reshape(250,40) array([[ 0, 1, 2, ..., 37, 38, 39], [ 40, 41, 42, ..., 77, 78, 79], [ 80, 81, 82, ..., 117

如何打印完整的NumPy数组?

当我打印一个numpy数组时,我得到一个截断的表示,但我想要完整的数组。 有没有办法做到这一点? 例子: >>> numpy.arange(10000) array([ 0, 1, 2, ..., 9997, 9998, 9999]) >>> numpy.arange(10000).reshape(250,40) array([[ 0, 1, 2, ..., 37, 38, 39], [ 40, 41, 42, ..., 77, 78, 79], [ 80, 81, 82, ..., 117, 118, 119], ...,

Interactive matplotlib plot with two sliders

I used matplotlib to create some plot, which depends on 8 variables. I would like to study how the plot changes when I change some of them. I created some script that calls the matplotlib one and generates different snapshots that later I convert into a movie, it is not bad, but a bit clumsy. I wonder if somehow I could interact with the plot regeneration using keyboard keys to increase / dec

用两个滑块交互式绘制matplotlib图

我用matplotlib创建了一些图,这取决于8个变量。 我想研究当我改变其中一些情节时情节如何改变。 我创建了一些调用matplotlib的脚本,并生成不同的快照,稍后我将其转换为电影,但这并不坏,但有点笨拙。 我想知道是否可以通过键盘按键来与绘图再生相互作用,以增加/减少某些变量的值,并立即查看绘图如何变化。 什么是最好的方法呢? 另外,如果您可以用两个滑块将我指向有趣的链接或与一个情节示例的链接? 除了@tri

Wrapping C++ template method with keyword name using SWIG

I'm wrapping some C++ code using SWIG to generate Python code. The C++ class has a template method as defined as such: class Array { public: template <typename T> std::vector<T> as<T>(); ... } In my SWIG interface file I have wrapped this function as such: %template(fdata) Array::as<float>(); %template(idata) Array::as<int>(); ... This works fine in SWIG

使用SWIG包装关键字名称的C ++模板方法

我使用SWIG封装了一些C ++代码来生成Python代码。 C ++类具有as定义的模板方法: class Array { public: template <typename T> std::vector<T> as<T>(); ... } 在我的SWIG接口文件中,我已经包装了这个功能: %template(fdata) Array::as<float>(); %template(idata) Array::as<int>(); ... 这在SWIG 3.0.0中工作正常,但是当我尝试在3.0.12等更新的版本中编译时,我收到了一堆警告: W

Protocol Buffers in Python 3

I am trying to use Google Protocol Buffers in a Python 3 project. However the generated python files do not want to cooperate with the google.protobuf library. Trying to use the protobuf objects results in a NotImplementedError. My setup: Python 3.4.1 protoc 2.5.0 The problem appears when using these libraries: protobuf-py3 (https://pypi.python.org/pypi/protobuf-py3/2.5.1) python3-p

Python 3中的协议缓冲区

我正在尝试在Python 3项目中使用Google Protocol Buffers。 然而,生成的python文件不想与google.protobuf库合作。 尝试使用protobuf对象会导致NotImplementedError。 我的设置: Python 3.4.1 protoc 2.5.0 使用这些库时出现问题: protobuf-py3(https://pypi.python.org/pypi/protobuf-py3/2.5.1) python3-protobuf(https://pypi.python.org/pypi/python3-protobuf/2.5.0) 例: from pb_test import test_

creating a python dictionary like object from protocol buffers for use in pandas

I currently interface to a server that provides protocol buffers. I can potentially receive a very large number of messages. Currently my process to read the protocol buffers and convert them to a Pandas DataFrame (not a necessary step in general, but Pandas offers nice tools for analyzing datasets) is: Read protocol buffer, it will be a google protobuf object Convert protocol buffers to di

从协议缓冲区创建一个python字典,像对象用于熊猫

我当前连接到提供协议缓冲区的服务器。 我可能会收到大量的消息。 目前,我读取协议缓冲区并将其转换为Pandas DataFrame(通常不是必需步骤,但Pandas提供用于分析数据集的好工具)的过程是: 读协议缓冲区,它将是一个谷歌protobuf对象 使用protobuf_to_dict将协议缓冲区转换为字典 使用pandas.DataFrame.from_records获取DataFrame 这很好,但是,由于我从protobuf中读取了大量的消息,转换为字典,然后转换为熊猫效

generated Constants in Python

I'm using SWIG to create a Python interface to my C++ class library. I can't work out how to utilise the constants created by SWIG in Python . I can't even print their value. For example, both these print statements in Python fail silently... print CONST1 print rep (CONST1) In C++ , I have this #define CONST1 0x20000 const int CONST2 = 0x20000; // No different to #define in SW

在Python中生成常量

我正在使用SWIG为我的C ++类库创建一个Python接口。 我无法弄清楚如何利用Python中由SWIG创建的常量 。 我甚至无法打印他们的价值。 例如,Python中的这两个打印语句都会默默失败...... print CONST1 print rep (CONST1) 在C ++中 ,我有这个 #define CONST1 0x20000 const int CONST2 = 0x20000; // No different to #define in SWIG-generated code. 如果我看看由SWIG创建的Python模块 ,它有这样的东西... CONST1 =

Can I use generated swig code to convert C++ object to PyObject?

I'm working on embedding python into my C++ program using swig. At the moment I have a object written in C++ which I want to pass to a python function. I've created the swig interface to wrap the class. What I'm trying to do is take this C++ object which I've created and pass it to a python function with the ability to use it like I would in C++. Is it possible for me to use

我可以使用生成的swig代码将C ++对象转换为PyObject吗?

我正在使用swig将Python嵌入到我的C ++程序中。 目前我有一个用C ++编写的对象,我想传递给一个python函数。 我创建了swig界面来包装类。 我想要做的是把我创建的这个C ++对象,并将它传递给一个Python函数,并具有像在C ++中一样使用它的能力。 我可以使用swig生成的代码来执行此操作吗? 如果不是,我该如何解决这个问题? 您可以使用PyObject_CallMethod将新创建的对象传递回python。 假设ModuleName.object是一个py