创建html嵌套父js的数据的数据帧通过火花问题,怎么解决

I am trying to run random forest classification by using
but I am having issues with creating right data frame input into pipeline.
Here is sample data:
age,hours_per_week,education,sex,salaryRange
38,40,"hs-grad","male","A"
28,40,"bachelors","female","A"
52,45,"hs-grad","male","B"
31,50,"masters","female","B"
42,40,"bachelors","male","B"
age and hours_per_week are integers while other features including label salaryRange are categorical (String)
Loading this csv file (lets call it sample.csv) can be done by
like this:
val data = sqlContext.csvFile("/home/dusan/sample.csv")
By default all columns are imported as string so we need to change "age" and
"hours_per_week" to Int:
= udf[Int, String]( _.toInt)
val dataFixed = data.withColumn("age", toInt(data("age"))).withColumn("hours_per_week",toInt(data("hours_per_week")))
Just to check how schema looks now:
scala& dataFixed.printSchema
|-- age: integer (nullable = true)
|-- hours_per_week: integer (nullable = true)
|-- education: string (nullable = true)
|-- sex: string (nullable = true)
|-- salaryRange: string (nullable = true)
Then lets set the cross validator and pipeline:
val rf = new RandomForestClassifier()
val pipeline = new Pipeline().setStages(Array(rf))
val cv = new CrossValidator().setNumFolds(10).setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator)
Error shows up when running this line:
val cmModel = cv.fit(dataFixed)
java.lang.IllegalArgumentException: Field "features" does not exist.
It is possible to set label column and feature column in RandomForestClassifier ,however I have 4 columns as predictors (features) not only one.
How I should organize my data frame so it has label and features columns organized correctly?
For your convenience here is full code :
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.ml.classification.RandomForestClassifier
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.tuning.CrossValidator
import org.apache.spark.ml.Pipeline
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.functions._
import org.apache.spark.mllib.linalg.{Vector, Vectors}
object SampleClassification {
def main(args: Array[String]): Unit = {
//set spark context
val conf = new SparkConf().setAppName("Simple Application").setMaster("local");
val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
import com.databricks.spark.csv._
//load data by using databricks "Spark CSV Library"
val data = sqlContext.csvFile("/home/dusan/sample.csv")
//by default all columns are imported as string so we need to change "age" and
"hours_per_week" to Int
= udf[Int, String]( _.toInt)
val dataFixed = data.withColumn("age", toInt(data("age"))).withColumn("hours_per_week",toInt(data("hours_per_week")))
val rf = new RandomForestClassifier()
val pipeline = new Pipeline().setStages(Array(rf))
val cv = new CrossValidator().setNumFolds(10).setEstimator(pipeline).setEvaluator(new BinaryClassificationEvaluator)
// this fails with error
//java.lang.IllegalArgumentException: Field "features" does not exist.
val cmModel = cv.fit(dataFixed)
Thanks for help!
解决方案 You simply need to make sure that you have a "features" column in your dataframe that is of type VectorUDF as show below:
scala& val df2 = dataFixed.withColumnRenamed("age", "features")
df2: org.apache.spark.sql.DataFrame = [features: int, hours_per_week: int, education: string, sex: string, salaryRange: string]
scala& val cmModel = cv.fit(df2)
java.lang.IllegalArgumentException: requirement failed: Column features must be of type org.apache.spark.mllib.linalg.VectorUDT@1eef but was actually IntegerType.
at scala.Predef$.require(Predef.scala:233)
at org.apache.spark.ml.util.SchemaUtils$.checkColumnType(SchemaUtils.scala:37)
at org.apache.spark.ml.PredictorParams$class.validateAndTransformSchema(Predictor.scala:50)
at org.apache.spark.ml.Predictor.validateAndTransformSchema(Predictor.scala:71)
at org.apache.spark.ml.Predictor.transformSchema(Predictor.scala:118)
at org.apache.spark.ml.Pipeline$$anonfun$transformSchema$4.apply(Pipeline.scala:164)
at org.apache.spark.ml.Pipeline$$anonfun$transformSchema$4.apply(Pipeline.scala:164)
at scala.collection.IndexedSeqOptimized$class.foldl(IndexedSeqOptimized.scala:51)
at scala.collection.IndexedSeqOptimized$class.foldLeft(IndexedSeqOptimized.scala:60)
at scala.collection.mutable.ArrayOps$ofRef.foldLeft(ArrayOps.scala:108)
at org.apache.spark.ml.Pipeline.transformSchema(Pipeline.scala:164)
at org.apache.spark.ml.tuning.CrossValidator.transformSchema(CrossValidator.scala:142)
at org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:59)
at org.apache.spark.ml.tuning.CrossValidator.fit(CrossValidator.scala:107)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:67)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:72)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:74)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:76)
Essentially there need to be two fields in your data frame "features" for feature vector and "label" for instance labels. Instance must be of type Double.
To create a "features" fields with Vector type first create a udf as show below:
val toVec4
= udf[Vector, Int, Int, String, String] { (a,b,c,d) =&
val e3 = c match {
case "hs-grad" =& 0
case "bachelors" =& 1
case "masters" =& 2
val e4 = d match {case "male" =& 0 case "female" =& 1}
Vectors.dense(a, b, e3, e4)
Now to also encode the "label" field, create another udf as shown below:
val encodeLabel
= udf[Double, String]( _ match { case "A" =& 0.0 case "B" =& 1.0} )
Now we transform original dataframe using these two udf:
val df = dataFixed.withColumn(
"features",
dataFixed("age"),
dataFixed("hours_per_week"),
dataFixed("education"),
dataFixed("sex")
).withColumn("label", encodeLabel(dataFixed("salaryRange"))).select("features", "label")
Note that there can be extra columns / fields present in the dataframe, but in this case I have selected only features and label:
scala& df.show()
+-------------------+-----+
features|label|
+-------------------+-----+
|[38.0,40.0,0.0,0.0]|
|[28.0,40.0,1.0,1.0]|
|[52.0,45.0,0.0,0.0]|
|[31.0,50.0,2.0,1.0]|
|[42.0,40.0,1.0,0.0]|
+-------------------+-----+
Now its upto you to set correct parameters for your learning algorithm to make it work.
本文地址: &
我试图用运行随机森林分类,但我有与创建正确的数据帧输入到管道的问题。 下面是样本数据: 年龄,hours_per_week,教育,性别,salaryRange38,40,“HS-毕业生”,“男性”,“A”28,40,“单身汉”,“女性”,“A”52,45,“HS-毕业生”,“男性”,“B”31,50,“主人”,“女性”,“B”42,40,“单身汉”,“男性”,“B”
年龄和 hours_per_week 是整数,而其他的功能,包括标签的 salaryRange 是分类(字符串)加载该csv文件(可以称之为sample.csv)可以通过这样的:
VAL数据= sqlContext.csvFile(“/家庭/杜尚/ sample.csv”) 在默认情况下所有列导入为字符串,所以我们需要改变“年龄”和“hours_per_week”来诠释:
VAL toInt = UDF [诠释,字符串](_.toInt)VAL dataFixed = data.withColumn(“时代”,toInt(数据(“时代”)))。withColumn(“hours_per_week”,toInt(数据(“hours_per_week”))) 只是为了检查模式现在的样子: 斯卡拉> dataFixed.printSchema根 |
- 年龄:整数(可为空=真) |
hours_per_week:整数(可为空=真) |
- 教育:字符串(可为空=真) |
- 性别:字符串(可为空=真) |
salaryRange:字符串(可为空=真) 然后让设置交叉验证和管道:
VAL RF =新RandomForestClassifier()VAL管道=新管道()。setStages(阵列(RF))VAL CV =新CrossValidator()。setNumFolds(10).setEstimator(管道).setEvaluator(新BinaryClassificationEvaluator) 运行这条线时的错误显示出来:
VAL cmModel = cv.fit(dataFixed)
java.lang.IllegalArgumentException异常:字段“特色”不存在 这是可以设置标签栏和功能列RandomForestClassifier,但是我有4列,predictors(功能)不是唯一的一个。
我应该如何组织我的数据帧因此具有标签和功能正常组织列? 为了您的方便这里到处是code: 进口org.apache.spark.SparkConf进口org.apache.spark.SparkContext进口org.apache.spark.ml.classification.RandomForestClassifier进口org.apache.spark.ml.evaluation.BinaryClassificationEvaluator进口org.apache.spark.ml.tuning.CrossValidator进口org.apache.spark.ml.Pipeline进口org.apache.spark.sql.DataFrame进口org.apache.spark.sql.functions._进口org.apache.spark.mllib.linalg {向量,向量}反对SampleClassification {
高清主(参数:数组[字符串]):单位= {
//设置背景火花
VAL的conf =新SparkConf()setAppName(“简单应用程序”)setMaster(“本地”)。
VAL SC =新SparkContext(CONF)
VAL sqlContext =新org.apache.spark.sql.SQLContext(SC)
进口sqlContext.implicits._
进口com.databricks.spark.csv._
通过使用databricks //加载数据“星火CSV库”
VAL数据= sqlContext.csvFile(“/家庭/杜尚/ sample.csv”)
//默认情况下,所有列导入为字符串,所以我们需要改变“年龄”和“hours_per_week”为Int
VAL toInt = UDF [诠释,字符串](_.toInt)
VAL dataFixed = data.withColumn(“时代”,toInt(数据(“时代”)))。withColumn(“hours_per_week”,toInt(数据(“hours_per_week”)))
VAL RF =新RandomForestClassifier()
VAL管道=新管道()。setStages(阵列(RF))
VAL CV =新CrossValidator()。setNumFolds(10).setEstimator(管道).setEvaluator(新BinaryClassificationEvaluator)
//这个失败,错误
//java.lang.IllegalArgumentException:字段“特色”不存在。
VAL cmModel = cv.fit(dataFixed)
}} 感谢您的帮助!解决方案 您只需要确保你在你的数据帧一“特色”列,它是的键入 VectorUDF ,如下所示: 斯卡拉> VAL DF2 = dataFixed.withColumnRenamed(“时代”,“特色”)DF2:org.apache.spark.sql.DataFrame = [特点:INT,hours_per_week:INT,教育:字符串,性别:字符串,salaryRange:字符串]斯卡拉> VAL cmModel = cv.fit(DF2)java.lang.IllegalArgumentException异常:要求失败:列功能的类型必须为org.apache.spark.mllib.linalg.VectorUDT@1eef,但实际上是IntegerType。
。在斯卡拉preDEF $ .require(predef.scala:233)
在org.apache.spark.ml.util.SchemaUtils $ .checkColumnType(SchemaUtils.scala:37)
在org.apache.spark.ml predictorParams $ class.validateAndTransformSchema(predictor.scala:50)。
。在org.apache.spark.ml predictor.validateAndTransformSchema(predictor.scala:71)
。在org.apache.spark.ml predictor.transformSchema(predictor.scala:118)
在org.apache.spark.ml.Pipeline $$ anonfun $ transformSchema $ 4.适用(Pipeline.scala:164)
在org.apache.spark.ml.Pipeline $$ anonfun $ transformSchema $ 4.适用(Pipeline.scala:164)
在scala.collection.IndexedSeqOptimized $ class.foldl(IndexedSeqOptimized.scala:51)
在scala.collection.IndexedSeqOptimized $ class.foldLeft(IndexedSeqOptimized.scala:60)
在scala.collection.mutable.ArrayOps $ ofRef.foldLeft(ArrayOps.scala:108)
在org.apache.spark.ml.Pipeline.transformSchema(Pipeline.scala:164)
在org.apache.spark.ml.tuning.CrossValidator.transformSchema(CrossValidator.scala:142)
在org.apache.spark.ml.PipelineStage.transformSchema(Pipeline.scala:59)
在org.apache.spark.ml.tuning.CrossValidator.fit(CrossValidator.scala:107)
在$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:67)
在$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:72)
在$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.&init&(&console&:74)
在IWC万国表$ $$ $$ IWC万国表IWC万国表IWC $$ $$ IWC万国表$$ $$ IWC万国表$$ $$ IWC万国表$$ $$ IWC万国表$$ $$ IWC万国表<&初始化GT;(小于控制台计算值: 76)
本质上说需要在你的数据帧的“功能”,例如标签的特征向量和“标签”两个领域。实例的类型必须是双击。要建立一个“功能”领域与矢量键入首先创建一个 UDF ,如下所示:
VAL toVec4 = UDF [载体,INT,INT,字符串,字符串] {(A,B,C,D)=>
VAL E3 = C匹配{
案“HS-毕业生”=> 0
案“单身汉”=> 1
案“主人”=> 2
VAL E4 = D匹配{案“男”=> 0的情况下“女”=> 1}
Vectors.dense(A,B,E3,E4)} 现在也EN code中的“标签”字段,创建另一个 UDF ,如下所示:
VAL EN codeLabel = UDF [双,字符串](_ {匹配的情况下“A”=> 0.0案件“B”=> 1.0}) 现在我们使用变换原数据帧这两个 UDF :
VAL DF = dataFixed.withColumn(
“特征”,
dataFixed(“时代”),
dataFixed(“hours_per_week”),
dataFixed(“教育”),
dataFixed(“性”)
)).withColumn(“标签”,恩codeLabel(dataFixed(“salaryRange”)))。选择(“特征”,“标签”) 请注意,可以有额外的列/域present的数据帧,但在这种情况下,我只选择了功能和标签: 斯卡拉> df.show()------------------- + + ----- +|功能|标签|------------------- + + ----- +| [38.0,40.0,0.0,0.0] | 0.0 || [28.0,40.0,1.0,1.0] | 0.0 || [52.0,45.0,0.0,0.0] | 1.0 || [31.0,50.0,2.0,1.0] | 1.0 || [42.0,40.0,1.0,0.0] | 1.0 |------------------- + + ----- + 现在其高达您设置正确的参数为你的学习算法,使其工作。
本文地址: &
扫一扫关注官方微信

我要回帖

更多关于 html嵌套父js的数据 的文章

 

随机推荐