我对很多地方看到的以下代码感到困惑:
cdef list my_list
Run Code Online (Sandbox Code Playgroud)
我很困惑因为list不是C数据类型,而是Python数据类型.为什么人们会使用cdef而不是def呢?
我非常喜欢这个功能,因为有时候我需要在我的代码中使用list,而且在没有python列表的情况下将代码重构为C需要付出巨大的努力.我很困惑Cython正在做什么,当a list定义cdef并且让所有方法暴露给我们时.
我试图搜索文件,但没有运气.任何帮助,将不胜感激!
有没有快速的方法来阅读Python中的最后N行CSV文件,使用numpy或pandas?
我不能在skip_headerin numpy或skiprowin中,pandas因为文件的长度不同,我总是需要最后N行.
我知道我可以使用纯Python从文件的最后一行逐行读取,但这将非常慢.如果必须的话,我可以这样做,但是使用numpy或者pandas(实质上使用C)的更有效方式将非常受欢迎.
我试图在ndarray中找到一个浮点数。由于我使用的软件包(Abaqus),它输出的精度有点低。例如,10类似于10.00003。因此,我想知道是否有一种“正确”的方法可以做到,这比我的代码更整洁。
示例代码:
import numpy as np
array = np.arange(10)
number = 5.00001
Run Code Online (Sandbox Code Playgroud)
如果我这样做?
idx = np.where(number==array)[0][0]
Run Code Online (Sandbox Code Playgroud)
然后结果为空,因为5.00001不等于5。
现在我正在做:
atol = 1e-3 # Absolute tolerance
idx = np.where(abs(number-array) < atol)[0][0]
Run Code Online (Sandbox Code Playgroud)
可以,并且不会太杂乱...但是我想知道是否会有更整洁的方式来做到这一点。谢谢!
PS:这numpy.allclose()是另一种实现方法,但是我需要使用number * np.ones([array.shape[0], array.shape[1]]),对我来说似乎仍然很冗长...
编辑:非常感谢大家的奇妙答案!np.isclose()是我要查找的确切函数,由于它不在文档中,所以我错过了它。如果不是您,我不会意识到这一点,除非他们更新了文档。再次感谢你!
首先,我确实意识到这是一个非常简单的问题,请耐心等待.
如何在python中获取字符串的数量?我想做这样的事情:
def func(input_strings):
# Make the input_strings iterable
if len(input_strings) == 1:
input_strings = (input_strings, )
# Do something
for input_string in input_strings:
analyze_string(input_string)
return
Run Code Online (Sandbox Code Playgroud)
所以使用这个函数,如果输入是一个列表,['string1','string2','string3'],它将循环遍历它们; 如果输入只是一个字符串,如'string1',那么它仍然会处理它,而不是抛出异常.
但是,len(str)返回字符串中的字符数,并且不会给我"1".
我真的很感谢你的帮助!
我有一个如下所示的列表:
my_list = [[20, 15, 10], [15, 22, 37, 46], [22, 91]]
Run Code Online (Sandbox Code Playgroud)
所以它是二维的,但不是每一行都有相同数量的元素.
我现在有一个扁平的ndarray,如:
my_ndarray = np.array([9, 2, 4, 4, 1, 6, 7, 8, 17])
Run Code Online (Sandbox Code Playgroud)
与my_list具有相同数量的元素.现在我想将my_ndarray塑造成与my_list相同,即:
my_ndarray = [[9, 2, 4], [4, 1, 6, 7], [8, 17]]
Run Code Online (Sandbox Code Playgroud)
所以我们可以注意到,my_list和my_ndarray都包含3个子列表,第一个子列表包含3个元素,第二个子列表包含4个,第3个子列表包含2个元素.
有一个巧妙的方法来做到这一点?
谢谢!
我很好奇是否有更好的方法来做到这一点。我有一个包含两列的 csv 文件(这相当常见),例如第一列是时间戳,第二列是数据。
# temp.csv
0., 0.
1., 5.
2., 10.
3., 15.
4., 10.
5., 0.
Run Code Online (Sandbox Code Playgroud)
然后我想读取这个 temp.csv 文件:
import numpy as np
my_csv = np.genfromtxt('./temp.csv', delimiter=',')
time = my_csv[:, 0]
data = my_csv[:, 1]
Run Code Online (Sandbox Code Playgroud)
这完全没问题,但我只是好奇是否有更优雅的方法来做到这一点,因为这是一种相当常见的做法。
谢谢你!
-肖恩
由于这是一种非常普遍的做法...我想知道matplotlib是否等效于scikit-image中的此函数?
from skimage import io
im = io.imread(fname, as_grey=True)
Run Code Online (Sandbox Code Playgroud)
要直接读取RGB文件为灰度?
我需要使用等效的matplotlib,因为我正在使用它来绘制结果。正如我所观察到的,似乎io.imread读取的ndarray与plt.imread读取的ndarray不相等。
谢谢!
我是 Cython 的新手,正在尝试学习如何将其与 numpy 一起使用来加速代码。我一直在关注此链接中的教程。
我在这里复制了他们的代码:
from __future__ import division
import numpy as np
# "cimport" is used to import special compile-time information
# about the numpy module (this is stored in a file numpy.pxd which is
# currently part of the Cython distribution).
cimport numpy as np
# We now need to fix a datatype for our arrays. I've used the variable
# DTYPE for this, which is assigned to the usual NumPy runtime
# type info …Run Code Online (Sandbox Code Playgroud) 很抱歉对 Fortran 90 和 f2py 都不熟悉。
我使用的是 Windows 64 位、Python 3.4 64 位、gfortran。Numpy 版本是 1.9.1,我在 gnu.py 中评论了“raise NotImplementedError("Only MS compiler supported with gfortran on win64")”,如以下链接所示:http ://scientificcomputingco.blogspot.com.au /2013/02/f2py-on-64bit-windows-python27.html
我在 fortran 中有一个模块,编写如下,带有模块范围变量dp:
! testf2py.f90
module testf2py
implicit none
private
public dp, i1
integer, parameter :: dp=kind(0.d0)
contains
real(dp) function i1(m)
real(dp), intent(in) :: m(3, 3)
i1 = m(1, 1) + m(2, 2) + m(3, 3)
return
end function i1
end module testf2py
Run Code Online (Sandbox Code Playgroud)
然后,如果我跑 f2py -c testf2py.f90 -m testf2py
它会报告错误,指出未声明 …
python ×9
numpy ×6
scipy ×3
csv ×2
cython ×2
f2py ×1
fortran ×1
fortran90 ×1
matplotlib ×1
pandas ×1
precision ×1
scikit-image ×1
statsmodels ×1