错误:重载方法值“预测”与备选/ Double不带参数

时间:2017-08-09 10:10:14

标签: scala apache-spark linear-regression apache-spark-mllib apache-spark-dataset

我正在尝试使用LinearRegressionWithSGD构建一个简单的线性模型来预测标签值。 我转换数据集以获取要素和标签,然后再次转换为标记点进行回归

      final ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {

     @Override
                public void onImageAvailable(ImageReader reader) {
                    Image image = null;
                    try {
                        image = reader.acquireLatestImage();
                        ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                        byte[] bytes = new byte[buffer.capacity()];
                        buffer.get(bytes);
                        save(bytes);
                        mBitmapToSave1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        mBitmapToSave = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        Bitmap scaled = Bitmap.createScaledBitmap(mBitmapToSave, width, height, true);
                        int w = scaled.getWidth();
                        int h = scaled.getHeight();
                        // Setting post rotate to 90
                        Matrix mtx = new Matrix();
                        mtx.postRotate(-180);
                        // Rotating Bitmap
                        mBitmapToSave = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
                        // mBitmapToSave = Bitmap.createBitmap(width+rowPadding/pixelStride,height, Bitmap.Config.RGB_565);
                        // mBitmapToSave.copyPixelsToBuffer(buffer);

                        if (detector.isOperational() && mBitmapToSave != null) {
                            Frame frame = new Frame.Builder().setBitmap(mBitmapToSave).build();
                            SparseArray<Face> faces = detector.detect(frame);
                            for (index = 0; index < faces.size(); ++index) {
                                Face face = faces.valueAt(index);
                            }if (faces.size() == 0) {
                                MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.not);
                                mediaPlayer.start();
                                //Toast.makeText(AndroidCamera2API.this, "Face Not detected Adjust Camera Properly", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(AndroidCamera2API.this, "Face Found " + "\n", Toast.LENGTH_SHORT).show();
                            }

                            }
                        }catch(FileNotFoundException e){
                            e.printStackTrace();
                        } catch(IOException e){
                            e.printStackTrace();
                        } finally{
                            if (image != null) {
                                image.close();
                            }
                        }
                    }

                private void save(byte[] bytes) throws IOException {
                    OutputStream output = null;
                    try {
                        output = new FileOutputStream(file);
                        output.write(bytes);
                    } finally {
                        if (null != output) {
                            output.close();
                        }
                    }
                }
            };
            reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
            final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
                @Override
                public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                    super.onCaptureCompleted(session, request, result);
                    Toast.makeText(AndroidCamera2API.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
                    uploadMultipart();
                    createCameraPreview();

                }
 }
            };
            cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured(CameraCaptureSession session) {
                    try {
                        session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed(CameraCaptureSession session) {
                }
            }, mBackgroundHandler);
            mBitmapToSave = null;
        } catch(CameraAccessException e){
                e.printStackTrace();
            }
        }

现在我正在使用模型

val train = dftrain.withColumn("label", dftrain("col2")).select("features", "label")
val test = dftest.withColumn("label", dftest("col2")).select("features", "label")

val realout  = train.rdd.map(row => LabeledPoint(row.getAs[Double]("label"),DenseVector.fromML(row.getAs[org.apache.spark.ml.linalg.DenseVector]("features"))))
val realout1  = test.rdd.map(row => LabeledPoint(row.getAs[Double]("label"),DenseVector.fromML(row.getAs[org.apache.spark.ml.linalg.DenseVector]("features"))))
val numIterations = 100
val stepSize = 0.00000001
//fitting the model with converted Labeled points Train Data
val model = LinearRegressionWithSGD.train(realout, numIterations, stepSize)

它给了我一些警告,并且它给 17/08/09 12:16:15 WARN LinearRegressionWithSGD: The input data is not directly c ached, which may hurt performance if its parent RDDs are also uncached. 17/08/09 12:16:17 WARN BLAS: Failed to load implementation from: com.github.fomm il.netlib.NativeSystemBLAS 17/08/09 12:16:17 WARN BLAS: Failed to load implementation from: com.github.fomm il.netlib.NativeRefBLAS 17/08/09 12:16:17 WARN LinearRegressionWithSGD: The input data was not directly cached, which may hurt performance if its parent RDDs are also uncached. model: org.apache.spark.mllib.regression.LinearRegressionModel = org.apache.spar k.mllib.regression.LinearRegressionModel: intercept = 0.0, numFeatures = 1 为0.0,我感觉不正确。但是当我预测模型时,它会让我错误。

Intercept

此外,如果我从here

执行此操作
val prediction = model.predict(realout1)

<console>:98: error: overloaded method value predict with alternatives:
  (testData: org.apache.spark.api.java.JavaRDD[org.apache.spark.mllib.linalg.Vec
tor])org.apache.spark.api.java.JavaRDD[Double] <and>
  (testData: org.apache.spark.mllib.linalg.Vector)Double <and>
  (testData: org.apache.spark.rdd.RDD[org.apache.spark.mllib.linalg.Vector])org.
apache.spark.rdd.RDD[Double]
 cannot be applied to (org.apache.spark.rdd.RDD[org.apache.spark.mllib.regressio
n.LabeledPoint])
       val prediction = model.predict(realout1)
                              ^

我相信这些步骤是正确的。但我不知道为什么它显示重载方法值预测与替代 Double不接受参数

1 个答案:

答案 0 :(得分:0)

val prediction = model.predict(realout1.map(_.features));

这个工作正常。但我不知道这个是多么正确。任何建议表示赞赏。谢谢。