环境
spark-1.6
python3.5
一、有无截距
对于逻辑回归分类,就是找到z那条直线,不通过原点有截距的直线与通过原点的直线相比,有截距更能将数据分类的彻底。
packagecom.bjsxt.lrimportorg.apache.spark.mllib.classification.{LogisticRegressionWithLBFGS}importorg.apache.spark.mllib.util.MLUtilsimportorg.apache.spark.{SparkConf, SparkContext}/*** 逻辑回归 健康状况训练集*/object LogisticRegression {
def main(args: Array[String]) {
val conf= new SparkConf().setAppName("spark").setMaster("local[3]")
val sc= newSparkContext(conf)//加载 LIBSVM 格式的数据 这种格式特征前缀要从1开始
val inputData = MLUtils.loadLibSVMFile(sc, "健康状况训练集.txt")
val splits= inputData.randomSplit(Array(0.7, 0.3), seed = 1L)
val (trainingData, testData)= (splits(0), splits(1))
val lr= newLogisticRegressionWithLBFGS()//lr.setIntercept(true)
val model =lr.run(trainingData)
val result=testData
.map{point=>Math.abs(point.label-model.predict(point.features)) }
println("正确率="+(1.0-result.mean()))/***逻辑回归算法训练出来的模型,模型中的参数个数(w0....w6)=训练集中特征数(6)+1*/println(model.weights.toArray.mkString(" "))
println(model.intercept)
sc.stop()
}
}
packagecom.bjsxt.lrimportorg.apache.spark.mllib.classification.{LogisticRegressionWithLBFGS, LogisticRegressionWithSGD}importorg.apache.spark.mllib.regression.LabeledPointimportorg.apache.spark.mllib.util.MLUtilsimportorg.apache.spark.rdd.RDDimportorg.apache.spark.{SparkConf, SparkContext}/*** 有无截距*/object LogisticRegression2 {
def main(args: Array[String]) {
val conf= new SparkConf().setAppName("spark").setMaster("local[3]")
val sc= newSparkContext(conf)
val inputData: RDD[LabeledPoint]= MLUtils.loadLibSVMFile(sc, "w0测试数据.txt")/*** randomSplit(Array(0.7, 0.3))方法就是将一个RDD拆分成N个RDD,N = Array.length
* 第一个RDD中的数据量和数组中的第一个元素值相关*/val splits= inputData.randomSplit(Array(0.7, 0.3),11L)
val (trainingData, testData)= (splits(0), splits(1))
val lr= newLogisticRegressionWithSGD//设置要有W0,也就是有截距
lr.setIntercept(true)
val model=lr.run(trainingData)
val result=testData.map{labeledpoint=>Math.abs(labeledpoint.label-model.predict(labeledpoint.features)) }
println("正确率="+(1.0-result.mean()))
println(model.weights.toArray.mkString(" "))
println(model.intercept)
}</