我load对librosa的read功能和的功能之间的区别有疑问scipy.io.wavfile。
from scipy.io import wavfile
import librosa
fs, data = wavfile.read(name)
data, fs = librosa.load(name)
Run Code Online (Sandbox Code Playgroud)
导入的语音文件是同一文件。如果运行上面的代码,则数据的值从两个函数中得出的结果是不同的。我想知道为什么数据值不同。
我foo在 Python 扩展模块中有一个函数,它应该向 Python 返回一个整数元组。这可以使用Py_BuildValue以下方法轻松完成:
static PyObject*
foo(PyObject* self, PyObject* args)
{
int a = 0;
int b = 0;
/* calculations and stuff */
PyObject* out = Py_BuildValue("(iii)", a, b, a+b);
Py_INCREF(out);
return out;
}
Run Code Online (Sandbox Code Playgroud)
而不是Py_BuildValue,我想使用PyTuple_Pack,它确保返回值确实是一个元组。
在Python的C API文档说,PyTuple_Pack(3, a, b, a+b)相当于Py_BuildValue("(iii)", a, b, a+b)。这两个函数都返回一个新的类型引用PyPbject*。
因此,鉴于上面的代码,
static PyObject*
foo(PyObject* self, PyObject* args)
{
/* ... */
PyObject* out = PyTuple_Pack(3, a, b, a+b);
Py_INCREF(out); …Run Code Online (Sandbox Code Playgroud) 我有一个带一个参数的函数,它应该以 anint或 aNone作为参数。有几种方法可以为这种复合类型创建类型别名:
# test.py
import typing
IntOrNone_1 = typing.TypeVar('IntOrNone_1', int, None)
IntOrNone_2 = typing.Union[int, None]
def my_func1(xyz: IntOrNone_1):
return xyz
def my_func2(xyz: IntOrNone_2):
return xyz
my_func1(12)
my_func1(None)
my_func1(13.7)
my_func1('str')
my_func2(12)
my_func2(None)
my_func2(13.7)
my_func2('str')
Run Code Online (Sandbox Code Playgroud)
两种方法都按照我的预期执行,但是,对应的mypy错误略有不同,但基本上具有相同的含义。
test.py:14: 错误:“my_func1”的类型变量“IntOrNone_1”的值不能是“float”
test.py:15: 错误:“my_func1”的类型变量“IntOrNone_1”的值不能是“str”
test.py:19: 错误:“my_func2”的参数 1 具有不兼容的类型“float”;预期“可选[int]”
test.py:20: 错误:“my_func2”的参数 1 具有不兼容的类型“str”;预期“可选[int]”
我倾向于使用第二种方法,因为它还会报告导致错误的参数。
我认为这两种方法确实等效,还是首选其中之一?
我基本上想在其支持上“imshow”三维狄利克雷分布的pdf。下面的函数simplex计算该支撑上的常规点,这些点存储在数组 中sim。该数组pdf保存 中每一行的标量密度sim。
我首先想到的是使用三角测量。然而,color的参数plot_trisurface仅支持所有三角形使用一种颜色。根据 z 坐标值设置cmap三角形的颜色(见图 1)。也plot_trisurface忽略了facecolorskwarg。然而,我想要的是基于 为表面着色pdf。
作为一种解决方法,我发现可以将表面插值为 3d 散点图。这通常会给出所需的可视化效果,但我清楚地看到它是一个散点图;特别是在边界上。(见图 2。)
有没有办法将 pdf 的投影绘制到单纯形上?
import itertools
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
def simplex(n_vals):
base = np.linspace(0, 1, n_vals, endpoint=False)
coords = np.asarray(list(itertools.product(base, repeat=3)))
return coords[np.isclose(coords.sum(axis=-1), 1.0)]
sim = simplex(20)
pdf = stats.dirichlet([1.1, 1.5, 1.3]).pdf(sim.T)
fig1 = plt.figure()
ax1 = fig1.add_subplot(1, 2, 1, …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码来测试matplotlib savefig()函数的性能:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import rand
import time
for i in xrange(10):
init = time.time()
x = rand(100)
y = rand(100)
t_init=time.time()-init
init = time.time()
ax = plt.axes()
t_axes=time.time()-init
init = time.time()
num, _, _ = np.histogram2d(x,y)
t_hist = time.time()-init
init = time.time()
ax.imshow(num, extent=[-.5,.5,-.5,.5], interpolation="bicubic")
t_imshow = time.time()-init
init = time.time()
t = ax.text(i*.1,.1, "Value ="+str(i))
plt.savefig("test/"+str(i)+".png")
t.remove()
t_savefig = time.time()-init
print t_init, t_axes, t_hist, t_imshow, t_savefig
Run Code Online (Sandbox Code Playgroud)
出乎意料的是savefig(),每次迭代都会降低性能,如下表的最后一列所示: …
我尝试绘制一些高质量的时间序列,但matplotlib无法保存高质量的数字.在绘图中输出如下图所示,这是截图.
但是使用plt.safefig()或保存按钮会给我以下结果
问题的核心是我必须使用png-backend保存图.Safing as pdf,创建了一个文件,其他应用程序无法打开.即使文件<500 kb.安全性为.eps时也会发生同样的情况.我的代码是这样的:
fig = plt.figure(1, figsize=(10, 6))
ax = fig.add_subplot(111, autoscale_on=True, ylim=(-1,1))
ax.plot(sig_obj, '-k', alpha=.3)
# plot of vertical lines ...
plt.savefig('figure_1.png') # or .pdf
Run Code Online (Sandbox Code Playgroud)
其中sig_obj是一个长度> 350000的数组.在OS X 10.9.4上使用Python 3.4和matplotlib 1.3.3.
有没有人有想法提高我的情节质量?
这是我试图实现的代码的简单版本:
for key, value in data.items():
for key2, value2 in data.items():
# other stuff
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种key2从key. 因此,例如在 C++ 中,它将是:
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
# other stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我需要能够浏览整个数据集。key是一个字符串并且value是一个没有任何特定顺序的列表,所以我不能只检查是否key2< key。
我有一个列表a,然后我max(a)用来获取最大值。下一步我试图用来enumerate()获得最大值的第一个位置。但它注意到我只能enumerate()用于 int 列表?浮动列表怎么样?有没有办法在浮点数(也有整数)列表中获得最大值的第一个位置?非常感谢
a = [1.5, 1.8, 3.1, 4.2, 5.5, 3.2, 4, 2, 1, 5.5, 3, 2.7]
b = max(a)
maxIndex = [i for i, j in enumerate(b) if j == b][0]
Run Code Online (Sandbox Code Playgroud)
回溯(最近一次调用最后一次):文件“”,第 1 行,在 TypeError 中:“float”对象不可迭代
考虑以下程序:
#include <stdio.h>
int main(void)
{
int a[] = {1, 2, 3};
for (size_t i = 0; i < 3; i++)
printf ("%i\n", a[0, i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,a例如在Python中,像二维数组一样访问一维数组。但是,此代码的确带有unused-value警告。我希望它会产生错误,因为我一直认为这是因为在C中多索引是完全错误的(请参见K&R第112页)。令我惊讶的是,以上代码确实打印出了数组元素。
如果将a[0, i]第六行更改为a[i, 0],则第一个数组元素将被打印3次。如果您使用a[i, 1]第二个元素,则将打印三遍。
如何将一维数组上语法错误的多索引转换为指针算术,并且a[i, 0]未使用结果的什么值?
而且,是的,我知道如何在C中进行多索引。
python ×8
matplotlib ×3
plot ×2
python-3.x ×2
arrays ×1
c ×1
cpython ×1
image ×1
indexing ×1
librosa ×1
performance ×1
scipy ×1
typechecking ×1
typing ×1