:: DeveloperApi :: Abstract class for estimators that fit models to data.
:: DeveloperApi :: Abstract class for estimators that fit models to data.
:: DeveloperApi :: A fitted model, i.e., a Transformer produced by an Estimator.
:: DeveloperApi :: A fitted model, i.e., a Transformer produced by an Estimator.
model type
A simple pipeline, which acts as an estimator.
A simple pipeline, which acts as an estimator. A Pipeline consists of a sequence of stages, each of which is either an Estimator or a Transformer. When Pipeline#fit is called, the stages are executed in order. If a stage is an Estimator, its Estimator#fit method will be called on the input dataset to fit a model. Then the model, which is a transformer, will be used to transform the dataset as the input to the next stage. If a stage is a Transformer, its Transformer#transform method will be called to produce the dataset for the next stage. The fitted model from a Pipeline is a PipelineModel, which consists of fitted models and transformers, corresponding to the pipeline stages. If there are no stages, the pipeline acts as an identity transformer.
Represents a fitted pipeline.
Represents a fitted pipeline.
:: DeveloperApi :: A stage in a pipeline, either an Estimator or a Transformer.
:: DeveloperApi :: A stage in a pipeline, either an Estimator or a Transformer.
:: DeveloperApi :: Abstraction for a model for prediction tasks (regression and classification).
:: DeveloperApi :: Abstraction for a model for prediction tasks (regression and classification).
Type of features. E.g., org.apache.spark.mllib.linalg.VectorUDT for vector features.
Specialization of PredictionModel. If you subclass this type, use this type parameter to specify the concrete type for the corresponding model.
:: DeveloperApi :: Abstraction for prediction problems (regression and classification).
:: DeveloperApi :: Abstraction for prediction problems (regression and classification).
Type of features. E.g., org.apache.spark.mllib.linalg.VectorUDT for vector features.
Specialization of this class. If you subclass this type, use this type parameter to specify the concrete type.
Specialization of PredictionModel. If you subclass this type, use this type parameter to specify the concrete type for the corresponding model.
:: DeveloperApi :: Abstract class for transformers that transform one dataset into another.
:: DeveloperApi :: Abstract class for transformers that transform one dataset into another.
:: DeveloperApi :: Abstract class for transformers that take one input column, apply transformation, and output the result as a new column.
:: DeveloperApi :: Abstract class for transformers that take one input column, apply transformation, and output the result as a new column.
The ML pipeline API uses DataFrames as ML datasets.
The ML pipeline API uses DataFrames as ML datasets. Each dataset consists of typed columns, e.g., string, double, vector, etc. However, knowing only the column type may not be sufficient to handle the data properly. For instance, a double column with values 0.0, 1.0, 2.0, ... may represent some label indices, which cannot be treated as numeric values in ML algorithms, and, for another instance, we may want to know the names and types of features stored in a vector column. ML attributes are used to provide additional information to describe columns in a dataset.
A column with ML attributes attached is called an ML column. The data in ML columns are stored as double values, i.e., an ML column is either a scalar column of double values or a vector column. Columns of other types must be encoded into ML columns using transformers. We use Attribute to describe a scalar ML column, and AttributeGroup to describe a vector ML column. ML attributes are stored in the metadata field of the column schema.
The ml.feature
package provides common feature transformers that help convert raw data or
features into more suitable forms for model fitting.
The ml.feature
package provides common feature transformers that help convert raw data or
features into more suitable forms for model fitting.
Most feature transformers are implemented as Transformers, which transform one DataFrame
into another, e.g., HashingTF.
Some feature transformers are implemented as Estimators, because the transformation requires
some aggregated information of the dataset, e.g., document frequencies in IDF.
For those feature transformers, calling Estimator!.fit is required to obtain the model first,
e.g., IDFModel, in order to apply transformation.
The transformation is usually done by appending new columns to the input DataFrame, so all
input columns are carried over.
We try to make each transformer minimal, so it becomes flexible to assemble feature transformation pipelines. Pipeline can be used to chain feature transformers, and VectorAssembler can be used to combine multiple feature transformations, for example:
import org.apache.spark.ml.feature._ import org.apache.spark.ml.Pipeline // a DataFrame with three columns: id (integer), text (string), and rating (double). val df = spark.createDataFrame(Seq( (0, "Hi I heard about Spark", 3.0), (1, "I wish Java could use case classes", 4.0), (2, "Logistic regression models are neat", 4.0) )).toDF("id", "text", "rating") // define feature transformers val tok = new RegexTokenizer() .setInputCol("text") .setOutputCol("words") val sw = new StopWordsRemover() .setInputCol("words") .setOutputCol("filtered_words") val tf = new HashingTF() .setInputCol("filtered_words") .setOutputCol("tf") .setNumFeatures(10000) val idf = new IDF() .setInputCol("tf") .setOutputCol("tf_idf") val assembler = new VectorAssembler() .setInputCols(Array("tf_idf", "rating")) .setOutputCol("features") // assemble and fit the feature transformation pipeline val pipeline = new Pipeline() .setStages(Array(tok, sw, tf, idf, assembler)) val model = pipeline.fit(df) // save transformed features with raw data model.transform(df) .select("id", "text", "rating", "features") .write.format("parquet").save("/output/path")
Some feature transformers implemented in MLlib are inspired by those implemented in scikit-learn. The major difference is that most scikit-learn feature transformers operate eagerly on the entire input dataset, while MLlib's feature transformers operate lazily on individual columns, which is more efficient and flexible to handle large and complex datasets.
DataFrame-based machine learning APIs to let users quickly assemble and configure practical machine learning pipelines.