所以我正在使用boto来调用我的lambda函数并测试我的后端.我想异步调用它们.我注意到"invoke_async"已弃用,不应使用.相反,您应该使用InvocationType为"Event"的"invoke"来异步执行该函数.
我似乎无法弄清楚如何在函数返回时从函数中获取响应.我尝试过以下方法:
payload3=b"""{
"latitude": 39.5732160891,
"longitude": -119.672918997,
"radius": 100
}"""
client = boto3.client('lambda')
for x in range (0, 5):
response = client.invoke(
FunctionName="loadSpotsAroundPoint",
InvocationType='Event',
Payload=payload3
)
time.sleep(15)
print(json.loads(response['Payload'].read()))
print("\n")
Run Code Online (Sandbox Code Playgroud)
即使我告诉代码睡眠15秒,当我尝试打印它时,响应变量仍然是空的.如果我将invokation Invokation类型更改为"RequestResponse",它一切正常并且响应变量打印,但这是同步的.我错过了一些容易的事吗?当async invokation返回时,我如何执行一些代码,例如打印出结果?
谢谢.
首先,我知道注释不是有效的 json。也就是说,出于某种原因,我必须处理的这个 .json 文件在行首和行尾有注释。
我如何在 python 中处理这个问题并基本上加载 .json 文件但忽略注释以便我可以处理它?我目前正在做以下事情:
with open('/home/sam/Lean/Launcher/bin/Debug/config.json', 'r') as f:
config_data=json.load(f)
Run Code Online (Sandbox Code Playgroud)
但是这会在 json.load(f) 命令处崩溃,因为文件 f 中有注释。
我认为这将是一个常见问题,但我找不到太多在线 RE 如何在 python 中处理它。有人建议 commentjson 但这使我的脚本崩溃说
ImportError: cannot import name 'dump'
Run Code Online (Sandbox Code Playgroud)
当我导入commentjson时
想法?
编辑:这是我必须处理的 json 文件的片段。
{
// this configuration file works by first loading all top-level
// configuration items and then will load the specified environment
// on top, this provides a layering affect. environment names can be
// anything, and just require definition in this file. There's …Run Code Online (Sandbox Code Playgroud) 我在短时间内从使用许多不同线程的 python 代码(使用 pymysql)对 mysql 表进行了大量插入。
每个线程,其中有很多,最终可能会或可能不会将数据推送到 MySql 表。
这是导致问题的代码块(可以为每个正在运行的线程调用):
sql = ("INSERT INTO LOCATIONS (location_id, place_name) VALUES (%s, %s)")
cursor = self.connection.cursor()
cursor.execute(sql, (location_id, place_name))
cursor.close()
Run Code Online (Sandbox Code Playgroud)
特别是这一行:
cursor.execute(sql, (location_id, place_name))
Run Code Online (Sandbox Code Playgroud)
这会导致此错误:
pymysql.err.InterfaceError: (0, '')
Run Code Online (Sandbox Code Playgroud)
另请注意,我在上述块所在的类的 init 中定义了 self.connection。因此所有线程共享一个 self.connection 对象,但获得自己的游标对象。
该错误似乎是随机发生的,并且仅在对 mysql 表进行了多次插入后才开始出现(我认为)。它不一致意味着每次尝试插入 mysql 时都不会发生这种情况。
我在谷歌上搜索了这个特定的错误,似乎它可能是由于在运行查询之前关闭了游标。但我相信很明显我在执行查询后关闭了游标。
现在我认为这是因为:
想法?
假设我将以下数据帧存储为称为坐标的变量,其中前几行如下所示:
business_lat business_lng business_rating
0 19.111841 72.910729 5.
1 19.111342 72.908387 5.
2 19.111342 72.908387 4.
3 19.137815 72.914085 5.
4 19.119677 72.905081 2.
5 19.119677 72.905081 2.
. . .
. . .
. . .
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,此数据是地理空间数据(具有纬度和经度),并且每行都有一个附加值business_ rating,它对应于该行中经纬度处的企业评级。我想对数据进行聚类,其中附近且具有相似评级的企业被分配到同一个集群中。本质上,我需要一个地理空间集群,并附加要求集群必须考虑评级列。
我在网上查看过,但找不到太多解决此问题的方法:只有严格的地理空间聚类(只有要聚类的特征是 latlng)或非空间聚类。
我在下面运行了一个简单的 DBSCAN,但是当我绘制聚类结果时,它似乎没有正确执行我想要的操作。
from sklearn.cluster import DBSCAN
import numpy as np
db = DBSCAN(eps=2/6371., min_samples=5, algorithm='ball_tree', metric='haversine').fit(np.radians(coordinates))
Run Code Online (Sandbox Code Playgroud)
尝试调整 DBSCAN 的参数、对数据进行一些额外的处理或同时使用不同的方法是否会更好?
我正在使用Google Cloud在云上训练神经网络,如下例所示:
首先,我将以下内容设置为环境变量:
PROJECT_ID=$(gcloud config list project --format "value(core.project)")
BUCKET_NAME=${PROJECT_ID}-mlengine
Run Code Online (Sandbox Code Playgroud)
然后,我使用以下命令将我的培训和评估数据(名称为eval_set.csv和train_set.csv的csv)上传到Google云存储:
gsutil cp -r data gs://$BUCKET_NAME
Run Code Online (Sandbox Code Playgroud)
然后我验证了这两个csv文件位于我的Google Cloud存储的polar-terminal-160506-mlengine/data目录中.
然后,我做了以下环境变量分配
# Assign appropriate values.
PROJECT=$(gcloud config list project --format "value(core.project)")
JOB_ID="flowers_${USER}_$(date +%Y%m%d_%H%M%S)"
GCS_PATH="${BUCKET}/${USER}/${JOB_ID}"
DICT_FILE=gs://cloud-ml-data/img/flower_photos/dict.txt
Run Code Online (Sandbox Code Playgroud)
在尝试预处理我的评估数据之前:
# Preprocess the eval set.
python trainer/preprocess.py \
--input_dict "$DICT_FILE" \
--input_path "gs://cloud-ml-data/img/flower_photos/eval_set.csv" \
--output_path "${GCS_PATH}/preproc/eval" \
--cloud
Run Code Online (Sandbox Code Playgroud)
遗憾的是,这会运行一段时间然后崩溃输出以下错误:
ValueError: Unable to get the Filesystem for path gs://polar-terminal-160506-mlengine/data/eval_set.csv
Run Code Online (Sandbox Code Playgroud)
这似乎不可能,因为我通过我的Google云端存储控制台确认eval_set.csv存储在此位置.这可能是权限问题还是我没有看到的问题?
编辑:
我发现此运行时错误的原因来自trainer.preprocess.py文件中的某一行.这一行是这样的:
read_input_source = beam.io.ReadFromText(
opt.input_path, strip_trailing_newlines=True)
Run Code Online (Sandbox Code Playgroud)
似乎是一个非常好的线索,但我仍然不确定发生了什么.当我google"beam.io.ReadFromText ValueError:无法获取路径的文件系统"时,根本没有任何相关性,这有点奇怪.思考?
我正在尝试使用 scikit learn 拟合一个简单的机器学习模型。在这条线上:
clf.fit(features, labels)
Run Code Online (Sandbox Code Playgroud)
我得到一个熟悉的错误:
Input contains NaN, infinity or a value too large for dtype('float64').
Run Code Online (Sandbox Code Playgroud)
每当我之前遇到过这种情况时,我的数据中就有 NaN 值。我已经确认数据中没有 NaN。.fit() 方法的两个输入(特征和标签)是 np 数组,但它们是从 Pandas 数据帧生成的。在拉出我打印的 NaN 值之前:
print(features_df[features_df.isnull().any(axis=1)])
print(labels_df[labels_df.isnull().any(axis=1)])
Run Code Online (Sandbox Code Playgroud)
这打印了空数据帧,所以我知道其中没有包含 NaN 值的行。我还在转换后检查了 numpy 数组的 NaN 值,甚至使用 np sum() 方法成功地对它们求和,因此传递到 fit 的特征或标签 np 数组中没有 NaN 值。
这意味着必须有无穷大的值或非常大的值,我觉得这两者都令人难以置信。有什么方法可以打印数据帧或 np 数组中的任何值:
are NaN, infinity or a value too large for dtype('float64')?
Run Code Online (Sandbox Code Playgroud)
我需要特别指出它们,因为我无法用肉眼找到它们并且没有 NaN 值。
假设我在Google云端存储上有一些存储桶/子目录,此存储桶的地址是:
gs://test-monkeys-example/training_data/cats
Run Code Online (Sandbox Code Playgroud)
在这个猫子目录中,我有一堆猫的图像,所有这些都是jpgs.我如何在python循环中通过cats子目录并打印出其中所有文件的名称?
就像是:
for x in directory('gs://test-monkeys-example/training_data/cats'):
print(x)
Run Code Online (Sandbox Code Playgroud)
显然目录('gs:// test-monkeys-example/training_data/cats')不是如何做到的,只是伪问题 - 我该怎么做?!
python google-cloud-storage google-cloud-platform google-cloud-datalab
这看起来应该很容易,但我在谷歌搜索时找不到答案。假设我有一些类型为 pandas.Period 的变量 k,其值为:
Period('2018-11', 'M')
Run Code Online (Sandbox Code Playgroud)
如何向该变量添加 n 个月。例如,如果 n 是 3 我希望 k 是
Period('2019-02', 'M')
Run Code Online (Sandbox Code Playgroud)
我已经尝试过以下方法:
k.month = k.month + 12
Run Code Online (Sandbox Code Playgroud)
但这失败了:
AttributeError: attribute 'month' of 'pandas._libs.tslibs.period._Period' objects is not writable
Run Code Online (Sandbox Code Playgroud) 我有一个 tweepy tweet 对象,我可以看到和打印它favorite_count,retweet_count但是like_count当我打印对象的成员变量时我没有看到。了解收藏的数量似乎与处理推文的典型用例无关,因为在 Twitter 上喜欢某物比喜欢它更常见。
我能够做这样的事情:
api = tweepy.API(auth)
tweet = api.get_status(tweet_id)
print(tweet.retweet_count)
print(tweet.favorite_count)
Run Code Online (Sandbox Code Playgroud)
但做
print(tweet.like_count)
Run Code Online (Sandbox Code Playgroud)
不起作用,我看不到任何替代方案。此外,当我用谷歌搜索诸如“tweepy get like count of a tweet”之类的东西时,没有任何相关的弹出窗口看起来很奇怪。我怎样才能做到这一点?
我的一般问题是我有一个数据框,其中的列与要素值相对应。数据框中还有一个日期列。每个功能列可能缺少NaN值。我想用诸如“ fill_mean”或“ fill zero”的填充逻辑填充一列。
但是我不想只将填充逻辑应用于整个列,因为如果较早的值之一是NaN,则我不希望此特定NaN的平均值被后来的平均值所污染。该模型应该没有任何知识。从本质上讲,这是不向模型泄漏有关未来信息的普遍问题,尤其是在尝试填充我的时间序列时。
无论如何,我已经将问题简化为几行代码。这是我对上述一般问题的简化尝试:
#assume ts_values is a time series where the first value in the list is the oldest value and the last value in the list is the most recent.
ts_values = [17.0, np.NaN, 12.0, np.NaN, 18.0]
nan_inds = np.argwhere(np.isnan(ts_values))
for nan_ind in nan_inds:
nan_ind_value = nan_ind[0]
ts_values[nan_ind_value] = np.mean(ts_values[0:nan_ind_value])
Run Code Online (Sandbox Code Playgroud)
上面脚本的输出是:
[17.0, 17.0, 12.0, 15.333333333333334, 18.0]
Run Code Online (Sandbox Code Playgroud)
这正是我所期望的。
我唯一的问题是,相对于数据集中NaN的数量,它将是线性时间。有没有办法在常量或日志时间内执行此操作,而我不会遍历nan索引值。
python ×10
pandas ×3
numpy ×2
aws-lambda ×1
boto ×1
csv ×1
data-science ×1
dbscan ×1
geospatial ×1
json ×1
mysql ×1
nan ×1
numpy-dtype ×1
period ×1
pymysql ×1
scikit-learn ×1
terminal ×1
time-series ×1
tweepy ×1
twitter ×1