下面的代码正在运行,并从文本文件创建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)
返回以下内容:
['值']
在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)
我希望这会打印出上面的分类器名称
我正在使用 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) …
是否有 Spark api 可以在 Spark 中构建堆叠集成,或者应该从头开始构建它们?我还没有\xe2\x80\x99t 在网上找到任何有关此主题的资源
\npython apache-spark ensemble-learning pyspark apache-spark-mllib
我非常密切地关注这个示例,尝试在地图上绘制散点,这工作得很好: https: //plot.ly/python/scatter-plots-on-maps/
但是,当您将鼠标悬停在每个散点上时,您会注意到文本与纬度和经度一起显示。有没有办法从显示的文本中删除两个坐标?
我有一个 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 ×5
apache-spark ×4
pyspark ×3
dataframe ×1
datetime ×1
geolocation ×1
header ×1
model ×1
pandas ×1
plot ×1
plotly ×1
python-2.7 ×1
scikit-learn ×1
text-files ×1