在Python中,理论上,哪种方法应该更快test1和test2(假设相同的值x).我试过用%timeit但看到的差别很小.
import numpy as np
class Tester():
def __init__(self):
self.x = np.arange(100000)
def test1(self):
return np.sum(self.x * self.x )
def test2(self,x):
return np.sum(x*x)
Run Code Online (Sandbox Code Playgroud) 是否有更多pythonic方式来执行以下代码?我想在一行中做到这一点
parsed_rows是一个可以返回大小为3或无的元组的函数.
parsed_rows = [ parse_row(tr) for tr in tr_els ]
data = [ x for x in parsed_rows if x is not None ]
Run Code Online (Sandbox Code Playgroud) 晚上好,
我试图加快这段代码中的循环.我已经阅读了numpy docs,但无济于事.np.accumulate看起来几乎是我需要的,但并不完全.
我该怎么做才能加快循环速度?
import numpy as np
N = 1000
AR_part = np.random.randn(N+1)
s2 = np.ndarray(N+1)
s2[0] = 1.0
beta = 1.3
old_s2 = s2[0]
for t in range( 1, N+1 ):
s2_t = AR_part[ t-1 ] + beta * old_s2
s2[t] = s2_t
old_s2 = s2_t
Run Code Online (Sandbox Code Playgroud)
为了回应沃伦,我更新了我的代码:
从scipy.signal import lfilter,lfiltic导入numpy as np
N = 1000
AR_part = np.random.randn(N+1)
beta = 1.3
def method1( AR_part):
s2 = np.empty_like(AR_part)
s2[0] = 1.0
old_s2 = s2[0]
for t in range( 1, …Run Code Online (Sandbox Code Playgroud) 我在Anacondas Python 3.3中安装Tweepy时遇到问题。
首先,我进入了Python 3.3安装的scripts目录。然后我跑了
easy_install tweepy
Run Code Online (Sandbox Code Playgroud)
如果我在默认的Python Scripts目录中运行它,则它可以工作,但是那是我不想使用的旧Python版本。
我的输出如下。它说了有关导入报价的内容。
C:\Anaconda\envs\py33\Scripts>easy_install tweepy
Searching for tweepy
Reading http://pypi.python.org/simple/tweepy/
Best match: tweepy 2.3.0
Downloading https://pypi.python.org/packages/source/t/tweepy/tweepy-2.3.0.tar.gz#md5=065c80d244360988c61d64b5dfb7e229
Processing tweepy-2.3.0.tar.gz
Writing c:\users\jon\appdata\local\temp\easy_install-tmda2q\tweepy-2.3.0\setup.cfg
Running tweepy-2.3.0\setup.py -q bdist_egg --dist-dir c:\users\jon\appdata\local\temp\easy_install-tmda2q\tweepy-2.3.0\egg-dist-tmp-006ghp
Traceback (most recent call last):
File "C:\Anaconda\envs\py33\Scripts\easy_install-script.py", line 5, in <module>
sys.exit(main())
File "C:\Anaconda\envs\py33\lib\site-packages\setuptools\command\easy_install.py", line 1986, in main
with_ei_usage(lambda:
File "C:\Anaconda\envs\py33\lib\site-packages\setuptools\command\easy_install.py", line 1967, in with_ei_usage
return f()
File "C:\Anaconda\envs\py33\lib\site-packages\setuptools\command\easy_install.py", line 1990, in <lambda>
distclass=DistributionWithoutHelpCommands, **kw
File "C:\Anaconda\envs\py33\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Anaconda\envs\py33\lib\distutils\dist.py", line 930, in …Run Code Online (Sandbox Code Playgroud) 有什么更Pythonic的方法可以做到这一点?
min_odds = np.arange( 1.05, 2.0, 0.01 )
min_odds = min_odds.reshape( len( min_ods ), -1 )
Run Code Online (Sandbox Code Playgroud)
该代码创建一个形状为(95,)的ndarray并将其转换为形状(95,1)。
另外,为什么numpy有时会创建大小为(95,)的数组,最后一个空白为空白?
我想在 Numba 函数中创建一个日期数组,以 nopython 模式运行。
我看不到日期类型,所以我正在尝试 NPDatetime。
我尝试的代码是:
import numba as nb
import numpy as np
@nb.jit(nopython=True)
def xxx():
return np.empty(10, dtype=nb.types.NPDatetime('D'))
print(xxx())
Run Code Online (Sandbox Code Playgroud)
但是,代码返回此错误:
Unknown attribute 'NPDatetime' of type Module(<module 'numba.types' from '/home/xxx/anaconda3/lib/python3.6/site-packages/numba/types/__init__.py'>)
Run Code Online (Sandbox Code Playgroud)
我的 numba 版本是 0.39.0
我在导入pandas时遇到问题,但是不知道修复它的错误信息有多少.
如何让我的熊猫安装再次运行?
我正在使用Anacondas.并在py33环境中运行它,而不是默认的2.x环境.
我试过pip install --upgrade pandas.但它没有帮助.
In [17]: import pandas as pd
DLL load failed: The specified module could not be found.
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-17-af55e7023913> in <module>()
----> 1 import pandas as pd
C:\Anaconda\envs\py33\lib\site-packages\pandas\__init__.py in <module>()
4
5 try:
----> 6 from . import hashtable, tslib, lib
7 except Exception: # pragma: no cover
8 import sys
ImportError: DLL load failed: The specified module could not be found.
In [18]:
Run Code Online (Sandbox Code Playgroud) 我有两个 numpy 数组
X.shape = (100, 10)
Y.shape = (100, 10)
Run Code Online (Sandbox Code Playgroud)
我想找到 X 和 Y 列之间的皮尔逊相关性
IE
from scipy.stats.stats import pearsonr
def corr( X, Y ):
return np.array([ pearsonr( x, y )[0] for x,y in zip( X.T, Y.T ) ] )
corr( X, Y ).shape = (10, )
Run Code Online (Sandbox Code Playgroud)
有这个功能吗?到目前为止,我能找到的所有函数都可以计算相关矩阵。Matlab 中有一个成对相关函数,所以我很确定一定有人为 Python 编写了一个。
我不喜欢上面的示例函数的原因是它看起来很慢。
如果我使用urllib加载此URL(https://www.fundingcircle.com/my-account/sell-my-loans/),我会收到400状态错误.
例如,以下内容返回400错误
>>> import urllib
>>> f = urllib.urlopen("https://www.fundingcircle.com/my-account/sell-my-loans/")
>>> print f.read()
Run Code Online (Sandbox Code Playgroud)
但是,如果我将网址复制并粘贴到我的浏览器中,我会看到一个网页,其中包含我想要查看的信息.
我尝试过尝试,除了,然后阅读错误.但返回的数据只是告诉我该页面不存在.例如
import urllib
try:
f = urllib.urlopen("https://www.fundingcircle.com/my-account/sell-my-loans/")
except Exception as e:
eString = e.read()
print eString
Run Code Online (Sandbox Code Playgroud)
为什么Python不能加载页面?
有没有更Pythonic/更好的方法来做到这一点?
def square_take( m, idx ):
""" Take a square submatrix of m """
return m.take( idx, axis=0 ).take( idx, axis=1 )
m = np.eye(3)
idx = np.array( [0, 1] )
print square_take( m, idx )
Run Code Online (Sandbox Code Playgroud)
它看起来很难看,我想知道是否可以使用单个命令来完成它。
更新:
我将这两种解决方案与原来的方法进行了比较:
def square_take( m, idx ):
""" Take a square submatrix of m """
return m.take( idx, axis=0 ).take( idx, axis=1 )
def square_take2( m, idx ):
""" Take a square submatrix of m """
return m[ np.ix_( idx, idx ) ] …Run Code Online (Sandbox Code Playgroud) 我正在使用cron作业下载网页并使用Google App Engine进行保存.
5秒后,我得到了Dealine超出错误.
我该如何避免错误.搜索这个网站,我可以延长urlfetch的时间限制.但它不是urlfetch导致问题.事实是我无法执行任务超过5秒.
例如,我尝试过这个修复程序,但它只在我自己运行页面时才有效,而不是通过cron作业: