PySpark中LinearRegression.load()抛出NoSuchMethodException问题求助
Hey there, let's break down why you're running into this error when trying to load your Linear Regression model in Spark 2.1.0. I've seen this issue pop up a few times, and it usually boils down to one of these common pitfalls:
1. You're mixing up Spark's ML and MLlib APIs
Spark has two separate machine learning APIs: the older RDD-based mllib package, and the newer DataFrame-focused ml package. The load() method for LinearRegressionModel only exists in the ml package—the old mllib version doesn't support this method at all.
Double-check your imports right away:
- ❌ Wrong import (old mllib API, no
load()method):import org.apache.spark.mllib.regression.LinearRegressionModel - ✅ Correct import (new ml API, supports
load()):import org.apache.spark.ml.regression.LinearRegressionModel
2. Mismatched save/load workflows
If you saved your model using the old mllib API (like saveAsObjectFile), you can't load it with the ml API's load() method—and vice versa. You have to use the same API for both saving and loading:
Example of a valid ML API workflow:
// Train and save the model (using ML API) import org.apache.spark.ml.regression.{LinearRegression, LinearRegressionModel} import org.apache.spark.sql.SparkSession val spark = SparkSession.builder().appName("LRModelDemo").getOrCreate() val trainingData = spark.read.format("libsvm").load("data/mllib/sample_linear_regression_data.txt") val lr = new LinearRegression() val lrModel = lr.fit(trainingData) lrModel.save("/user/your_username/lr_model") // Save via ML API // Load the model later val loadedModel = LinearRegressionModel.load("/user/your_username/lr_model")
3. Path-related issues
Make sure the path you're passing to load() is valid and accessible:
- For local files, prefix the path with
file:///(e.g.,file:///home/user/lr_model) - For HDFS paths, use the full URI (e.g.,
hdfs://namenode:port/user/your_username/lr_model) - Verify your Spark app has read permissions for the target path
If you've checked all these and still get errors, share the exact error message and a snippet of your load code—I can help you dig deeper!
内容的提问来源于stack exchange,提问作者Gaurav Gupta

