我有一些这种格式的点(大约 3000)和边(大约 6000):
points = numpy.array([1,2],[4,5],[2,7],[3,9],[9,2])
edges = numpy.array([0,1],[3,4],[3,2],[2,4])
Run Code Online (Sandbox Code Playgroud)
其中边是点的索引,因此每条边的开始和结束坐标由下式给出:
points[edges]
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种更快/更好的方法来绘制它们。目前我有:
from matplotlib import pyplot as plt
x = points[:,0].flatten()
y = points[:,1].flatten()
plt.plot(x[edges.T], y[edges.T], 'y-') # Edges
plt.plot(x, y, 'ro') # Points
plt.savefig('figure.png')
Run Code Online (Sandbox Code Playgroud)
我读到了有关 lineCollections 的内容,但不确定如何使用它们。有没有办法更直接地使用我的数据?这里的瓶颈是什么?
一些更真实的测试数据,绘图时间约为132秒:
points = numpy.random.randint(0, 100, (3000, 2))
edges = numpy.random.randint(0, 3000, (6000, 2))
Run Code Online (Sandbox Code Playgroud) 如何在 numpy 二维数组中存储列表?
import numpy
A = numpy.empty([5,5], dtype=**?**)
Run Code Online (Sandbox Code Playgroud)
这里变量列表类型的数据类型应该是什么?或者是否有不同的方法来实现这一点,我强烈认为是这种情况。
我有一个 numpy 直方图,我想将其输出为制表符分隔的文本文件。我的代码如下:
targethist = np.histogram(targetlist, bins=ilist)
print targethist
np.savetxt('ChrI_dens.txt',targethist,delimiter='\t')
Run Code Online (Sandbox Code Playgroud)
targetlist 和 ilist 是长整数列表。我得到以下输出:
(array([0, 0, 0, ..., 0, 0, 0]), array([ 1, 10000, 20000, ..., 15060000, 15070000, 15072422])) 回溯(最近一次调用) :文件“target_dens_np.py”,第 62 行,在 np.savetxt('ChrI_dens.txt',targethist,delimiter='\t') 文件“/Library/Frameworks/Python.framework/Versions/7.3/lib/python2. 7/site-packages/numpy/lib/npyio.py”,第 979 行,在 savetxt fh.write(asbytes(format % tuple(row) + newline)) 中 TypeError:需要 float 参数,而不是 numpy.ndarray
似乎直方图数组已创建,但我在 np.savetxt() 行中做错了一些事情。我已阅读文档,但不明白为什么此函数中的任何参数都需要浮点数。我哪里出错了?
例如,我有以下数组:
x = [0, 1, 2, 3, 4.5, 5]
y = [2, 8, 3, 7, 8, 1]
Run Code Online (Sandbox Code Playgroud)
我希望能够执行以下操作x:
>>> what_is_y_when_x_is(2)
(2, 3)
>>> what_is_y_when_x_is(3.1) # Perhaps set rules to round to nearest (or up or down)
(3, 7)
Run Code Online (Sandbox Code Playgroud)
另一方面,当给出y:
>>> what_is_x_when_y_is(2)
(0, 2)
>>> what_is_x_when_y_is(max(y))
([1, 4.5], 8)
Run Code Online (Sandbox Code Playgroud)
我可以绘制与使用封闭分析函数的y对比结果x,只需调用foo_function(x). 但是,我正在运行数值模拟,其数据图没有闭合解析解。
我之前解决过类似的问题,大致是这样处理的:
what_is_y_when_x_is(some_x)
x中搜索some_x.i.y[i]。有一个更好的方法吗?也许是内置numpy …
我用图像数据制作了一个 numpy 数组。我想将 numpy 数组转换为一维数组。
import numpy as np
import matplotlib.image as img
if __name__ == '__main__':
my_image = img.imread("zebra.jpg")[:,:,0]
width, height = my_image.shape
my_image = np.array(my_image)
img_buffer = my_image.copy()
img_buffer = img_buffer.reshape(width * height)
print str(img_buffer.shape)
Run Code Online (Sandbox Code Playgroud)
128x128 的图像在这里。

然而,该程序打印出 (128, 128)。我想img_buffer成为一个一维数组。我如何重塑这个数组?为什么 numpy 实际上不会将数组重塑为一维数组?
假设我有一组 2D 坐标,表示 2D 规则网格的单元中心。我想为网格中的每个单元格找到每个方向上两个最近的邻居。
如果分配给每个单元格和索引定义如下,那么问题就非常简单:
idx_cell = idx+N*idy
其中 N 是网格中单元格的总数,idx=x/dx 和 idy=y/dx,其中 x 和 y 是单元格的 x 坐标和 y 坐标,dx 是单元格的大小。
例如,idx_cell=5 的小区的相邻小区是 idx_cell 等于 4,6(对于 x 轴)和 5+N,5-N(对于 y 轴)的小区。
我遇到的问题是,对于大型(N>1e6)数据集,我的算法实现非常慢。
例如,为了获取 x 轴的邻居,我这样做
[x[(idx_cell==idx_cell[i]-1)|(idx_cell==idx_cell[i]+1)] for i in cells]
您认为有最快的方法来实现这个算法吗?
在numpy 页面上他们给出了示例
s = np.random.dirichlet((10, 5, 3), 20)
Run Code Online (Sandbox Code Playgroud)
这一切都很好、很棒;但是如果您想从 2D alpha 数组生成随机样本怎么办?
alphas = np.random.randint(10, size=(20, 3))
Run Code Online (Sandbox Code Playgroud)
如果您尝试np.random.dirichlet(alphas), np.random.dirichlet([x for x in alphas]), or np.random.dirichlet((x for x in alphas)),
则会产生
ValueError: object too deep for desired array. 唯一有效的似乎是:
y = np.empty(alphas.shape)
for i in xrange(np.alen(alphas)):
y[i] = np.random.dirichlet(alphas[i])
print y
Run Code Online (Sandbox Code Playgroud)
...这对于我的代码结构来说远非理想。为什么会出现这种情况,有人能想到一种更“类似 numpy”的方法吗?
提前致谢。
我有一个功能:
f = x**0.5*numpy.exp(-x/150)
Run Code Online (Sandbox Code Playgroud)
我使用 numpy 和 matplot.lib 生成 f 作为 x 和 x 的函数的图:
x = np.linspace(0.0,1000.0, num=10.0)
Run Code Online (Sandbox Code Playgroud)
我想知道如何创建随机 x 值的数组,以便使用我首先制作的 x 数组为此函数创建相同的图?
布莱恩
当使用 Sklearn 的 RandomForestRegressor 时,如何获得回归的残差?我想绘制这些残差以检查线性度。
当文件中只有一个值时,numpy.loadtxt()只返回值而不是数组,如何避免?
先感谢您!
例如文件中只有 12345
12345.6
Run Code Online (Sandbox Code Playgroud)
numpy.loadtxt() 返回
12345.6
Run Code Online (Sandbox Code Playgroud)
代替
array([12345.6])
Run Code Online (Sandbox Code Playgroud) numpy ×10
python ×8
matplotlib ×2
csv ×1
histogram ×1
list ×1
python-2.7 ×1
random ×1
scikit-learn ×1
scipy ×1
search ×1