即使一切似乎都是矢量化的,下面的代码运行得太慢了.
from numpy import *
from scipy.sparse import *
n = 100000;
i = xrange(n); j = xrange(n);
data = ones(n);
A=csr_matrix((data,(i,j)));
x = A[i,j]
Run Code Online (Sandbox Code Playgroud)
问题似乎是索引操作是作为python函数实现的,并且调用A[i,j]结果导致以下分析输出
500033 function calls in 8.718 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100000 7.933 0.000 8.156 0.000 csr.py:265(_get_single_element)
1 0.271 0.271 8.705 8.705 csr.py:177(__getitem__)
(...)
Run Code Online (Sandbox Code Playgroud)
也就是说,python函数_get_single_element被调用100000次,这实在是效率低下.为什么不在纯C中实现?有没有人知道解决这个限制的方法,并加快上述代码?我应该使用不同的稀疏矩阵类型吗?

有没有办法在显示matplotlib图时默认自动激活"缩放到矩形"工具?
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-300, 300)
y = x**2-7*x
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.plot(x, y)
# Add something here to activate the "Zoom to rectangle" tool?
plt.show()
Run Code Online (Sandbox Code Playgroud) 我有以下代码,它的工作原理.这基本上重命名列中的值,以便以后可以合并它们.
pop = pd.read_csv('population.csv')
pop_recent = pop[pop['Year'] == 2014]
mapping = {
'Korea, Rep.': 'South Korea',
'Taiwan, China': 'Taiwan'
}
f= lambda x: mapping.get(x, x)
pop_recent['Country Name'] = pop_recent['Country Name'].map(f)
Run Code Online (Sandbox Code Playgroud)
警告: 正在尝试在DataFrame的切片副本上设置值.尝试使用.loc [row_indexer,col_indexer] = value,请参阅文档中的警告:http: //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy pop_recent ['国家名称'] = pop_recent ['国家名称'].地图(f)
我确实谷歌了!但似乎没有任何例子使用地图,所以我不知所措......
当使用matplotlib 2.0.2,python2.7,Win7,64bit创建直方图时,我在bin之间得到垂直条纹,在pdf和png中都可见.我使用latex来创建PDF,我将在pdflatex文档中使用includegraphics.创建的PNG只是一个快速检查.
在Matplotlib 1.5.3中并非如此.如何摆脱分隔各个箱子的这些白线?
事情尝试:
用于生成图像的代码
import matplotlib as mpl
mpl.use('pgf')
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 10, # LaTeX default is 10pt …Run Code Online (Sandbox Code Playgroud) 我正在进行本地化项目并使用最小二乘估计来确定发射机的位置.我需要一种方法来统计我的程序中我的解决方案的"适应性",这可以用来告诉我是否有一个好的答案,或者我需要额外的测量,或者有不好的数据.我已经阅读了一些关于使用"确定系数"或R平方的内容,但未能找到任何好的例子.关于如何表征我是否有一个好的解决方案,或需要额外的测量的任何想法将非常感激.
谢谢!
我的代码给了我以下输出,
grid_lat和grid_lon对应于可能的目标位置的网格的纬度和经度坐标
grid_lat = [[ 38.16755799 38.16755799 38.16755799 38.16755799 38.16755799
38.16755799]
[ 38.17717199 38.17717199 38.17717199 38.17717199 38.17717199
38.17717199]
[ 38.186786 38.186786 38.186786 38.186786 38.186786 38.186786 ]
[ 38.1964 38.1964 38.1964 38.1964 38.1964 38.1964 ]
[ 38.20601401 38.20601401 38.20601401 38.20601401 38.20601401
38.20601401]
[ 38.21562801 38.21562801 38.21562801 38.21562801 38.21562801
38.21562801]
[ 38.22524202 38.22524202 38.22524202 38.22524202 38.22524202
38.22524202]]
grid_lon = [[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 -75.81406878 -75.80607233
-75.79807588]
[-75.83805812 -75.83006167 -75.82206522 …Run Code Online (Sandbox Code Playgroud) 我有一本名为 G 的字典。当我输入 时G. keys (),输出的示例如下:
>>> G.keys ()
[(1490775.0, 12037425.0), (1493775.0, 12042675.0), (1481055.0, 12046305.0), (1503105.0, 12047415.0), (1488585.0, 12050685.0), (1483935.0, 12051405.0),...
Run Code Online (Sandbox Code Playgroud)
当我使用该操作时,key in G结果是错误的。
>>> (1490775.0, 12037425.0) in G
False
Run Code Online (Sandbox Code Playgroud)
为什么我的字典无法识别我的按键?
>>> type (G.keys()[0])
<type 'numpy.void'>
>>> type (G.keys()[0][0])
<type 'numpy.float64'>
>>> type (G.keys()[0][1])
<type 'numpy.float64'>
type(G)
<type 'dict'>
Run Code Online (Sandbox Code Playgroud) 已经问过类似的问题,但没有一个答案完全符合我的需要 - 有些允许多维搜索(又名 matlab 中的“行”选项)但不返回索引。有些返回索引但不允许行。我的数组非常大(1M x 2)并且我成功地制作了一个有效的循环,但显然这非常慢。在 matlab 中,内置的 ismember 函数大约需要 10 秒。
这是我要找的:
a=np.array([[4, 6],[2, 6],[5, 2]])
b=np.array([[1, 7],[1, 8],[2, 6],[2, 1],[2, 4],[4, 6],[4, 7],[5, 9],[5, 2],[5, 1]])
Run Code Online (Sandbox Code Playgroud)
执行此操作的确切 matlab 函数是:
[~,index] = ismember(a,b,'rows')
Run Code Online (Sandbox Code Playgroud)
在哪里
index = [6, 3, 9]
Run Code Online (Sandbox Code Playgroud) 我有一个大型数据集,具有以下结构
User X
1 0
1 0
2 0
2 0
2 1
3 0
3 0
Run Code Online (Sandbox Code Playgroud)
我想获取数据的子集,使得每个用户的列X的总和为0.给定上述示例,子集应仅包括用户1和3的观察结果,如下所示
User X
1 0
1 0
3 0
3 0
Run Code Online (Sandbox Code Playgroud)
有没有办法使用groupby函数执行此操作而不分组数据?我希望子集包含个别观察.
我正在尝试使用 python 绘制矢量场散度的轮廓图,然后向该图中添加一个颜色条。我的级别旨在从 -0.01 到 0.01 在零附近对称。
这是我的代码的一部分:
div_levs = [-0.01, -0.005, -0.0025, 0.0025, 0.005, 0.01]
col = ['Blue', 'SteelBlue', 'White', 'Orange', 'Red']
c = plt.contourf(iwrf['x'], iwrf['y'], np.squeeze(iwrf['DIV'][ind_lev,:,:]),
levels=div_levs, colors=col, extend='both')
c.cmap.set_over('Magenta')
c.cmap.set_under('MidnightBlue')
bar = plt.colorbar(ticks=div_levs)
bar.set_label('1/s')
Run Code Online (Sandbox Code Playgroud)
如果我执行 python 脚本,它就可以工作,并且所有内容都以正确的方式绘制,但颜色图标有:
0.9900, 0.9950, 0.9975, 1.025, 1.0050, 1.0100
Run Code Online (Sandbox Code Playgroud)
并且在颜色条的顶部显示“-1”。
我已经尝试了很多,包括在创建颜色条后设置它的刻度,或者在调试模式下设置刻度,但似乎没有任何改变这种行为。
对此有何想法?
我有一个由0和1组成的字符串,就像'00101'.我想将它转换为numpy数组numpy.array([0,0,1,0,1].
我正在使用for循环:
import numpy as np
X = np.zeros((1,5),int)
S = '00101'
for i in xrange(5):
X[0][i] = int(S[i])
Run Code Online (Sandbox Code Playgroud)
但由于我有很多字符串,每个字符串的长度是1024,这种方式非常慢.有没有更好的方法来做到这一点?
python ×10
numpy ×4
matplotlib ×3
pandas ×2
scipy ×2
colorbar ×1
dictionary ×1
format ×1
indexing ×1
key ×1
matlab ×1
optimization ×1
pgf ×1
python-2.7 ×1
statistics ×1
types ×1