Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/scala-parallel-classification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ limitations under the License.

This is based on Classification Engine Template v0.14.0.

Please refer to http://predictionio.apache.org/templates/classification/how-to/
Please refer to https://predictionio.apache.org/templates/classification/how-to/
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class NaiveBayesAlgorithm(val ap: AlgorithmParams)

@transient lazy val logger = Logger[this.type]

override
def train(sc: SparkContext, data: PreparedData): NaiveBayesModel = {
// MLLib NaiveBayes cannot handle empty training data.
require(data.labeledPoints.take(1).nonEmpty,
Expand All @@ -47,6 +48,7 @@ class NaiveBayesAlgorithm(val ap: AlgorithmParams)
NaiveBayes.train(data.labeledPoints, ap.lambda)
}

override
def predict(model: NaiveBayesModel, query: Query): PredictedResult = {
val label = model.predict(Vectors.dense(
Array(query.attr0, query.attr1, query.attr2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ case class Precision(label: Double)
extends OptionAverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header: String = s"Precision(label = $label)"

override
def calculate(query: Query, predicted: PredictedResult, actual: ActualResult)
: Option[Double] = {
if (predicted.label == label) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PreparedData(

class Preparator extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(trainingData.labeledPoints)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RandomForestAlgorithm(val ap: RandomForestAlgorithmParams) // CHANGED
Query, PredictedResult] {

// CHANGED
override
def train(sc: SparkContext, data: PreparedData): RandomForestModel = {
// CHANGED
// Empty categoricalFeaturesInfo indicates all features are continuous.
Expand All @@ -57,6 +58,7 @@ class RandomForestAlgorithm(val ap: RandomForestAlgorithmParams) // CHANGED
ap.maxBins)
}

override
def predict(
model: RandomForestModel, // CHANGED
query: Query): PredictedResult = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class NaiveBayesAlgorithm(val ap: AlgorithmParams)

@transient lazy val logger = Logger[this.type]

override
def train(sc: SparkContext, data: PreparedData): NaiveBayesModel = {
// MLLib NaiveBayes cannot handle empty training data.
require(data.labeledPoints.take(1).nonEmpty,
Expand All @@ -47,6 +48,7 @@ class NaiveBayesAlgorithm(val ap: AlgorithmParams)
NaiveBayes.train(data.labeledPoints, ap.lambda)
}

override
def predict(model: NaiveBayesModel, query: Query): PredictedResult = {
val label = model.predict(Vectors.dense(
// MODIFIED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ case class Precision(label: Double)
extends OptionAverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header: String = s"Precision(label = $label)"

override
def calculate(query: Query, predicted: PredictedResult, actual: ActualResult)
: Option[Double] = {
if (predicted.label == label) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PreparedData(

class Preparator extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(trainingData.labeledPoints)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-parallel-ecommercerecommendation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ limitations under the License.

This is based on E-Commerce Recommendation Template v0.14.0.

Please refer to http://predictionio.apache.org/templates/ecommercerecommendation/how-to/
Please refer to https://predictionio.apache.org/templates/ecommercerecommendation/how-to/
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class ECommAlgorithm(val ap: ECommAlgorithmParams)

@transient lazy val logger = Logger[this.type]

override
def train(sc: SparkContext, data: PreparedData): ECommModel = {
require(!data.viewEvents.take(1).isEmpty,
s"viewEvents in PreparedData cannot be empty." +
Expand Down Expand Up @@ -239,6 +240,7 @@ class ECommAlgorithm(val ap: ECommAlgorithmParams)
buyCountsRDD.collectAsMap.toMap
}

override
def predict(model: ECommModel, query: Query): PredictedResult = {

val userFeatures = model.userFeatures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
class Preparator
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(
users = trainingData.users,
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-parallel-recommendation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ limitations under the License.

This is based on Recommendation Template v0.14.0.

Please refer to http://predictionio.apache.org/templates/recommendation/how-to/
Please refer to https://predictionio.apache.org/templates/recommendation/how-to/
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
s"To remedy it, set lower numIterations or checkpoint parameters.")
}

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
// MLLib ALS cannot handle empty training data.
require(!data.ratings.take(1).isEmpty,
Expand Down Expand Up @@ -92,6 +93,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
itemStringIntMap = itemStringIntMap)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {
// Convert String ID to Int index for Mllib
model.userStringIntMap.get(query.user).map { userInt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ALSModel(
scored.top(num)(Ordering.by(_._2))
}

override
def save(id: String, params: ALSAlgorithmParams,
sc: SparkContext): Boolean = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class PrecisionAtK(k: Int, ratingThreshold: Double = 2.0)

override def header = s"Precision@K (k=$k, threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Option[Double] = {
val positives: Set[String] = a.ratings.filter(_.rating >= ratingThreshold).map(_.item).toSet

Expand All @@ -53,6 +54,7 @@ case class PositiveCount(ratingThreshold: Double = 2.0)
extends AverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header = s"PositiveCount (threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Double = {
a.ratings.filter(_.rating >= ratingThreshold).size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
class Preparator
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(ratings = trainingData.ratings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
s"To remedy it, set lower numIterations or checkpoint parameters.")
}

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
// MLLib ALS cannot handle empty training data.
require(!data.ratings.take(1).isEmpty,
Expand Down Expand Up @@ -92,6 +93,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
itemStringIntMap = itemStringIntMap)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {
// Convert String ID to Int index for Mllib
model.userStringIntMap.get(query.user).map { userInt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ALSModel(
extends MatrixFactorizationModel(rank, userFeatures, productFeatures)
with PersistentModel[ALSAlgorithmParams] {

override
def save(id: String, params: ALSAlgorithmParams,
sc: SparkContext): Boolean = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class PrecisionAtK(k: Int, ratingThreshold: Double = 2.0)

override def header = s"Precision@K (k=$k, threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Option[Double] = {
val positives: Set[String] = a.ratings.filter(_.rating >= ratingThreshold).map(_.item).toSet

Expand All @@ -53,6 +54,7 @@ case class PositiveCount(ratingThreshold: Double = 2.0)
extends AverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header = s"PositiveCount (threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Double = {
a.ratings.filter(_.rating >= ratingThreshold).size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ case class CustomPreparatorParams(
class Preparator(pp: CustomPreparatorParams) // ADDED CustomPreparatorParams
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
val noTrainItems = Source.fromFile(pp.filepath).getLines.toSet // CHANGED
val ratings = trainingData.ratings.filter( r =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
s"To remedy it, set lower numIterations or checkpoint parameters.")
}

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
// MLLib ALS cannot handle empty training data.
require(!data.ratings.take(1).isEmpty,
Expand Down Expand Up @@ -92,6 +93,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
itemStringIntMap = itemStringIntMap)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {
// Convert String ID to Int index for Mllib
model.userStringIntMap.get(query.user).map { userInt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ALSModel(
extends MatrixFactorizationModel(rank, userFeatures, productFeatures)
with PersistentModel[ALSAlgorithmParams] {

override
def save(id: String, params: ALSAlgorithmParams,
sc: SparkContext): Boolean = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class PrecisionAtK(k: Int, ratingThreshold: Double = 2.0)

override def header = s"Precision@K (k=$k, threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Option[Double] = {
val positives: Set[String] = a.ratings.filter(_.rating >= ratingThreshold).map(_.item).toSet

Expand All @@ -53,6 +54,7 @@ case class PositiveCount(ratingThreshold: Double = 2.0)
extends AverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header = s"PositiveCount (threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Double = {
a.ratings.filter(_.rating >= ratingThreshold).size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
class Preparator
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(ratings = trainingData.ratings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
s"To remedy it, set lower numIterations or checkpoint parameters.")
}

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
// MLLib ALS cannot handle empty training data.
require(!data.ratings.take(1).isEmpty,
Expand Down Expand Up @@ -92,6 +93,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
itemStringIntMap = itemStringIntMap)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {
// Convert String ID to Int index for Mllib
model.userStringIntMap.get(query.user).map { userInt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ALSModel(
extends MatrixFactorizationModel(rank, userFeatures, productFeatures)
with PersistentModel[ALSAlgorithmParams] {

override
def save(id: String, params: ALSAlgorithmParams,
sc: SparkContext): Boolean = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class PrecisionAtK(k: Int, ratingThreshold: Double = 2.0)

override def header = s"Precision@K (k=$k, threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Option[Double] = {
val positives: Set[String] = a.ratings.filter(_.rating >= ratingThreshold).map(_.item).toSet

Expand All @@ -53,6 +54,7 @@ case class PositiveCount(ratingThreshold: Double = 2.0)
extends AverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header = s"PositiveCount (threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Double = {
a.ratings.filter(_.rating >= ratingThreshold).size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
class Preparator
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(ratings = trainingData.ratings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
s"To remedy it, set lower numIterations or checkpoint parameters.")
}

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
// MLLib ALS cannot handle empty training data.
require(!data.ratings.take(1).isEmpty,
Expand Down Expand Up @@ -93,6 +94,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
itemStringIntMap = itemStringIntMap)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {
// Convert String ID to Int index for Mllib
model.userStringIntMap.get(query.user).map { userInt =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ALSModel(
extends MatrixFactorizationModel(rank, userFeatures, productFeatures)
with PersistentModel[ALSAlgorithmParams] {

override
def save(id: String, params: ALSAlgorithmParams,
sc: SparkContext): Boolean = {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class PrecisionAtK(k: Int, ratingThreshold: Double = 2.0)

override def header = s"Precision@K (k=$k, threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Option[Double] = {
val positives: Set[String] = a.ratings.filter(_.rating >= ratingThreshold).map(_.item).toSet

Expand All @@ -53,6 +54,7 @@ case class PositiveCount(ratingThreshold: Double = 2.0)
extends AverageMetric[EmptyEvaluationInfo, Query, PredictedResult, ActualResult] {
override def header = s"PositiveCount (threshold=$ratingThreshold)"

override
def calculate(q: Query, p: PredictedResult, a: ActualResult): Double = {
a.ratings.filter(_.rating >= ratingThreshold).size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
class Preparator
extends PPreparator[TrainingData, PreparedData] {

override
def prepare(sc: SparkContext, trainingData: TrainingData): PreparedData = {
new PreparedData(ratings = trainingData.ratings)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-parallel-similarproduct/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ limitations under the License.

This is based on Similar Product Template v0.14.0.

Please refer to http://predictionio.apache.org/templates/similarproduct/how-to/
Please refer to https://predictionio.apache.org/templates/similarproduct/how-to/
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)

@transient lazy val logger = Logger[this.type]

override
def train(sc: SparkContext, data: PreparedData): ALSModel = {
require(!data.viewEvents.take(1).isEmpty,
s"viewEvents in PreparedData cannot be empty." +
Expand Down Expand Up @@ -133,6 +134,7 @@ class ALSAlgorithm(val ap: ALSAlgorithmParams)
)
}

override
def predict(model: ALSModel, query: Query): PredictedResult = {

val productFeatures = model.productFeatures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CooccurrenceModel(
class CooccurrenceAlgorithm(val ap: CooccurrenceAlgorithmParams)
extends P2LAlgorithm[PreparedData, CooccurrenceModel, Query, PredictedResult] {

override
def train(sc: SparkContext, data: PreparedData): CooccurrenceModel = {

val itemStringIntMap = BiMap.stringInt(data.items.keys)
Expand Down Expand Up @@ -103,6 +104,7 @@ class CooccurrenceAlgorithm(val ap: CooccurrenceAlgorithmParams)
topCooccurrences
}

override
def predict(model: CooccurrenceModel, query: Query): PredictedResult = {

// convert items to Int index
Expand Down
Loading