小编dig*_*vee的帖子

GCS - 直接从Google云端存储中读取文本文件到python中

我现在觉得有点蠢.我一直在阅读大量文档和stackoverflow问题,但我无法做到正确.

我在Google云端存储上有一个文件.它在一个桶'test_bucket'中.在这个存储桶中有一个文件夹'temp_files_folder',它包含两个文件,一个名为'test.txt'的.txt文件和一个名为'test.csv'的.csv文件.这两个文件只是因为我尝试使用两个文件,但结果是相同的.

文件中的内容是

hej
san
Run Code Online (Sandbox Code Playgroud)

我希望将它读入python,就像我在本地用的那样

textfile = open("/file_path/test.txt", 'r')
times = textfile.read().splitlines()
textfile.close()
print(times)
Run Code Online (Sandbox Code Playgroud)

这使

['hej', 'san']
Run Code Online (Sandbox Code Playgroud)

我试过用

from google.cloud import storage

client = storage.Client()

bucket = client.get_bucket('test_bucket')

blob = bucket.get_blob('temp_files_folder/test.txt')

print(blob.download_as_string)
Run Code Online (Sandbox Code Playgroud)

但它给出了输出

<bound method Blob.download_as_string of <Blob: test_bucket, temp_files_folder/test.txt>>
Run Code Online (Sandbox Code Playgroud)

如何获取文件中的实际字符串?

python google-cloud-storage

11
推荐指数
3
解决办法
1万
查看次数

尝试 Apache Beams 示例代码时出现 Oauth2client 错误

我正在尝试运行 Apache Beam Python 示例(例如https://github.com/apache/beam/blob/master/sdks/python/apache_beam/examples/complete/game/user_score.py)。当我运行脚本时,我得到

(py27) XXX-MBP:my_path$ python user_score.py --output /my_path/user_score_output
No handlers could be found for logger "oauth2client.contrib.multistore_file"
INFO:root:Missing pipeline option (runner). Executing pipeline using the default runner: DirectRunner.
/my_path_2/miniconda3/envs/py27/lib/python2.7/site-packages/apache_beam/io/gcp/gcsio.py:121: DeprecationWarning: object() takes no parameters
  super(GcsIO, cls).__new__(cls, storage_client))
INFO:root:Starting the size estimation of the input
INFO:oauth2client.transport:Attempting refresh to obtain initial access_token
INFO:oauth2client.client:Refreshing access_token
INFO:root:Finished the size estimation of the input at 1 files. Estimation took 0.453105211258 seconds
/my_path_2/miniconda3/envs/py27/lib/python2.7/site-packages/apache_beam/coders/typecoders.py:135: UserWarning: Using fallback coder for typehint: Any.
  warnings.warn('Using fallback …
Run Code Online (Sandbox Code Playgroud)

apache oauth-2.0 google-oauth

6
推荐指数
0
解决办法
460
查看次数

XGBoost/CatBoost 中具有大量类别的分类变量

我有一个关于随机森林的问题。想象一下,我有关于用户与项目交互的数据。项目数量很大,大约 10 000 个。我的随机森林输出应该是用户可能与之交互的项目(如推荐系统)。对于任何用户,我想使用一个描述用户过去交互过的项目的功能。然而,将分类产品特征映射为 one-hot 编码似乎内存效率非常低,因为用户最多与不超过几百个项目进行交互,有时甚至只有 5 个。

当输入特征之一是具有约 10 000 个可能值的分类变量并且输出是具有约 10 000 个可能值的分类变量时,您将如何构建随机森林?我应该使用具有分类特征的 CatBoost 吗?或者我应该使用 one-hot 编码,如果是这样,您认为 XGBoost 还是 CatBoost 更好?

machine-learning random-forest categorical-data xgboost catboost

5
推荐指数
1
解决办法
5467
查看次数

在R中存储多维索引和相应值的最佳方法

我有一个三维索引列表;

> head(TW_idx, n = 4)
[[1]]
[1]   1   1 135

[[2]]
[1]  1  2 96

[[3]]
[1]   1   3 120

[[4]]
[1]   1   1 135
Run Code Online (Sandbox Code Playgroud)

每个索引对应一个值100.但是,一个索引可以在TW_idx中出现多次(在列表1和列表4中),并且对于索引的每次出现,该值线性增加(因此,如果一个索引存在3次,该指数的价值是300).我想找到一种方法来存储索引及其相应的值.

我的想法是,我们创建一个看起来很像的数据框

> df
 idx    value
1,1,135  200
1,2,96   100
1,3,120  100
Run Code Online (Sandbox Code Playgroud)

对于上面的4个值,但我不知道如何创建它.更好的是,如果我能得到一个输出,其中索引只是没有重复的相同列表(即TW_idx < - 唯一(TW_idx)),并且值存储在相同长度的列表/数字列表中,其中每个元素描述新的(没有重复的)TW_idx列表中相应元素的值.但是如果获得数据帧更简单,我会很高兴.

提前致谢

r list dataframe

1
推荐指数
1
解决办法
33
查看次数

R中列表中所有矩阵的元素乘法

我有一个矩阵列表。我需要将它们相乘。通常,如果我们有矩阵A和B,并且需要在R中进行矩阵乘法,则使用

A*B
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我似乎无法使lapply起作用。如果我的矩阵列表称为l1,而我使用lapply(l1,*),则会出现错误;如果我使用lapply(l1,prod),它将给出每个矩阵内所有元素的乘积。

例:

> l1
[[1]]
     [,1] [,2]
[1,]    2    5
[2,]    3    7

[[2]]
     [,1] [,2]
[1,]    3    7
[2,]    5    9

[[3]]
     [,1] [,2]
[1,]    5    8
[2,]    1    2
Run Code Online (Sandbox Code Playgroud)

所需的输出:

 l1
      [,1] [,2]
[,1]   30   280
[,2]   15   126
Run Code Online (Sandbox Code Playgroud)

r list matrix matrix-multiplication

0
推荐指数
1
解决办法
419
查看次数