我是 SQL 新手,一直在寻找一种在 ANSI SQL 中设置变量的方法。我有这个:
select * from table1
where first_date > '2014-01-01'
and where second_date = '2014-01-01'
and where third_date < '2014-01-01'
Run Code Online (Sandbox Code Playgroud)
但我希望有这样的事情:
set x = '2010-12-31'
select * from table1
where first_date > x
and where second_date = x
and where third_date < x
Run Code Online (Sandbox Code Playgroud)
我读过有关存储过程的内容,但对于看似简单的事情来说,这似乎有点矫枉过正。我正在 Netezza 上运行,但我想要一个也可以在其他数据库上运行的通用解决方案。
我正在试图抓住nature.com对期刊文章进行一些分析.当我执行以下操作时:
import requests
from bs4 import BeautifulSoup
import re
query = "http://www.nature.com/search?journal=nature&order=date_desc"
for page in range (1, 10):
req = requests.get(query + "&page=" + str(page))
soup = BeautifulSoup(req.text)
cards = soup.findAll("li", "mb20 card cleared")
matches = re.findall('mb20 card cleared', req.text)
print(len(cards), len(matches))
Run Code Online (Sandbox Code Playgroud)
我希望Beautifulsoup打印"25"(搜索结果的数量)10次(每页一个),但事实并非如此.相反,它打印:
14, 25
12, 25
25, 25
15, 25
15, 25
17, 25
17, 25
15, 25
14, 25
Run Code Online (Sandbox Code Playgroud)
查看html源代码显示每页应该返回25个结果但是Beautifulsoup似乎在这里混淆了,我无法弄清楚原因.
更新1如果重要,我使用Anaconda Python 2.7.10和bs4版本4.3.1在Mac OSX Mavericks上运行
更新2我添加了一个正则表达式,以显示req.text确实包含我正在寻找的东西,但beautifulsoup没有找到它
更新3当我多次运行这个简单的脚本时,我有时会得到"分段错误:11".不知道为什么
我正在将 CSV 文件读入 pandas:
df = pd.read_csv('file.csv')
Run Code Online (Sandbox Code Playgroud)
但是,我注意到列顺序没有保留。我在文档中找不到任何解释如何在读取 CSV 文件时保持列顺序的内容。
我有一些艺术家姓名,data['artist']我想通过以下方式转换为分类列:
x = data['artist'].astype('category').cat.codes
x.dtype
Run Code Online (Sandbox Code Playgroud)
返回:
dtype('int32')
Run Code Online (Sandbox Code Playgroud)
我得到负数,这表明存在某种溢出情况。因此,我想使用np.int64它,但我找不到有关如何完成此操作的文档。
x = data['artist'].astype('category').cat.codes.astype(np.int64)
x.dtype
Run Code Online (Sandbox Code Playgroud)
给予
dtype('int64')
Run Code Online (Sandbox Code Playgroud)
但很明显 int32 已转换为 int64,因此负值仍然存在
x = data['artist'].astype('category').cat.codes.astype(np.int64)
x.min()
-1
Run Code Online (Sandbox Code Playgroud) 我有一大组 sklearn 管道,我想与 Dask 并行构建。这是一个简单但幼稚的顺序方法:
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, Y_train, Y_test = train_test_split(iris.data, iris.target, test_size=0.2)
pipe_nb = Pipeline([('clf', MultinomialNB())])
pipe_lr = Pipeline([('clf', LogisticRegression())])
pipe_rf = Pipeline([('clf', RandomForestClassifier())])
pipelines = [pipe_nb, pipe_lr, pipe_rf] # In reality, this would include many more different types of models with varying but specific parameters
for pl in pipelines:
pl.fit(X_train, Y_train) …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来检查 numpy 数组是否为np.float64或np.float32。这适用于np.float64:
a = np.random.rand(10)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # No exception is raised for np.float64
Run Code Online (Sandbox Code Playgroud)
但失败了np.float32:
a = np.random.rand(10).astype(np.float32)
if not issubclass(a.dtype.type, np.float):
raise "Wrong type" # An exception is raised!
Run Code Online (Sandbox Code Playgroud) 我有两个 NumPy 数组:
import numpy as np
m = 3
x = np.array([1, 0, 0, np.inf, 0, 0, 1, 1, 2, np.inf, np.inf, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.arange(x.shape[0]-m+1)
Run Code Online (Sandbox Code Playgroud)
假设有np.infin 的地方x,该索引位置被称为i。对于每个i,我想设置y[i-m+1:i+m] = np.inf. 所以,更换后,y应该是这样的:
array([0, np.inf, np.inf, np.inf, np.inf, np.inf, 6, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, 13, 14, 15, 16, 17])
Run Code Online (Sandbox Code Playgroud)
当 的值m增加或减少时,这也应该起作用。
假设我有一个 NumPy 数组:
x = np.array([2, 3, 4, 0, 0, 1, 1, 4, 6, 5, 8, 9, 9, 4, 2, 0, 3])
Run Code Online (Sandbox Code Playgroud)
对于 中的所有值x >= 2,我需要找到连续值x >=2(即,一个大于或等于 2 的单个值的运行不计算在内)的开始/停止索引。然后,我对x >= 3, x >=4, ...,重复此操作x >= x.max()。输出应该是一个三列的 NumPy 数组(第一列是最小值,第二列是包含开始索引,第三列是停止索引),看起来像:
[[2, 0, 2],
[2, 7, 14],
[3, 1, 2],
[3, 7, 13],
[4, 7, 13],
[5, 8, 12],
[6, 10, 12],
[8, 10, 12],
[9, 11, 12]
]
Run Code Online (Sandbox Code Playgroud)
天真地,我可以查看每个唯一值,然后搜索开始/停止索引。但是,这需要对 …
我有一个很长的包含1_000_000_000元素的 NumPy 数组,我想50在数组上滑动一个元素窗口,并询问窗口内的所有元素是否都是有限的。如果元素窗口内的所有元素50都是有限的,则返回True(对于该窗口),否则,如果50元素窗口内的一个或多个元素不是有限的,则返回False(对于该窗口)。继续此评估,直到评估完所有窗口。一个很好的方法是:
import numpy as np
def rolling_window(a, window):
a = np.asarray(a)
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
if __name__ == "__main__":
a = np.random.rand(100_000_000) # This is 10x shorter than my real data
w = 50
idx = np.random.randint(0, len(a), size=len(a)//10) # Simulate having np.nan in my array
a[idx] = np.nan
print(np.all(rolling_window(np.isfinite(a), w), …Run Code Online (Sandbox Code Playgroud) 我有以下代码
import asyncio
loop = asyncio.get_event_loop()
async def while_loop():
n = 0
while True:
print(f"{n}")
await asyncio.sleep(2)
n = n+1
async def some_func():
await asyncio.sleep(5)
print("Some Func")
future = loop.create_task(while_loop())
loop.run_until_complete(some_func())
Run Code Online (Sandbox Code Playgroud)
我希望while_loop函数永远运行,但它似乎只是作为调用的结果执行,run_until_complete并且一旦some_func完成执行就停止打印while循环.我看到的输出是:
0
1
2
Some Func
Run Code Online (Sandbox Code Playgroud)
我预计数字会在some_func完成后继续打印.
0
1
2
Some Func
3
4
5
6
.
.
.
Run Code Online (Sandbox Code Playgroud)
获得更多数字的唯一方法是some_func再次打电话.
python ×9
numpy ×4
pandas ×2
ansi-sql ×1
arrays ×1
dask ×1
netezza ×1
performance ×1
python-3.x ×1
scikit-learn ×1
sql ×1
stumpy ×1