我正在读取一个 csv 文件到数据框
datafram = spark.read.csv(fileName, header=True)
Run Code Online (Sandbox Code Playgroud)
但是dataframe中的数据类型是String,我想把数据类型改成float。有没有办法有效地做到这一点?
我有一个 Spark 数据框,其中有一列类型为 spark.mllib.linalg.SparseVector:
1)如何将其写入csv文件?
2)如何打印所有向量?
I used cross validation to train a linear regression model using the following code:
from pyspark.ml.evaluation import RegressionEvaluator
lr = LinearRegression(maxIter=maxIteration)
modelEvaluator=RegressionEvaluator()
pipeline = Pipeline(stages=[lr])
paramGrid = ParamGridBuilder().addGrid(lr.regParam, [0.1, 0.01]).addGrid(lr.elasticNetParam, [0, 1]).build()
crossval = CrossValidator(estimator=pipeline,
estimatorParamMaps=paramGrid,
evaluator=modelEvaluator,
numFolds=3)
cvModel = crossval.fit(training)
Run Code Online (Sandbox Code Playgroud)
now I want to draw the roc curve, I used the following code but I get this error:
'LinearRegressionTrainingSummary' object has no attribute 'areaUnderROC'
trainingSummary = cvModel.bestModel.stages[-1].summary
trainingSummary.roc.show()
print("areaUnderROC: " + str(trainingSummary.areaUnderROC))
Run Code Online (Sandbox Code Playgroud)
I also want to check the objectiveHistory …
machine-learning cross-validation apache-spark pyspark apache-spark-ml
我有一个字符串
abababa:nsndnfnng.leleelld_kdjdh
Run Code Online (Sandbox Code Playgroud)
我想将它拆分为":"和".",以便我得到如下列表:
{abababa, nsndnfnng, eleelld_kdjdh}
Run Code Online (Sandbox Code Playgroud)
如何调用split()一次呢?
假设我创建了下图。我的问题是如何可视化它?
# Create a Vertex DataFrame with unique ID column "id"
v = sqlContext.createDataFrame([
("a", "Alice", 34),
("b", "Bob", 36),
("c", "Charlie", 30),
], ["id", "name", "age"])
# Create an Edge DataFrame with "src" and "dst" columns
e = sqlContext.createDataFrame([
("a", "b", "friend"),
("b", "c", "follow"),
("c", "b", "follow"),
], ["src", "dst", "relationship"])
# Create a GraphFrame
from graphframes import *
g = GraphFrame(v, e)
Run Code Online (Sandbox Code Playgroud) 我有一组样本,S,我想找到它的PDF.问题是,当我使用ksdensity时,我得到的值大于1!
[f,xi] = ksdensity(S)
Run Code Online (Sandbox Code Playgroud)
在数组f中,大多数值都大于1!你能告诉我问题是什么吗?谢谢你的帮助.
例如:
S=normrnd(0.3035, 0.0314,1,1000);
ksdensity(S)
Run Code Online (Sandbox Code Playgroud) 假设table1有3个属性:first_name,last_name,和country.例如,使用以下元组:
John White Canada
John Smith France
Mary Smith Canada
Ben Smith Canada
Mary Black USA
Run Code Online (Sandbox Code Playgroud)
我正在寻找名字为"John"或姓氏"Smith"或国家"US"的人:
SELECT *
FROM table1
WHERE
first_name='John' or
last_name='smith' or
country='US'
Run Code Online (Sandbox Code Playgroud)
我希望按以下顺序得到结果:首先是姓名为John的人,然后是名字为smith的人,最后是带国家的人.
我知道我可以编写以下3个不同的查询,然后按照我想要的顺序使用它们的输出:
SELECT *
FROM table1
WHERE first_name='John'
SELECT *
FROM table1
WHERE last_name='smith'
SELECT *
FROM table1
WHERE country='US'
Run Code Online (Sandbox Code Playgroud)
我正在寻找更好的方法.
问题1:如果我使用上述3个查询并找到他们的联盟,订单将会改变,对吧?如果是,我如何追加结果?
问题2:有更好的方法吗?
我有以下代码,我想知道是否可以将其转换为单行循环?
for a,b in myList :
sth = calcSth(a, b)
if sth > 60 :
return True
return False
Run Code Online (Sandbox Code Playgroud)
另一个问题是:单行循环和块编码循环的性能是否有所不同?
我有一个数据框,例如如下:
import pandas as pd
my_df = pd.DataFrame({'col1':['A', 'B', 'C', 'A', 'A', 'B'],
'col2':['foo bar', 'bar', 'something foo', 'foo', 'bar', 'foo']})
Run Code Online (Sandbox Code Playgroud)
我想为其中一列中出现的每个单词生成列,例如col2,并计算该行中出现的次数.
col1 col2 foo bar something
0 A foo bar 1 1 0
1 B bar 0 1 0
2 C something foo 1 0 1
3 A foo 1 0 0
4 A bar 0 1 0
5 B foo 1 0 0
Run Code Online (Sandbox Code Playgroud)
我的数据框比这个例子要大得多.每列可以有多个单词.
如何在 pyspark 数据框中找到两列之间的余弦相似度?
假设我有一个火花数据框
|a |b |
+--+--|
|1 |4 |
|2 |5 |
|3 |6 |
+--+--+
Run Code Online (Sandbox Code Playgroud)
现在我想知道a列中的值和b列中的值之间的余弦相似度是多少,即,
cosine_similarity([1, 2, 3], [4, 5, 6])
Run Code Online (Sandbox Code Playgroud) python ×6
apache-spark ×5
pyspark ×5
dataframe ×1
graph ×1
graphframes ×1
java ×1
matlab ×1
mysql ×1
pandas ×1
postgresql ×1
sql ×1
where ×1
where-clause ×1