小编Odi*_*seo的帖子

Spark 2.3.0读取带有标题选项的文本文件不起作用

下面的代码正在运行,并从文本文件创建Spark数据框。但是,我正在尝试使用header选项将第一列用作标题,由于某种原因,它似乎没有发生。我不明白为什么!这一定是愚蠢的,但我无法解决。

>>>from pyspark.sql import SparkSession
>>>spark = SparkSession.builder.master("local").appName("Word Count")\
    .config("spark.some.config.option", "some-value")\
    .getOrCreate()
>>>df = spark.read.option("header", "true")\
    .option("delimiter", ",")\
    .option("inferSchema", "true")\
    .text("StockData/ETFs/aadr.us.txt")
>>>df.take(3)
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

[行(value = u'Date,Open,High,Low,Close,Volume,OpenInt'),行(value = u'2010-07-21,24.333,24.333,23.946,23.946,43321,0'),行(值= u'2010-07-22,24.644,24.644,24.362,24.487,18031,0')]

>>>df.columns
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

['值']

header text-files python-2.7 apache-spark spark-dataframe

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

在SkLearn中打印估算器名称

在Sklearn中,是否可以打印出估算器的类名?

我尝试使用name属性,但是不起作用。

from sklearn.linear_model import LogisticRegression  

def print_estimator_name(estimator):
    print(estimator.__name__)

#Expected Outcome:
print_estimator_name(LogisticRegression())
Run Code Online (Sandbox Code Playgroud)

我希望这会打印出上面的分类器名称

python model scikit-learn

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

Spark VectorAssembler 错误 - PySpark 2.3 - Python

我正在使用 pySpark 2.3.0,并创建了一个非常简单的 Spark 数据框来测试 VectorAssembler 的功能。这是一个更大的数据框的子集,我只选择了几个数字(双数据类型)列:

>>>cols = ['index','host_listings_count','neighbourhood_group_cleansed',\
        'bathrooms','bedrooms','beds','square_feet', 'guests_included',\
        'review_scores_rating']
>>>test = df[cols]
>>>test.take(3)
Run Code Online (Sandbox Code Playgroud)

[行(索引=0,host_listings_count=1,neighborhood_group_cleansed=无,浴室=1.5,卧室=2.0,床位=3.0,square_feet=无,guests_included=1,review_scores_rating=100.0),行(索引=1,host_listings_count=1, neighborhood_group_cleansed=无,浴室=1.5,卧室=2.0,床位=3.0,square_feet=无,guests_included=1,review_scores_rating=100.0),行(索引=2,host_listings_count=1,neighborhood_group_cleansed=None.5,卧室=1 ,床位=3.0,square_feet=无,guests_included=1,review_scores_rating=100.0)]

从上面看来,这个 Spark 数据框没有任何问题。所以我然后创建如下所示的汇编程序并得到显示的错误。可能出了什么问题?

>>>from pyspark.ml.feature import VectorAssembler
>>>assembler = VectorAssembler(inputCols=cols, outputCol="features")
>>>output = assembler.transform(test)
>>>output.take(3)
Run Code Online (Sandbox Code Playgroud)

Py4JJavaError:调用 o279.collectToPython 时出错。:org.apache.spark.SparkException:作业因阶段失败而中止:阶段 5.0 中的任务 0 失败 1 次,最近失败:阶段 5.0 中丢失任务 0.0(TID 10、本地主机、执行程序驱动程序):org.apache.spark .SparkException: 无法在 org.apache.spark.sql.catalyst.expressions.GeneratedClass$GeneratedIterator.processNext(Unknown Source) at org.apache.spark 执行用户定义的函数($anonfun$3: (struct) => vector)。 sql.execution.BufferedRowIterator.hasNext(BufferedRowIterator.java:43) 在 org.apache.spark.sql.execution.WholeStageCodegenExec$$anonfun$8$$anon$1.hasNext(WholeStageCodegenExec.scala:377) 在 org.apache.spark。 sql.execution.SparkPlan$$anonfun$2.apply(SparkPlan.scala:231) 在 org.apache.spark.sql.execution.SparkPlan$$anonfun$2。Thread.run(Thread.java:748) 导致:org.apache.spark.SparkException:要组装的值不能为空。在 org.apache.spark.ml.feature.VectorAssembler$$anonfun$assemble$1.apply(VectorAssembler.scala:160) 在 org.apache.spark.ml.feature.VectorAssembler$$anonfun$assemble$1.apply(VectorAssembler. scala:143) at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33) at scala.collection.mutable.WrappedArray.foreach(WrappedArray.scala:35) at org.apache.spark.ml.feature.VectorAssembler $.assemble(VectorAssembler.scala:143) 在 org.apache.spark.ml.feature.VectorAssembler$$anonfun$3.apply(VectorAssembler.scala:99) …

python apache-spark pyspark spark-dataframe

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

在 Spark 中堆叠 ML 算法

是否有 Spark api 可以在 Spark 中构建堆叠集成,或者应该从头开始构建它们?我还没有\xe2\x80\x99t 在网上找到任何有关此主题的资源

\n

python apache-spark ensemble-learning pyspark apache-spark-mllib

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

在地图上绘制散点 - 从文本中删除经度和纬度

我非常密切地关注这个示例,尝试在地图上绘制散点,这工作得很好: https: //plot.ly/python/scatter-plots-on-maps/

但是,当您将鼠标悬停在每个散点上时,您会注意到文本与纬度和经度一起显示。有没有办法从显示的文本中删除两个坐标?

python plot geolocation pandas plotly

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

将 PySpark 字符串转换为月年格式的日期

我有一个 PySpark 数据框,其中日期列编码为字符串,格式如下:

df.select("issue_d").show()

+--------+
| issue_d|
+--------+
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
|Dec-2015|
Run Code Online (Sandbox Code Playgroud)

我想将其转换为日期列。我知道我可以提取前 3 个字母并映射到一个整数,但这似乎不专业。必须有更好的方法来用一两行代码来转换它。这是我想要得到的输出:

df.select("issue_month").show()

+------------+
| issue_month|
+------------+
|12|
|12|
|12|
|12|
|12|
|12|
|12|
|12|
|12|
Run Code Online (Sandbox Code Playgroud)

python datetime dataframe apache-spark pyspark

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