Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #
HasRegParam, HasMaxIter, HasFitIntercept, HasTol, HasStandardization, HasWeightCol, \ HasAggregationDepth, HasThreshold, HasBlockSize, HasMaxBlockSizeInMB, Param, Params, \ TypeConverters, HasElasticNetParam, HasSeed, HasStepSize, HasSolver, HasParallelism _TreeEnsembleModel, _RandomForestParams, _GBTParams, \ _HasVarianceImpurity, _TreeClassifierParams JavaMLReadable, JavaMLReader, JavaMLWritable, JavaMLWriter, \ MLReader, MLReadable, MLWriter, MLWritable, HasTrainingSummary JavaPredictor, JavaPredictionModel, JavaWrapper
'LinearSVCSummary', 'LinearSVCTrainingSummary', 'LogisticRegression', 'LogisticRegressionModel', 'LogisticRegressionSummary', 'LogisticRegressionTrainingSummary', 'BinaryLogisticRegressionSummary', 'BinaryLogisticRegressionTrainingSummary', 'DecisionTreeClassifier', 'DecisionTreeClassificationModel', 'GBTClassifier', 'GBTClassificationModel', 'RandomForestClassifier', 'RandomForestClassificationModel', 'RandomForestClassificationSummary', 'RandomForestClassificationTrainingSummary', 'BinaryRandomForestClassificationSummary', 'BinaryRandomForestClassificationTrainingSummary', 'NaiveBayes', 'NaiveBayesModel', 'MultilayerPerceptronClassifier', 'MultilayerPerceptronClassificationModel', 'MultilayerPerceptronClassificationSummary', 'MultilayerPerceptronClassificationTrainingSummary', 'OneVsRest', 'OneVsRestModel', 'FMClassifier', 'FMClassificationModel', 'FMClassificationSummary', 'FMClassificationTrainingSummary']
""" Classifier Params for classification tasks.
.. versionadded:: 3.0.0 """
""" Classifier for classification tasks. Classes are indexed {0, 1, ..., numClasses - 1}. """
def setRawPredictionCol(self, value): """ Sets the value of :py:attr:`rawPredictionCol`. """ return self._set(rawPredictionCol=value)
""" Model produced by a ``Classifier``. Classes are indexed {0, 1, ..., numClasses - 1}. """
def setRawPredictionCol(self, value): """ Sets the value of :py:attr:`rawPredictionCol`. """
def numClasses(self): """ Number of classes (values which the label can take). """ raise NotImplementedError()
def predictRaw(self, value): """ Raw prediction for each possible label. """ raise NotImplementedError()
""" Params for :py:class:`ProbabilisticClassifier` and :py:class:`ProbabilisticClassificationModel`.
.. versionadded:: 3.0.0 """
metaclass=ABCMeta): """ Probabilistic Classifier for classification tasks. """
def setProbabilityCol(self, value): """ Sets the value of :py:attr:`probabilityCol`. """ return self._set(probabilityCol=value)
def setThresholds(self, value): """ Sets the value of :py:attr:`thresholds`. """
_ProbabilisticClassifierParams, metaclass=ABCMeta): """ Model produced by a ``ProbabilisticClassifier``. """
def setProbabilityCol(self, value): """ Sets the value of :py:attr:`probabilityCol`. """
def setThresholds(self, value): """ Sets the value of :py:attr:`thresholds`. """
def predictProbability(self, value): """ Predict the probability of each class given the features. """ raise NotImplementedError()
""" Java Classifier for classification tasks. Classes are indexed {0, 1, ..., numClasses - 1}. """
def setRawPredictionCol(self, value): """ Sets the value of :py:attr:`rawPredictionCol`. """ return self._set(rawPredictionCol=value)
""" Java Model produced by a ``Classifier``. Classes are indexed {0, 1, ..., numClasses - 1}. To be mixed in with :class:`pyspark.ml.JavaModel` """
def numClasses(self): """ Number of classes (values which the label can take). """
def predictRaw(self, value): """ Raw prediction for each possible label. """
metaclass=ABCMeta): """ Java Probabilistic Classifier for classification tasks. """
_JavaClassificationModel): """ Java Model produced by a ``ProbabilisticClassifier``. """
def predictProbability(self, value): """ Predict the probability of each class given the features. """
""" Abstraction for multiclass classification results for a given model.
.. versionadded:: 3.1.0 """
def predictions(self): """ Dataframe outputted by the model's `transform` method. """
def predictionCol(self): """ Field in "predictions" which gives the prediction of each class. """
def labelCol(self): """ Field in "predictions" which gives the true label of each instance. """
def weightCol(self): """ Field in "predictions" which gives the weight of each instance as a vector. """ return self._call_java("weightCol")
def labels(self): """ Returns the sequence of labels in ascending order. This order matches the order used in metrics which are specified as arrays over labels, e.g., truePositiveRateByLabel.
.. versionadded:: 3.1.0
Notes ----- In most cases, it will be values {0.0, 1.0, ..., numClasses-1}, However, if the training set is missing a label, then all of the arrays over labels (e.g., from truePositiveRateByLabel) will be of length numClasses-1 instead of the expected numClasses. """
def truePositiveRateByLabel(self): """ Returns true positive rate for each label (category). """
def falsePositiveRateByLabel(self): """ Returns false positive rate for each label (category). """
def precisionByLabel(self): """ Returns precision for each label (category). """
def recallByLabel(self): """ Returns recall for each label (category). """
""" Returns f-measure for each label (category). """
def accuracy(self): """ Returns accuracy. (equals to the total number of correctly classified instances out of the total number of instances.) """
def weightedTruePositiveRate(self): """ Returns weighted true positive rate. (equals to precision, recall and f-measure) """
def weightedFalsePositiveRate(self): """ Returns weighted false positive rate. """
def weightedRecall(self): """ Returns weighted averaged recall. (equals to precision, recall and f-measure) """
def weightedPrecision(self): """ Returns weighted averaged precision. """
""" Returns weighted averaged f-measure. """
""" Abstraction for Training results.
.. versionadded:: 3.1.0 """
def objectiveHistory(self): """ Objective function (scaled loss + regularization) at each iteration. It contains one more element, the initial state, than number of iterations. """
def totalIterations(self): """ Number of training iterations until termination. """
""" Binary classification results for a given model.
.. versionadded:: 3.1.0 """
def scoreCol(self): """ Field in "predictions" which gives the probability or raw prediction of each class as a vector. """
def roc(self): """ Returns the receiver operating characteristic (ROC) curve, which is a Dataframe having two fields (FPR, TPR) with (0.0, 0.0) prepended and (1.0, 1.0) appended to it.
.. versionadded:: 3.1.0
Notes ----- `Wikipedia reference <http://en.wikipedia.org/wiki/Receiver_operating_characteristic>`_ """
def areaUnderROC(self): """ Computes the area under the receiver operating characteristic (ROC) curve. """
def pr(self): """ Returns the precision-recall curve, which is a Dataframe containing two fields recall, precision with (0.0, 1.0) prepended to it. """
def fMeasureByThreshold(self): """ Returns a dataframe with two fields (threshold, F-Measure) curve with beta = 1.0. """
def precisionByThreshold(self): """ Returns a dataframe with two fields (threshold, precision) curve. Every possible probability obtained in transforming the dataset are used as thresholds used in calculating the precision. """
def recallByThreshold(self): """ Returns a dataframe with two fields (threshold, recall) curve. Every possible probability obtained in transforming the dataset are used as thresholds used in calculating the recall. """
HasStandardization, HasWeightCol, HasAggregationDepth, HasThreshold, HasMaxBlockSizeInMB): """ Params for :py:class:`LinearSVC` and :py:class:`LinearSVCModel`.
.. versionadded:: 3.0.0 """
"The threshold in binary classification applied to the linear model" " prediction. This threshold can be any real number, where Inf will make" " all predictions 0.0 and -Inf will make all predictions 1.0.", typeConverter=TypeConverters.toFloat)
standardization=True, threshold=0.0, aggregationDepth=2, maxBlockSizeInMB=0.0)
""" This binary classifier optimizes the Hinge Loss using the OWLQN optimizer. Only supports L2 regularization currently.
.. versionadded:: 2.2.0
Notes ----- `Linear SVM Classifier <https://en.wikipedia.org/wiki/Support_vector_machine#Linear_SVM>`_
Examples -------- >>> from pyspark.sql import Row >>> from pyspark.ml.linalg import Vectors >>> df = sc.parallelize([ ... Row(label=1.0, features=Vectors.dense(1.0, 1.0, 1.0)), ... Row(label=0.0, features=Vectors.dense(1.0, 2.0, 3.0))]).toDF() >>> svm = LinearSVC() >>> svm.getMaxIter() 100 >>> svm.setMaxIter(5) LinearSVC... >>> svm.getMaxIter() 5 >>> svm.getRegParam() 0.0 >>> svm.setRegParam(0.01) LinearSVC... >>> svm.getRegParam() 0.01 >>> model = svm.fit(df) >>> model.setPredictionCol("newPrediction") LinearSVCModel... >>> model.getPredictionCol() 'newPrediction' >>> model.setThreshold(0.5) LinearSVCModel... >>> model.getThreshold() 0.5 >>> model.getMaxBlockSizeInMB() 0.0 >>> model.coefficients DenseVector([0.0, -1.0319, -0.5159]) >>> model.intercept 2.579645978780695 >>> model.numClasses 2 >>> model.numFeatures 3 >>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, -1.0, -1.0))]).toDF() >>> model.predict(test0.head().features) 1.0 >>> model.predictRaw(test0.head().features) DenseVector([-4.1274, 4.1274]) >>> result = model.transform(test0).head() >>> result.newPrediction 1.0 >>> result.rawPrediction DenseVector([-4.1274, 4.1274]) >>> svm_path = temp_path + "/svm" >>> svm.save(svm_path) >>> svm2 = LinearSVC.load(svm_path) >>> svm2.getMaxIter() 5 >>> model_path = temp_path + "/svm_model" >>> model.save(model_path) >>> model2 = LinearSVCModel.load(model_path) >>> model.coefficients[0] == model2.coefficients[0] True >>> model.intercept == model2.intercept True >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True """
maxIter=100, regParam=0.0, tol=1e-6, rawPredictionCol="rawPrediction", fitIntercept=True, standardization=True, threshold=0.0, weightCol=None, aggregationDepth=2, maxBlockSizeInMB=0.0): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, regParam=0.0, tol=1e-6, rawPredictionCol="rawPrediction", \ fitIntercept=True, standardization=True, threshold=0.0, weightCol=None, \ aggregationDepth=2, maxBlockSizeInMB=0.0): """ "org.apache.spark.ml.classification.LinearSVC", self.uid)
maxIter=100, regParam=0.0, tol=1e-6, rawPredictionCol="rawPrediction", fitIntercept=True, standardization=True, threshold=0.0, weightCol=None, aggregationDepth=2, maxBlockSizeInMB=0.0): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, regParam=0.0, tol=1e-6, rawPredictionCol="rawPrediction", \ fitIntercept=True, standardization=True, threshold=0.0, weightCol=None, \ aggregationDepth=2, maxBlockSizeInMB=0.0): Sets params for Linear SVM Classifier. """
def setMaxIter(self, value): """ Sets the value of :py:attr:`maxIter`. """
def setRegParam(self, value): """ Sets the value of :py:attr:`regParam`. """
def setTol(self, value): """ Sets the value of :py:attr:`tol`. """ return self._set(tol=value)
def setFitIntercept(self, value): """ Sets the value of :py:attr:`fitIntercept`. """ return self._set(fitIntercept=value)
def setStandardization(self, value): """ Sets the value of :py:attr:`standardization`. """ return self._set(standardization=value)
def setThreshold(self, value): """ Sets the value of :py:attr:`threshold`. """ return self._set(threshold=value)
def setWeightCol(self, value): """ Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
def setAggregationDepth(self, value): """ Sets the value of :py:attr:`aggregationDepth`. """ return self._set(aggregationDepth=value)
def setMaxBlockSizeInMB(self, value): """ Sets the value of :py:attr:`maxBlockSizeInMB`. """ return self._set(maxBlockSizeInMB=value)
HasTrainingSummary): """ Model fitted by LinearSVC.
.. versionadded:: 2.2.0 """
def setThreshold(self, value): """ Sets the value of :py:attr:`threshold`. """
def coefficients(self): """ Model coefficients of Linear SVM Classifier. """
def intercept(self): """ Model intercept of Linear SVM Classifier. """
def summary(self): """ Gets summary (accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
""" Evaluates the model on a test dataset.
.. versionadded:: 3.1.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """ raise TypeError("dataset must be a DataFrame but got %s." % type(dataset))
""" Abstraction for LinearSVC Results for a given model.
.. versionadded:: 3.1.0 """
""" Abstraction for LinearSVC Training results.
.. versionadded:: 3.1.0 """
HasElasticNetParam, HasMaxIter, HasFitIntercept, HasTol, HasStandardization, HasWeightCol, HasAggregationDepth, HasThreshold, HasMaxBlockSizeInMB): """ Params for :py:class:`LogisticRegression` and :py:class:`LogisticRegressionModel`.
.. versionadded:: 3.0.0 """
"Threshold in binary classification prediction, in range [0, 1]." + " If threshold and thresholds are both set, they must match." + "e.g. if threshold is p, then thresholds must be equal to [1-p, p].", typeConverter=TypeConverters.toFloat)
"The name of family which is a description of the label distribution to " + "be used in the model. Supported options: auto, binomial, multinomial", typeConverter=TypeConverters.toString)
"The lower bounds on coefficients if fitting under bound " "constrained optimization. The bound matrix must be " "compatible with the shape " "(1, number of features) for binomial regression, or " "(number of classes, number of features) " "for multinomial regression.", typeConverter=TypeConverters.toMatrix)
"The upper bounds on coefficients if fitting under bound " "constrained optimization. The bound matrix must be " "compatible with the shape " "(1, number of features) for binomial regression, or " "(number of classes, number of features) " "for multinomial regression.", typeConverter=TypeConverters.toMatrix)
"The lower bounds on intercepts if fitting under bound " "constrained optimization. The bounds vector size must be" "equal with 1 for binomial regression, or the number of" "lasses for multinomial regression.", typeConverter=TypeConverters.toVector)
"The upper bounds on intercepts if fitting under bound " "constrained optimization. The bound vector size must be " "equal with 1 for binomial regression, or the number of " "classes for multinomial regression.", typeConverter=TypeConverters.toVector)
maxBlockSizeInMB=0.0)
def setThreshold(self, value): """ Sets the value of :py:attr:`threshold`. Clears value of :py:attr:`thresholds` if it has been set. """
def getThreshold(self): """ Get threshold for binary classification.
If :py:attr:`thresholds` is set with length 2 (i.e., binary classification), this returns the equivalent threshold: :math:`\\frac{1}{1 + \\frac{thresholds(0)}{thresholds(1)}}`. Otherwise, returns :py:attr:`threshold` if set or its default value if unset. """ ts = self.getOrDefault(self.thresholds) if len(ts) != 2: raise ValueError("Logistic Regression getThreshold only applies to" + " binary classification, but thresholds has length != 2." + " thresholds: " + ",".join(ts)) return 1.0/(1.0 + ts[0]/ts[1]) else:
def setThresholds(self, value): """ Sets the value of :py:attr:`thresholds`. Clears value of :py:attr:`threshold` if it has been set. """ self._set(thresholds=value) self.clear(self.threshold) return self
def getThresholds(self): """ If :py:attr:`thresholds` is set, return its value. Otherwise, if :py:attr:`threshold` is set, return the equivalent thresholds for binary classification: (1-threshold, threshold). If neither are set, throw an error. """ self._checkThresholdConsistency() if not self.isSet(self.thresholds) and self.isSet(self.threshold): t = self.getOrDefault(self.threshold) return [1.0-t, t] else: return self.getOrDefault(self.thresholds)
raise ValueError("Logistic Regression getThreshold only applies to" + " binary classification, but thresholds has length != 2." + " thresholds: {0}".format(str(ts))) " threshold (%g) and thresholds (equivalent to %g)" % (t2, t))
def getFamily(self): """ Gets the value of :py:attr:`family` or its default value. """ return self.getOrDefault(self.family)
def getLowerBoundsOnCoefficients(self): """ Gets the value of :py:attr:`lowerBoundsOnCoefficients` """ return self.getOrDefault(self.lowerBoundsOnCoefficients)
def getUpperBoundsOnCoefficients(self): """ Gets the value of :py:attr:`upperBoundsOnCoefficients` """ return self.getOrDefault(self.upperBoundsOnCoefficients)
def getLowerBoundsOnIntercepts(self): """ Gets the value of :py:attr:`lowerBoundsOnIntercepts` """ return self.getOrDefault(self.lowerBoundsOnIntercepts)
def getUpperBoundsOnIntercepts(self): """ Gets the value of :py:attr:`upperBoundsOnIntercepts` """ return self.getOrDefault(self.upperBoundsOnIntercepts)
JavaMLReadable): """ Logistic regression. This class supports multinomial logistic (softmax) and binomial logistic regression.
.. versionadded:: 1.3.0
Examples -------- >>> from pyspark.sql import Row >>> from pyspark.ml.linalg import Vectors >>> bdf = sc.parallelize([ ... Row(label=1.0, weight=1.0, features=Vectors.dense(0.0, 5.0)), ... Row(label=0.0, weight=2.0, features=Vectors.dense(1.0, 2.0)), ... Row(label=1.0, weight=3.0, features=Vectors.dense(2.0, 1.0)), ... Row(label=0.0, weight=4.0, features=Vectors.dense(3.0, 3.0))]).toDF() >>> blor = LogisticRegression(weightCol="weight") >>> blor.getRegParam() 0.0 >>> blor.setRegParam(0.01) LogisticRegression... >>> blor.getRegParam() 0.01 >>> blor.setMaxIter(10) LogisticRegression... >>> blor.getMaxIter() 10 >>> blor.clear(blor.maxIter) >>> blorModel = blor.fit(bdf) >>> blorModel.setFeaturesCol("features") LogisticRegressionModel... >>> blorModel.setProbabilityCol("newProbability") LogisticRegressionModel... >>> blorModel.getProbabilityCol() 'newProbability' >>> blorModel.getMaxBlockSizeInMB() 0.0 >>> blorModel.setThreshold(0.1) LogisticRegressionModel... >>> blorModel.getThreshold() 0.1 >>> blorModel.coefficients DenseVector([-1.080..., -0.646...]) >>> blorModel.intercept 3.112... >>> blorModel.evaluate(bdf).accuracy == blorModel.summary.accuracy True >>> data_path = "data/mllib/sample_multiclass_classification_data.txt" >>> mdf = spark.read.format("libsvm").load(data_path) >>> mlor = LogisticRegression(regParam=0.1, elasticNetParam=1.0, family="multinomial") >>> mlorModel = mlor.fit(mdf) >>> mlorModel.coefficientMatrix SparseMatrix(3, 4, [0, 1, 2, 3], [3, 2, 1], [1.87..., -2.75..., -0.50...], 1) >>> mlorModel.interceptVector DenseVector([0.04..., -0.42..., 0.37...]) >>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, 1.0))]).toDF() >>> blorModel.predict(test0.head().features) 1.0 >>> blorModel.predictRaw(test0.head().features) DenseVector([-3.54..., 3.54...]) >>> blorModel.predictProbability(test0.head().features) DenseVector([0.028, 0.972]) >>> result = blorModel.transform(test0).head() >>> result.prediction 1.0 >>> result.newProbability DenseVector([0.02..., 0.97...]) >>> result.rawPrediction DenseVector([-3.54..., 3.54...]) >>> test1 = sc.parallelize([Row(features=Vectors.sparse(2, [0], [1.0]))]).toDF() >>> blorModel.transform(test1).head().prediction 1.0 >>> blor.setParams("vector") Traceback (most recent call last): ... TypeError: Method setParams forces keyword arguments. >>> lr_path = temp_path + "/lr" >>> blor.save(lr_path) >>> lr2 = LogisticRegression.load(lr_path) >>> lr2.getRegParam() 0.01 >>> model_path = temp_path + "/lr_model" >>> blorModel.save(model_path) >>> model2 = LogisticRegressionModel.load(model_path) >>> blorModel.coefficients[0] == model2.coefficients[0] True >>> blorModel.intercept == model2.intercept True >>> model2 LogisticRegressionModel: uid=..., numClasses=2, numFeatures=2 >>> blorModel.transform(test0).take(1) == model2.transform(test0).take(1) True """
maxIter=100, regParam=0.0, elasticNetParam=0.0, tol=1e-6, fitIntercept=True, threshold=0.5, thresholds=None, probabilityCol="probability", rawPredictionCol="rawPrediction", standardization=True, weightCol=None, aggregationDepth=2, family="auto", lowerBoundsOnCoefficients=None, upperBoundsOnCoefficients=None, lowerBoundsOnIntercepts=None, upperBoundsOnIntercepts=None, maxBlockSizeInMB=0.0):
""" __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, regParam=0.0, elasticNetParam=0.0, tol=1e-6, fitIntercept=True, \ threshold=0.5, thresholds=None, probabilityCol="probability", \ rawPredictionCol="rawPrediction", standardization=True, weightCol=None, \ aggregationDepth=2, family="auto", \ lowerBoundsOnCoefficients=None, upperBoundsOnCoefficients=None, \ lowerBoundsOnIntercepts=None, upperBoundsOnIntercepts=None, \ maxBlockSizeInMB=0.0): If the threshold and thresholds Params are both set, they must be equivalent. """ "org.apache.spark.ml.classification.LogisticRegression", self.uid)
maxIter=100, regParam=0.0, elasticNetParam=0.0, tol=1e-6, fitIntercept=True, threshold=0.5, thresholds=None, probabilityCol="probability", rawPredictionCol="rawPrediction", standardization=True, weightCol=None, aggregationDepth=2, family="auto", lowerBoundsOnCoefficients=None, upperBoundsOnCoefficients=None, lowerBoundsOnIntercepts=None, upperBoundsOnIntercepts=None, maxBlockSizeInMB=0.0): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, regParam=0.0, elasticNetParam=0.0, tol=1e-6, fitIntercept=True, \ threshold=0.5, thresholds=None, probabilityCol="probability", \ rawPredictionCol="rawPrediction", standardization=True, weightCol=None, \ aggregationDepth=2, family="auto", \ lowerBoundsOnCoefficients=None, upperBoundsOnCoefficients=None, \ lowerBoundsOnIntercepts=None, upperBoundsOnIntercepts=None, \ maxBlockSizeInMB=0.0): Sets params for logistic regression. If the threshold and thresholds Params are both set, they must be equivalent. """
def setFamily(self, value): """ Sets the value of :py:attr:`family`. """ return self._set(family=value)
def setLowerBoundsOnCoefficients(self, value): """ Sets the value of :py:attr:`lowerBoundsOnCoefficients` """ return self._set(lowerBoundsOnCoefficients=value)
def setUpperBoundsOnCoefficients(self, value): """ Sets the value of :py:attr:`upperBoundsOnCoefficients` """ return self._set(upperBoundsOnCoefficients=value)
def setLowerBoundsOnIntercepts(self, value): """ Sets the value of :py:attr:`lowerBoundsOnIntercepts` """ return self._set(lowerBoundsOnIntercepts=value)
def setUpperBoundsOnIntercepts(self, value): """ Sets the value of :py:attr:`upperBoundsOnIntercepts` """ return self._set(upperBoundsOnIntercepts=value)
""" Sets the value of :py:attr:`maxIter`. """
""" Sets the value of :py:attr:`regParam`. """
""" Sets the value of :py:attr:`tol`. """ return self._set(tol=value)
""" Sets the value of :py:attr:`elasticNetParam`. """ return self._set(elasticNetParam=value)
""" Sets the value of :py:attr:`fitIntercept`. """ return self._set(fitIntercept=value)
""" Sets the value of :py:attr:`standardization`. """ return self._set(standardization=value)
""" Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
""" Sets the value of :py:attr:`aggregationDepth`. """ return self._set(aggregationDepth=value)
def setMaxBlockSizeInMB(self, value): """ Sets the value of :py:attr:`maxBlockSizeInMB`. """ return self._set(maxBlockSizeInMB=value)
JavaMLWritable, JavaMLReadable, HasTrainingSummary): """ Model fitted by LogisticRegression.
.. versionadded:: 1.3.0 """
def coefficients(self): """ Model coefficients of binomial logistic regression. An exception is thrown in the case of multinomial logistic regression. """
def intercept(self): """ Model intercept of binomial logistic regression. An exception is thrown in the case of multinomial logistic regression. """
def coefficientMatrix(self): """ Model coefficients. """
def interceptVector(self): """ Model intercept. """
def summary(self): """ Gets summary (accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ self).summary) else: self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
""" Evaluates the model on a test dataset.
.. versionadded:: 2.0.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """ raise TypeError("dataset must be a DataFrame but got %s." % type(dataset)) else:
""" Abstraction for Logistic Regression Results for a given model.
.. versionadded:: 2.0.0 """
def probabilityCol(self): """ Field in "predictions" which gives the probability of each class as a vector. """
def featuresCol(self): """ Field in "predictions" which gives the features of each instance as a vector. """
""" Abstraction for multinomial Logistic Regression Training results.
.. versionadded:: 2.0.0 """
LogisticRegressionSummary): """ Binary Logistic regression results for a given model.
.. versionadded:: 2.0.0 """
LogisticRegressionTrainingSummary): """ Binary Logistic regression training results for a given model.
.. versionadded:: 2.0.0 """
""" Params for :py:class:`DecisionTreeClassifier` and :py:class:`DecisionTreeClassificationModel`. """
maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", leafCol="", minWeightFractionPerNode=0.0)
JavaMLWritable, JavaMLReadable): """ `Decision tree <http://en.wikipedia.org/wiki/Decision_tree_learning>`_ learning algorithm for classification. It supports both binary and multiclass labels, as well as both continuous and categorical features.
.. versionadded:: 1.4.0
Examples -------- >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.feature import StringIndexer >>> df = spark.createDataFrame([ ... (1.0, Vectors.dense(1.0)), ... (0.0, Vectors.sparse(1, [], []))], ["label", "features"]) >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed") >>> si_model = stringIndexer.fit(df) >>> td = si_model.transform(df) >>> dt = DecisionTreeClassifier(maxDepth=2, labelCol="indexed", leafCol="leafId") >>> model = dt.fit(td) >>> model.getLabelCol() 'indexed' >>> model.setFeaturesCol("features") DecisionTreeClassificationModel... >>> model.numNodes 3 >>> model.depth 1 >>> model.featureImportances SparseVector(1, {0: 1.0}) >>> model.numFeatures 1 >>> model.numClasses 2 >>> print(model.toDebugString) DecisionTreeClassificationModel...depth=1, numNodes=3... >>> test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"]) >>> model.predict(test0.head().features) 0.0 >>> model.predictRaw(test0.head().features) DenseVector([1.0, 0.0]) >>> model.predictProbability(test0.head().features) DenseVector([1.0, 0.0]) >>> result = model.transform(test0).head() >>> result.prediction 0.0 >>> result.probability DenseVector([1.0, 0.0]) >>> result.rawPrediction DenseVector([1.0, 0.0]) >>> result.leafId 0.0 >>> test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"]) >>> model.transform(test1).head().prediction 1.0 >>> dtc_path = temp_path + "/dtc" >>> dt.save(dtc_path) >>> dt2 = DecisionTreeClassifier.load(dtc_path) >>> dt2.getMaxDepth() 2 >>> model_path = temp_path + "/dtc_model" >>> model.save(model_path) >>> model2 = DecisionTreeClassificationModel.load(model_path) >>> model.featureImportances == model2.featureImportances True >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True >>> df3 = spark.createDataFrame([ ... (1.0, 0.2, Vectors.dense(1.0)), ... (1.0, 0.8, Vectors.dense(1.0)), ... (0.0, 1.0, Vectors.sparse(1, [], []))], ["label", "weight", "features"]) >>> si3 = StringIndexer(inputCol="label", outputCol="indexed") >>> si_model3 = si3.fit(df3) >>> td3 = si_model3.transform(df3) >>> dt3 = DecisionTreeClassifier(maxDepth=2, weightCol="weight", labelCol="indexed") >>> model3 = dt3.fit(td3) >>> print(model3.toDebugString) DecisionTreeClassificationModel...depth=1, numNodes=3... """
probabilityCol="probability", rawPredictionCol="rawPrediction", maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", seed=None, weightCol=None, leafCol="", minWeightFractionPerNode=0.0): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", \ seed=None, weightCol=None, leafCol="", minWeightFractionPerNode=0.0) """ "org.apache.spark.ml.classification.DecisionTreeClassifier", self.uid)
probabilityCol="probability", rawPredictionCol="rawPrediction", maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", seed=None, weightCol=None, leafCol="", minWeightFractionPerNode=0.0): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", \ seed=None, weightCol=None, leafCol="", minWeightFractionPerNode=0.0) Sets params for the DecisionTreeClassifier. """
""" Sets the value of :py:attr:`maxDepth`. """ return self._set(maxDepth=value)
""" Sets the value of :py:attr:`maxBins`. """ return self._set(maxBins=value)
""" Sets the value of :py:attr:`minInstancesPerNode`. """ return self._set(minInstancesPerNode=value)
def setMinWeightFractionPerNode(self, value): """ Sets the value of :py:attr:`minWeightFractionPerNode`. """ return self._set(minWeightFractionPerNode=value)
""" Sets the value of :py:attr:`minInfoGain`. """ return self._set(minInfoGain=value)
""" Sets the value of :py:attr:`maxMemoryInMB`. """ return self._set(maxMemoryInMB=value)
""" Sets the value of :py:attr:`cacheNodeIds`. """ return self._set(cacheNodeIds=value)
def setImpurity(self, value): """ Sets the value of :py:attr:`impurity`. """ return self._set(impurity=value)
def setCheckpointInterval(self, value): """ Sets the value of :py:attr:`checkpointInterval`. """ return self._set(checkpointInterval=value)
""" Sets the value of :py:attr:`seed`. """ return self._set(seed=value)
def setWeightCol(self, value): """ Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
_DecisionTreeClassifierParams, JavaMLWritable, JavaMLReadable): """ Model fitted by DecisionTreeClassifier.
.. versionadded:: 1.4.0 """
def featureImportances(self): """ Estimate of the importance of each feature.
This generalizes the idea of "Gini" importance to other losses, following the explanation of Gini importance from "Random Forests" documentation by Leo Breiman and Adele Cutler, and following the implementation from scikit-learn.
This feature importance is calculated as follows: - importance(feature j) = sum (over nodes which split on feature j) of the gain, where gain is scaled by the number of instances passing through node - Normalize importances for tree to sum to 1.
.. versionadded:: 2.0.0
Notes ----- Feature importance for single decision trees can have high variance due to correlated predictor variables. Consider using a :py:class:`RandomForestClassifier` to determine feature importance instead. """
""" Params for :py:class:`RandomForestClassifier` and :py:class:`RandomForestClassificationModel`. """
maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", numTrees=20, featureSubsetStrategy="auto", subsamplingRate=1.0, leafCol="", minWeightFractionPerNode=0.0, bootstrap=True)
JavaMLWritable, JavaMLReadable): """ `Random Forest <http://en.wikipedia.org/wiki/Random_forest>`_ learning algorithm for classification. It supports both binary and multiclass labels, as well as both continuous and categorical features.
.. versionadded:: 1.4.0
Examples -------- >>> import numpy >>> from numpy import allclose >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.feature import StringIndexer >>> df = spark.createDataFrame([ ... (1.0, Vectors.dense(1.0)), ... (0.0, Vectors.sparse(1, [], []))], ["label", "features"]) >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed") >>> si_model = stringIndexer.fit(df) >>> td = si_model.transform(df) >>> rf = RandomForestClassifier(numTrees=3, maxDepth=2, labelCol="indexed", seed=42, ... leafCol="leafId") >>> rf.getMinWeightFractionPerNode() 0.0 >>> model = rf.fit(td) >>> model.getLabelCol() 'indexed' >>> model.setFeaturesCol("features") RandomForestClassificationModel... >>> model.setRawPredictionCol("newRawPrediction") RandomForestClassificationModel... >>> model.getBootstrap() True >>> model.getRawPredictionCol() 'newRawPrediction' >>> model.featureImportances SparseVector(1, {0: 1.0}) >>> allclose(model.treeWeights, [1.0, 1.0, 1.0]) True >>> test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"]) >>> model.predict(test0.head().features) 0.0 >>> model.predictRaw(test0.head().features) DenseVector([2.0, 0.0]) >>> model.predictProbability(test0.head().features) DenseVector([1.0, 0.0]) >>> result = model.transform(test0).head() >>> result.prediction 0.0 >>> numpy.argmax(result.probability) 0 >>> numpy.argmax(result.newRawPrediction) 0 >>> result.leafId DenseVector([0.0, 0.0, 0.0]) >>> test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"]) >>> model.transform(test1).head().prediction 1.0 >>> model.trees [DecisionTreeClassificationModel...depth=..., DecisionTreeClassificationModel...] >>> rfc_path = temp_path + "/rfc" >>> rf.save(rfc_path) >>> rf2 = RandomForestClassifier.load(rfc_path) >>> rf2.getNumTrees() 3 >>> model_path = temp_path + "/rfc_model" >>> model.save(model_path) >>> model2 = RandomForestClassificationModel.load(model_path) >>> model.featureImportances == model2.featureImportances True >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True """
probabilityCol="probability", rawPredictionCol="rawPrediction", maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", numTrees=20, featureSubsetStrategy="auto", seed=None, subsamplingRate=1.0, leafCol="", minWeightFractionPerNode=0.0, weightCol=None, bootstrap=True): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, impurity="gini", \ numTrees=20, featureSubsetStrategy="auto", seed=None, subsamplingRate=1.0, \ leafCol="", minWeightFractionPerNode=0.0, weightCol=None, bootstrap=True) """ "org.apache.spark.ml.classification.RandomForestClassifier", self.uid)
probabilityCol="probability", rawPredictionCol="rawPrediction", maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, seed=None, impurity="gini", numTrees=20, featureSubsetStrategy="auto", subsamplingRate=1.0, leafCol="", minWeightFractionPerNode=0.0, weightCol=None, bootstrap=True): """ setParams(self, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, seed=None, \ impurity="gini", numTrees=20, featureSubsetStrategy="auto", subsamplingRate=1.0, \ leafCol="", minWeightFractionPerNode=0.0, weightCol=None, bootstrap=True) Sets params for linear classification. """
""" Sets the value of :py:attr:`maxDepth`. """ return self._set(maxDepth=value)
""" Sets the value of :py:attr:`maxBins`. """ return self._set(maxBins=value)
""" Sets the value of :py:attr:`minInstancesPerNode`. """ return self._set(minInstancesPerNode=value)
""" Sets the value of :py:attr:`minInfoGain`. """ return self._set(minInfoGain=value)
""" Sets the value of :py:attr:`maxMemoryInMB`. """ return self._set(maxMemoryInMB=value)
""" Sets the value of :py:attr:`cacheNodeIds`. """ return self._set(cacheNodeIds=value)
def setImpurity(self, value): """ Sets the value of :py:attr:`impurity`. """ return self._set(impurity=value)
def setNumTrees(self, value): """ Sets the value of :py:attr:`numTrees`. """ return self._set(numTrees=value)
def setBootstrap(self, value): """ Sets the value of :py:attr:`bootstrap`. """ return self._set(bootstrap=value)
def setSubsamplingRate(self, value): """ Sets the value of :py:attr:`subsamplingRate`. """ return self._set(subsamplingRate=value)
def setFeatureSubsetStrategy(self, value): """ Sets the value of :py:attr:`featureSubsetStrategy`. """ return self._set(featureSubsetStrategy=value)
""" Sets the value of :py:attr:`seed`. """ return self._set(seed=value)
""" Sets the value of :py:attr:`checkpointInterval`. """ return self._set(checkpointInterval=value)
def setWeightCol(self, value): """ Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
def setMinWeightFractionPerNode(self, value): """ Sets the value of :py:attr:`minWeightFractionPerNode`. """ return self._set(minWeightFractionPerNode=value)
_RandomForestClassifierParams, JavaMLWritable, JavaMLReadable, HasTrainingSummary): """ Model fitted by RandomForestClassifier.
.. versionadded:: 1.4.0 """
def featureImportances(self): """ Estimate of the importance of each feature.
Each feature's importance is the average of its importance across all trees in the ensemble The importance vector is normalized to sum to 1. This method is suggested by Hastie et al. (Hastie, Tibshirani, Friedman. "The Elements of Statistical Learning, 2nd Edition." 2001.) and follows the implementation from scikit-learn.
.. versionadded:: 2.0.0
See Also -------- DecisionTreeClassificationModel.featureImportances """
def trees(self): """Trees in this ensemble. Warning: These have null parent Estimators."""
def summary(self): """ Gets summary (accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ super(RandomForestClassificationModel, self).summary) else: super(RandomForestClassificationModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
""" Evaluates the model on a test dataset.
.. versionadded:: 3.1.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """ raise TypeError("dataset must be a DataFrame but got %s." % type(dataset)) else:
""" Abstraction for RandomForestClassification Results for a given model.
.. versionadded:: 3.1.0 """
_TrainingSummary): """ Abstraction for RandomForestClassificationTraining Training results.
.. versionadded:: 3.1.0 """
""" BinaryRandomForestClassification results for a given model.
.. versionadded:: 3.1.0 """
RandomForestClassificationTrainingSummary): """ BinaryRandomForestClassification training results for a given model.
.. versionadded:: 3.1.0 """
""" Params for :py:class:`GBTClassifier` and :py:class:`GBTClassifierModel`.
.. versionadded:: 3.0.0 """
"Loss function which GBT tries to minimize (case-insensitive). " + "Supported options: " + ", ".join(supportedLossTypes), typeConverter=TypeConverters.toString)
maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, lossType="logistic", maxIter=20, stepSize=0.1, subsamplingRate=1.0, impurity="variance", featureSubsetStrategy="all", validationTol=0.01, leafCol="", minWeightFractionPerNode=0.0)
def getLossType(self): """ Gets the value of lossType or its default value. """ return self.getOrDefault(self.lossType)
JavaMLWritable, JavaMLReadable): """ `Gradient-Boosted Trees (GBTs) <http://en.wikipedia.org/wiki/Gradient_boosting>`_ learning algorithm for classification. It supports binary labels, as well as both continuous and categorical features.
.. versionadded:: 1.4.0
Notes ----- Multiclass labels are not currently supported.
The implementation is based upon: J.H. Friedman. "Stochastic Gradient Boosting." 1999.
Gradient Boosting vs. TreeBoost:
- This implementation is for Stochastic Gradient Boosting, not for TreeBoost. - Both algorithms learn tree ensembles by minimizing loss functions. - TreeBoost (Friedman, 1999) additionally modifies the outputs at tree leaf nodes based on the loss function, whereas the original gradient boosting method does not. - We expect to implement TreeBoost in the future: `SPARK-4240 <https://issues.apache.org/jira/browse/SPARK-4240>`_
Examples -------- >>> from numpy import allclose >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.feature import StringIndexer >>> df = spark.createDataFrame([ ... (1.0, Vectors.dense(1.0)), ... (0.0, Vectors.sparse(1, [], []))], ["label", "features"]) >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed") >>> si_model = stringIndexer.fit(df) >>> td = si_model.transform(df) >>> gbt = GBTClassifier(maxIter=5, maxDepth=2, labelCol="indexed", seed=42, ... leafCol="leafId") >>> gbt.setMaxIter(5) GBTClassifier... >>> gbt.setMinWeightFractionPerNode(0.049) GBTClassifier... >>> gbt.getMaxIter() 5 >>> gbt.getFeatureSubsetStrategy() 'all' >>> model = gbt.fit(td) >>> model.getLabelCol() 'indexed' >>> model.setFeaturesCol("features") GBTClassificationModel... >>> model.setThresholds([0.3, 0.7]) GBTClassificationModel... >>> model.getThresholds() [0.3, 0.7] >>> model.featureImportances SparseVector(1, {0: 1.0}) >>> allclose(model.treeWeights, [1.0, 0.1, 0.1, 0.1, 0.1]) True >>> test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"]) >>> model.predict(test0.head().features) 0.0 >>> model.predictRaw(test0.head().features) DenseVector([1.1697, -1.1697]) >>> model.predictProbability(test0.head().features) DenseVector([0.9121, 0.0879]) >>> result = model.transform(test0).head() >>> result.prediction 0.0 >>> result.leafId DenseVector([0.0, 0.0, 0.0, 0.0, 0.0]) >>> test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"]) >>> model.transform(test1).head().prediction 1.0 >>> model.totalNumNodes 15 >>> print(model.toDebugString) GBTClassificationModel...numTrees=5... >>> gbtc_path = temp_path + "gbtc" >>> gbt.save(gbtc_path) >>> gbt2 = GBTClassifier.load(gbtc_path) >>> gbt2.getMaxDepth() 2 >>> model_path = temp_path + "gbtc_model" >>> model.save(model_path) >>> model2 = GBTClassificationModel.load(model_path) >>> model.featureImportances == model2.featureImportances True >>> model.treeWeights == model2.treeWeights True >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True >>> model.trees [DecisionTreeRegressionModel...depth=..., DecisionTreeRegressionModel...] >>> validation = spark.createDataFrame([(0.0, Vectors.dense(-1.0),)], ... ["indexed", "features"]) >>> model.evaluateEachIteration(validation) [0.25..., 0.23..., 0.21..., 0.19..., 0.18...] >>> model.numClasses 2 >>> gbt = gbt.setValidationIndicatorCol("validationIndicator") >>> gbt.getValidationIndicatorCol() 'validationIndicator' >>> gbt.getValidationTol() 0.01 """
maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, impurity="variance", featureSubsetStrategy="all", validationTol=0.01, validationIndicatorCol=None, leafCol="", minWeightFractionPerNode=0.0, weightCol=None): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, \ lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, \ impurity="variance", featureSubsetStrategy="all", validationTol=0.01, \ validationIndicatorCol=None, leafCol="", minWeightFractionPerNode=0.0, \ weightCol=None) """ "org.apache.spark.ml.classification.GBTClassifier", self.uid)
maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, impurity="variance", featureSubsetStrategy="all", validationTol=0.01, validationIndicatorCol=None, leafCol="", minWeightFractionPerNode=0.0, weightCol=None): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, \ lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, \ impurity="variance", featureSubsetStrategy="all", validationTol=0.01, \ validationIndicatorCol=None, leafCol="", minWeightFractionPerNode=0.0, \ weightCol=None) Sets params for Gradient Boosted Tree Classification. """
""" Sets the value of :py:attr:`maxDepth`. """ return self._set(maxDepth=value)
""" Sets the value of :py:attr:`maxBins`. """ return self._set(maxBins=value)
""" Sets the value of :py:attr:`minInstancesPerNode`. """ return self._set(minInstancesPerNode=value)
""" Sets the value of :py:attr:`minInfoGain`. """ return self._set(minInfoGain=value)
""" Sets the value of :py:attr:`maxMemoryInMB`. """ return self._set(maxMemoryInMB=value)
""" Sets the value of :py:attr:`cacheNodeIds`. """ return self._set(cacheNodeIds=value)
def setImpurity(self, value): """ Sets the value of :py:attr:`impurity`. """ return self._set(impurity=value)
def setLossType(self, value): """ Sets the value of :py:attr:`lossType`. """ return self._set(lossType=value)
def setSubsamplingRate(self, value): """ Sets the value of :py:attr:`subsamplingRate`. """ return self._set(subsamplingRate=value)
def setFeatureSubsetStrategy(self, value): """ Sets the value of :py:attr:`featureSubsetStrategy`. """ return self._set(featureSubsetStrategy=value)
def setValidationIndicatorCol(self, value): """ Sets the value of :py:attr:`validationIndicatorCol`. """
def setMaxIter(self, value): """ Sets the value of :py:attr:`maxIter`. """
def setCheckpointInterval(self, value): """ Sets the value of :py:attr:`checkpointInterval`. """ return self._set(checkpointInterval=value)
def setSeed(self, value): """ Sets the value of :py:attr:`seed`. """ return self._set(seed=value)
def setStepSize(self, value): """ Sets the value of :py:attr:`stepSize`. """ return self._set(stepSize=value)
def setWeightCol(self, value): """ Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
def setMinWeightFractionPerNode(self, value): """ Sets the value of :py:attr:`minWeightFractionPerNode`. """
_GBTClassifierParams, JavaMLWritable, JavaMLReadable): """ Model fitted by GBTClassifier.
.. versionadded:: 1.4.0 """
def featureImportances(self): """ Estimate of the importance of each feature.
Each feature's importance is the average of its importance across all trees in the ensemble The importance vector is normalized to sum to 1. This method is suggested by Hastie et al. (Hastie, Tibshirani, Friedman. "The Elements of Statistical Learning, 2nd Edition." 2001.) and follows the implementation from scikit-learn.
.. versionadded:: 2.0.0
See Also -------- DecisionTreeClassificationModel.featureImportances """
def trees(self): """Trees in this ensemble. Warning: These have null parent Estimators."""
""" Method to compute error or loss for every iteration of gradient boosting.
.. versionadded:: 2.4.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """
""" Params for :py:class:`NaiveBayes` and :py:class:`NaiveBayesModel`.
.. versionadded:: 3.0.0 """
"default is 1.0", typeConverter=TypeConverters.toFloat) "(case-sensitive). Supported options: multinomial (default), bernoulli " + "and gaussian.", typeConverter=TypeConverters.toString)
def getSmoothing(self): """ Gets the value of smoothing or its default value. """
def getModelType(self): """ Gets the value of modelType or its default value. """
JavaMLWritable, JavaMLReadable): """ Naive Bayes Classifiers. It supports both Multinomial and Bernoulli NB. `Multinomial NB \ <http://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html>`_ can handle finitely supported discrete data. For example, by converting documents into TF-IDF vectors, it can be used for document classification. By making every vector a binary (0/1) data, it can also be used as `Bernoulli NB \ <http://nlp.stanford.edu/IR-book/html/htmledition/the-bernoulli-model-1.html>`_.
The input feature values for Multinomial NB and Bernoulli NB must be nonnegative. Since 3.0.0, it supports Complement NB which is an adaptation of the Multinomial NB. Specifically, Complement NB uses statistics from the complement of each class to compute the model's coefficients. The inventors of Complement NB show empirically that the parameter estimates for CNB are more stable than those for Multinomial NB. Like Multinomial NB, the input feature values for Complement NB must be nonnegative. Since 3.0.0, it also supports `Gaussian NB \ <https://en.wikipedia.org/wiki/Naive_Bayes_classifier#Gaussian_naive_Bayes>`_. which can handle continuous data.
.. versionadded:: 1.5.0
Examples -------- >>> from pyspark.sql import Row >>> from pyspark.ml.linalg import Vectors >>> df = spark.createDataFrame([ ... Row(label=0.0, weight=0.1, features=Vectors.dense([0.0, 0.0])), ... Row(label=0.0, weight=0.5, features=Vectors.dense([0.0, 1.0])), ... Row(label=1.0, weight=1.0, features=Vectors.dense([1.0, 0.0]))]) >>> nb = NaiveBayes(smoothing=1.0, modelType="multinomial", weightCol="weight") >>> model = nb.fit(df) >>> model.setFeaturesCol("features") NaiveBayesModel... >>> model.getSmoothing() 1.0 >>> model.pi DenseVector([-0.81..., -0.58...]) >>> model.theta DenseMatrix(2, 2, [-0.91..., -0.51..., -0.40..., -1.09...], 1) >>> model.sigma DenseMatrix(0, 0, [...], ...) >>> test0 = sc.parallelize([Row(features=Vectors.dense([1.0, 0.0]))]).toDF() >>> model.predict(test0.head().features) 1.0 >>> model.predictRaw(test0.head().features) DenseVector([-1.72..., -0.99...]) >>> model.predictProbability(test0.head().features) DenseVector([0.32..., 0.67...]) >>> result = model.transform(test0).head() >>> result.prediction 1.0 >>> result.probability DenseVector([0.32..., 0.67...]) >>> result.rawPrediction DenseVector([-1.72..., -0.99...]) >>> test1 = sc.parallelize([Row(features=Vectors.sparse(2, [0], [1.0]))]).toDF() >>> model.transform(test1).head().prediction 1.0 >>> nb_path = temp_path + "/nb" >>> nb.save(nb_path) >>> nb2 = NaiveBayes.load(nb_path) >>> nb2.getSmoothing() 1.0 >>> model_path = temp_path + "/nb_model" >>> model.save(model_path) >>> model2 = NaiveBayesModel.load(model_path) >>> model.pi == model2.pi True >>> model.theta == model2.theta True >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True >>> nb = nb.setThresholds([0.01, 10.00]) >>> model3 = nb.fit(df) >>> result = model3.transform(test0).head() >>> result.prediction 0.0 >>> nb3 = NaiveBayes().setModelType("gaussian") >>> model4 = nb3.fit(df) >>> model4.getModelType() 'gaussian' >>> model4.sigma DenseMatrix(2, 2, [0.0, 0.25, 0.0, 0.0], 1) >>> nb5 = NaiveBayes(smoothing=1.0, modelType="complement", weightCol="weight") >>> model5 = nb5.fit(df) >>> model5.getModelType() 'complement' >>> model5.theta DenseMatrix(2, 2, [...], 1) >>> model5.sigma DenseMatrix(0, 0, [...], ...) """
probabilityCol="probability", rawPredictionCol="rawPrediction", smoothing=1.0, modelType="multinomial", thresholds=None, weightCol=None): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", smoothing=1.0, \ modelType="multinomial", thresholds=None, weightCol=None) """ "org.apache.spark.ml.classification.NaiveBayes", self.uid)
probabilityCol="probability", rawPredictionCol="rawPrediction", smoothing=1.0, modelType="multinomial", thresholds=None, weightCol=None): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", smoothing=1.0, \ modelType="multinomial", thresholds=None, weightCol=None) Sets params for Naive Bayes. """
def setSmoothing(self, value): """ Sets the value of :py:attr:`smoothing`. """ return self._set(smoothing=value)
def setModelType(self, value): """ Sets the value of :py:attr:`modelType`. """
""" Sets the value of :py:attr:`weightCol`. """ return self._set(weightCol=value)
JavaMLReadable): """ Model fitted by NaiveBayes.
.. versionadded:: 1.5.0 """
def pi(self): """ log of class priors. """
def theta(self): """ log of class conditional probabilities. """
def sigma(self): """ variance of each feature. """
HasTol, HasStepSize, HasSolver, HasBlockSize): """ Params for :py:class:`MultilayerPerceptronClassifier`.
.. versionadded:: 3.0.0 """
"E.g., Array(780, 100, 10) means 780 inputs, one hidden layer with 100 " + "neurons and output layer of 10 neurons.", typeConverter=TypeConverters.toListInt) "options: l-bfgs, gd.", typeConverter=TypeConverters.toString) typeConverter=TypeConverters.toVector)
def getLayers(self): """ Gets the value of layers or its default value. """
def getInitialWeights(self): """ Gets the value of initialWeights or its default value. """ return self.getOrDefault(self.initialWeights)
JavaMLWritable, JavaMLReadable): """ Classifier trainer based on the Multilayer Perceptron. Each layer has sigmoid activation function, output layer has softmax. Number of inputs has to be equal to the size of feature vectors. Number of outputs has to be equal to the total number of labels.
.. versionadded:: 1.6.0
Examples -------- >>> from pyspark.ml.linalg import Vectors >>> df = spark.createDataFrame([ ... (0.0, Vectors.dense([0.0, 0.0])), ... (1.0, Vectors.dense([0.0, 1.0])), ... (1.0, Vectors.dense([1.0, 0.0])), ... (0.0, Vectors.dense([1.0, 1.0]))], ["label", "features"]) >>> mlp = MultilayerPerceptronClassifier(layers=[2, 2, 2], seed=123) >>> mlp.setMaxIter(100) MultilayerPerceptronClassifier... >>> mlp.getMaxIter() 100 >>> mlp.getBlockSize() 128 >>> mlp.setBlockSize(1) MultilayerPerceptronClassifier... >>> mlp.getBlockSize() 1 >>> model = mlp.fit(df) >>> model.setFeaturesCol("features") MultilayerPerceptronClassificationModel... >>> model.getMaxIter() 100 >>> model.getLayers() [2, 2, 2] >>> model.weights.size 12 >>> testDF = spark.createDataFrame([ ... (Vectors.dense([1.0, 0.0]),), ... (Vectors.dense([0.0, 0.0]),)], ["features"]) >>> model.predict(testDF.head().features) 1.0 >>> model.predictRaw(testDF.head().features) DenseVector([-16.208, 16.344]) >>> model.predictProbability(testDF.head().features) DenseVector([0.0, 1.0]) >>> model.transform(testDF).select("features", "prediction").show() +---------+----------+ | features|prediction| +---------+----------+ |[1.0,0.0]| 1.0| |[0.0,0.0]| 0.0| +---------+----------+ ... >>> mlp_path = temp_path + "/mlp" >>> mlp.save(mlp_path) >>> mlp2 = MultilayerPerceptronClassifier.load(mlp_path) >>> mlp2.getBlockSize() 1 >>> model_path = temp_path + "/mlp_model" >>> model.save(model_path) >>> model2 = MultilayerPerceptronClassificationModel.load(model_path) >>> model.getLayers() == model2.getLayers() True >>> model.weights == model2.weights True >>> model.transform(testDF).take(1) == model2.transform(testDF).take(1) True >>> mlp2 = mlp2.setInitialWeights(list(range(0, 12))) >>> model3 = mlp2.fit(df) >>> model3.weights != model2.weights True >>> model3.getLayers() == model.getLayers() True """
maxIter=100, tol=1e-6, seed=None, layers=None, blockSize=128, stepSize=0.03, solver="l-bfgs", initialWeights=None, probabilityCol="probability", rawPredictionCol="rawPrediction"): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, tol=1e-6, seed=None, layers=None, blockSize=128, stepSize=0.03, \ solver="l-bfgs", initialWeights=None, probabilityCol="probability", \ rawPredictionCol="rawPrediction") """ "org.apache.spark.ml.classification.MultilayerPerceptronClassifier", self.uid)
maxIter=100, tol=1e-6, seed=None, layers=None, blockSize=128, stepSize=0.03, solver="l-bfgs", initialWeights=None, probabilityCol="probability", rawPredictionCol="rawPrediction"): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ maxIter=100, tol=1e-6, seed=None, layers=None, blockSize=128, stepSize=0.03, \ solver="l-bfgs", initialWeights=None, probabilityCol="probability", \ rawPredictionCol="rawPrediction"): Sets params for MultilayerPerceptronClassifier. """
def setLayers(self, value): """ Sets the value of :py:attr:`layers`. """ return self._set(layers=value)
def setBlockSize(self, value): """ Sets the value of :py:attr:`blockSize`. """
def setInitialWeights(self, value): """ Sets the value of :py:attr:`initialWeights`. """
""" Sets the value of :py:attr:`maxIter`. """
""" Sets the value of :py:attr:`seed`. """ return self._set(seed=value)
""" Sets the value of :py:attr:`tol`. """ return self._set(tol=value)
def setStepSize(self, value): """ Sets the value of :py:attr:`stepSize`. """ return self._set(stepSize=value)
""" Sets the value of :py:attr:`solver`. """ return self._set(solver=value)
_MultilayerPerceptronParams, JavaMLWritable, JavaMLReadable, HasTrainingSummary): """ Model fitted by MultilayerPerceptronClassifier.
.. versionadded:: 1.6.0 """
def weights(self): """ the weights of layers. """
def summary(self): """ Gets summary (accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ super(MultilayerPerceptronClassificationModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
""" Evaluates the model on a test dataset.
.. versionadded:: 3.1.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """ raise TypeError("dataset must be a DataFrame but got %s." % type(dataset))
""" Abstraction for MultilayerPerceptronClassifier Results for a given model.
.. versionadded:: 3.1.0 """
_TrainingSummary): """ Abstraction for MultilayerPerceptronClassifier Training results.
.. versionadded:: 3.1.0 """
""" Params for :py:class:`OneVsRest` and :py:class:`OneVsRestModelModel`. """
def getClassifier(self): """ Gets the value of classifier or its default value. """
""" Reduction of Multiclass Classification to Binary Classification. Performs reduction using one against all strategy. For a multiclass classification with k classes, train k models (one per class). Each example is scored against all k models and the model with highest score is picked to label the example.
.. versionadded:: 2.0.0
Examples -------- >>> from pyspark.sql import Row >>> from pyspark.ml.linalg import Vectors >>> data_path = "data/mllib/sample_multiclass_classification_data.txt" >>> df = spark.read.format("libsvm").load(data_path) >>> lr = LogisticRegression(regParam=0.01) >>> ovr = OneVsRest(classifier=lr) >>> ovr.getRawPredictionCol() 'rawPrediction' >>> ovr.setPredictionCol("newPrediction") OneVsRest... >>> model = ovr.fit(df) >>> model.models[0].coefficients DenseVector([0.5..., -1.0..., 3.4..., 4.2...]) >>> model.models[1].coefficients DenseVector([-2.1..., 3.1..., -2.6..., -2.3...]) >>> model.models[2].coefficients DenseVector([0.3..., -3.4..., 1.0..., -1.1...]) >>> [x.intercept for x in model.models] [-2.7..., -2.5..., -1.3...] >>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, 0.0, 1.0, 1.0))]).toDF() >>> model.transform(test0).head().newPrediction 0.0 >>> test1 = sc.parallelize([Row(features=Vectors.sparse(4, [0], [1.0]))]).toDF() >>> model.transform(test1).head().newPrediction 2.0 >>> test2 = sc.parallelize([Row(features=Vectors.dense(0.5, 0.4, 0.3, 0.2))]).toDF() >>> model.transform(test2).head().newPrediction 0.0 >>> model_path = temp_path + "/ovr_model" >>> model.save(model_path) >>> model2 = OneVsRestModel.load(model_path) >>> model2.transform(test0).head().newPrediction 0.0 >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True >>> model.transform(test2).columns ['features', 'rawPrediction', 'newPrediction'] """
rawPredictionCol="rawPrediction", classifier=None, weightCol=None, parallelism=1): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ rawPredictionCol="rawPrediction", classifier=None, weightCol=None, parallelism=1): """
rawPredictionCol="rawPrediction", classifier=None, weightCol=None, parallelism=1): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ rawPredictionCol="rawPrediction", classifier=None, weightCol=None, parallelism=1): Sets params for OneVsRest. """ kwargs = self._input_kwargs return self._set(**kwargs)
def setClassifier(self, value): """ Sets the value of :py:attr:`classifier`. """
""" Sets the value of :py:attr:`labelCol`. """ return self._set(labelCol=value)
""" Sets the value of :py:attr:`featuresCol`. """ return self._set(featuresCol=value)
""" Sets the value of :py:attr:`predictionCol`. """
""" Sets the value of :py:attr:`rawPredictionCol`. """ return self._set(rawPredictionCol=value)
""" Sets the value of :py:attr:`weightCol`. """
""" Sets the value of :py:attr:`parallelism`. """ return self._set(parallelism=value)
else: "as it is not supported by {} now.".format(classifier))
else:
# persist if underlying dataset is not persistent. multiclassLabeled.persist(StorageLevel.MEMORY_AND_DISK)
binaryLabelCol, when(multiclassLabeled[labelCol] == float(index), 1.0).otherwise(0.0)) (classifier.featuresCol, featuresCol), (classifier.predictionCol, predictionCol)])
multiclassLabeled.unpersist()
""" Creates a copy of this instance with a randomly generated uid and some extra params. This creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over.
.. versionadded:: 2.0.0
Examples -------- extra : dict, optional Extra parameters to copy to the new instance
Returns ------- :py:class:`OneVsRest` Copy of this instance """
def _from_java(cls, java_stage): """ Given a Java OneVsRest, create and return a Python wrapper of it. Used for ML persistence. """ rawPredictionCol=rawPredictionCol, classifier=classifier, parallelism=parallelism)
""" Transfer this instance to a Java OneVsRest. Used for ML persistence.
Returns ------- py4j.java_gateway.JavaObject Java object equivalent to this instance. """ self.uid)
def read(cls):
else:
extraMetadata=extraMetadata)
def loadClassifier(path, sc):
def validateParams(instance):
raise ValueError(f'OneVsRest write will fail because it contains {elem.uid} ' f'which is not writable.')
else:
""" Model fitted by OneVsRest. This stores the models resulting from training k binary classifiers: one for each class. Each example is scored against all k models, and the model with the highest score is picked to label the example.
.. versionadded:: 2.0.0 """
""" Sets the value of :py:attr:`featuresCol`. """
""" Sets the value of :py:attr:`predictionCol`. """
""" Sets the value of :py:attr:`rawPredictionCol`. """ return self._set(rawPredictionCol=value)
# set java instance sc._gateway.jvm.org.apache.spark.ml .classification.ClassificationModel) # TODO: need to set metadata JavaParams._new_java_obj("org.apache.spark.ml.classification.OneVsRestModel", self.uid, metadata.empty(), java_models_array)
# determine the input columns: these need to be passed through
# add an accumulator column to store predictions of all the models
# persist if underlying dataset is not persistent. newDataset.persist(StorageLevel.MEMORY_AND_DISK)
# update the accumulator column with the result of prediction of models
# add temporary column to store intermediate scores and update lambda predictions, prediction: predictions + [prediction.tolist()[1]], ArrayType(DoubleType())) tmpColName, updateUDF(transformedDataset[accColName], transformedDataset[rawPredictionCol]))
# switch out the intermediate column with the accumulator column .select(*newColumns).withColumnRenamed(tmpColName, accColName)
newDataset.unpersist()
self.getRawPredictionCol(), rawPredictionUDF(aggregatedDataset[accColName]))
# output the index of the classifier with highest confidence as prediction key=operator.itemgetter(1))[0]), DoubleType()) self.getPredictionCol(), labelUDF(aggregatedDataset[accColName]))
""" Creates a copy of this instance with a randomly generated uid and some extra params. This creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over.
.. versionadded:: 2.0.0
Parameters ---------- extra : dict, optional Extra parameters to copy to the new instance
Returns ------- :py:class:`OneVsRestModel` Copy of this instance """ extra = dict()
def _from_java(cls, java_stage): """ Given a Java OneVsRestModel, create and return a Python wrapper of it. Used for ML persistence. """ .setFeaturesCol(featuresCol)
""" Transfer this instance to a Java OneVsRestModel. Used for ML persistence.
Returns ------- py4j.java_gateway.JavaObject Java object equivalent to this instance. """ java_models, sc._gateway.jvm.org.apache.spark.ml.classification.ClassificationModel) self.uid, metadata.empty(), java_models_array)
def read(cls):
[self.getClassifier()] + self.models)): else:
else:
JavaMLReadable): """ Factorization Machines learning algorithm for classification.
Solver supports:
* gd (normal mini-batch gradient descent) * adamW (default)
.. versionadded:: 3.0.0
Examples -------- >>> from pyspark.ml.linalg import Vectors >>> from pyspark.ml.classification import FMClassifier >>> df = spark.createDataFrame([ ... (1.0, Vectors.dense(1.0)), ... (0.0, Vectors.sparse(1, [], []))], ["label", "features"]) >>> fm = FMClassifier(factorSize=2) >>> fm.setSeed(11) FMClassifier... >>> model = fm.fit(df) >>> model.getMaxIter() 100 >>> test0 = spark.createDataFrame([ ... (Vectors.dense(-1.0),), ... (Vectors.dense(0.5),), ... (Vectors.dense(1.0),), ... (Vectors.dense(2.0),)], ["features"]) >>> model.predictRaw(test0.head().features) DenseVector([22.13..., -22.13...]) >>> model.predictProbability(test0.head().features) DenseVector([1.0, 0.0]) >>> model.transform(test0).select("features", "probability").show(10, False) +--------+------------------------------------------+ |features|probability | +--------+------------------------------------------+ |[-1.0] |[0.9999999997574736,2.425264676902229E-10]| |[0.5] |[0.47627851732981163,0.5237214826701884] | |[1.0] |[5.491554426243495E-4,0.9994508445573757] | |[2.0] |[2.005766663870645E-10,0.9999999997994233]| +--------+------------------------------------------+ ... >>> model.intercept -7.316665276826291 >>> model.linear DenseVector([14.8232]) >>> model.factors DenseMatrix(1, 2, [0.0163, -0.0051], 1) >>> model_path = temp_path + "/fm_model" >>> model.save(model_path) >>> model2 = FMClassificationModel.load(model_path) >>> model2.intercept -7.316665276826291 >>> model2.linear DenseVector([14.8232]) >>> model2.factors DenseMatrix(1, 2, [0.0163, -0.0051], 1) >>> model.transform(test0).take(1) == model2.transform(test0).take(1) True """
probabilityCol="probability", rawPredictionCol="rawPrediction", factorSize=8, fitIntercept=True, fitLinear=True, regParam=0.0, miniBatchFraction=1.0, initStd=0.01, maxIter=100, stepSize=1.0, tol=1e-6, solver="adamW", thresholds=None, seed=None): """ __init__(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ factorSize=8, fitIntercept=True, fitLinear=True, regParam=0.0, \ miniBatchFraction=1.0, initStd=0.01, maxIter=100, stepSize=1.0, \ tol=1e-6, solver="adamW", thresholds=None, seed=None) """ "org.apache.spark.ml.classification.FMClassifier", self.uid)
probabilityCol="probability", rawPredictionCol="rawPrediction", factorSize=8, fitIntercept=True, fitLinear=True, regParam=0.0, miniBatchFraction=1.0, initStd=0.01, maxIter=100, stepSize=1.0, tol=1e-6, solver="adamW", thresholds=None, seed=None): """ setParams(self, \\*, featuresCol="features", labelCol="label", predictionCol="prediction", \ probabilityCol="probability", rawPredictionCol="rawPrediction", \ factorSize=8, fitIntercept=True, fitLinear=True, regParam=0.0, \ miniBatchFraction=1.0, initStd=0.01, maxIter=100, stepSize=1.0, \ tol=1e-6, solver="adamW", thresholds=None, seed=None) Sets Params for FMClassifier. """
def setFactorSize(self, value): """ Sets the value of :py:attr:`factorSize`. """ return self._set(factorSize=value)
def setFitLinear(self, value): """ Sets the value of :py:attr:`fitLinear`. """ return self._set(fitLinear=value)
def setMiniBatchFraction(self, value): """ Sets the value of :py:attr:`miniBatchFraction`. """ return self._set(miniBatchFraction=value)
def setInitStd(self, value): """ Sets the value of :py:attr:`initStd`. """ return self._set(initStd=value)
def setMaxIter(self, value): """ Sets the value of :py:attr:`maxIter`. """ return self._set(maxIter=value)
def setStepSize(self, value): """ Sets the value of :py:attr:`stepSize`. """ return self._set(stepSize=value)
def setTol(self, value): """ Sets the value of :py:attr:`tol`. """ return self._set(tol=value)
def setSolver(self, value): """ Sets the value of :py:attr:`solver`. """ return self._set(solver=value)
def setSeed(self, value): """ Sets the value of :py:attr:`seed`. """
def setFitIntercept(self, value): """ Sets the value of :py:attr:`fitIntercept`. """ return self._set(fitIntercept=value)
def setRegParam(self, value): """ Sets the value of :py:attr:`regParam`. """ return self._set(regParam=value)
JavaMLWritable, JavaMLReadable, HasTrainingSummary): """ Model fitted by :class:`FMClassifier`.
.. versionadded:: 3.0.0 """
def intercept(self): """ Model intercept. """
def linear(self): """ Model linear term. """
def factors(self): """ Model factor term. """
def summary(self): """ Gets summary (accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
""" Evaluates the model on a test dataset.
.. versionadded:: 3.1.0
Parameters ---------- dataset : :py:class:`pyspark.sql.DataFrame` Test dataset to evaluate model on. """ raise TypeError("dataset must be a DataFrame but got %s." % type(dataset))
""" Abstraction for FMClassifier Results for a given model.
.. versionadded:: 3.1.0 """
""" Abstraction for FMClassifier Training results.
.. versionadded:: 3.1.0 """
# The small batch size here ensures that we see multiple batches, # even in these small test examples: .master("local[2]")\ .appName("ml.classification tests")\ .getOrCreate() finally: except OSError: pass sys.exit(-1) |