楼主: lion003
10618 50

Scala for Machine Learning [推广有奖]

  • 3关注
  • 0粉丝

硕士生

16%

还不是VIP/贵宾

-

威望
0
论坛币
1746 个
通用积分
2.3700
学术水平
24 点
热心指数
25 点
信用等级
18 点
经验
2108 点
帖子
11
精华
1
在线时间
145 小时
注册时间
2009-9-22
最后登录
2024-9-19

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容


















二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Learning earning machine SCALA Learn

已有 5 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
guo.bailing + 100 精彩帖子
accumulation + 100 + 1 + 1 + 1 精彩帖子
Lisrelchen + 100 + 1 + 1 + 1 精彩帖子
aclyang + 20 精彩帖子
Nicolle + 100 + 100 + 1 + 1 + 1 精彩帖子

总评分: 经验 + 400  论坛币 + 120  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

本帖被以下文库推荐

沙发
jiangqing001 发表于 2015-1-11 09:04:24 |只看作者 |坛友微信交流群
  1. package org.scalaml.supervised.bayes

  2. import NaiveBayesModel._

  3.                 /**
  4.                  * Implements the binomial (or 2-class) Naive Bayes model with a likelihood for positive and
  5.                  * negative outcome and for a specific density function.
  6.                  * @constructor Instantiation of a Binomial Naive Bayes model.
  7.                  * @tparam T type of features in each observation
  8.                  * @see org.scalaml.supervised.bayes.NaiveBayesModel
  9.                  * @param pos  Priors for the class of positive outcomes.
  10.                  * @param neg  Priors for the class of negatives outcomes.
  11.                  * @author Patrick Nicolas
  12.                  * @since 0.98 February 11, 2014
  13.                  * @version 0.98.1
  14.                  * @see Scala for Machine Learning Chapter 5 "Naive Bayes Models" / Naive Bayes Classifiers
  15.                  */
  16. protected class BinNaiveBayesModel[T <: AnyVal](
  17.                 pos: Likelihood[T],
  18.                 neg: Likelihood[T])(implicit f: T => Double) extends NaiveBayesModel[T] {

  19.                 /**
  20.                  * Classify a new observation (features vector) using the Binomial Naive Bayes model.
  21.                  * @param x new observation
  22.                  * @return 1 if the observation belongs to the positive class, 0 otherwise
  23.                  * @throws IllegalArgumentException if any of the observation is undefined.
  24.                  */
  25.         override def classify(x: Array[T], logDensity: LogDensity): Int = {
  26.                 require( x.length > 0,
  27.                                 "BinNaiveBayesModel.classify Undefined observations")
  28.                
  29.                    // Simply select one of the two classes with the
  30.                          // highest log posterior probability
  31.                 if (pos.score(x, logDensity) > neg.score(x, logDensity)) 1 else 0
  32.         }

  33.        
  34.         override def likelihoods: Seq[Likelihood[T]] = List[Likelihood[T]](pos, neg)
  35.        
  36.                        
  37.         override def toString(labels: Array[String]): String = {
  38.                 require( labels.length > 0, "BinNaiveBayesModel.toString Undefined labels")
  39.                 s"\nPositive class\n${pos.toString(labels)}\nNegative class\n${neg.toString(labels)}"
  40.         }
  41.        
  42.         override def toString: String =
  43.                         s"\nPositive\n${pos.toString}\nNegative\n${neg.toString}"
  44. }


  45.                 /**
  46.                  * Companion object for the Binomial Naive Bayes Model. This singleton is used
  47.                  * to define the constructor of the BinNaiveBayesModel class.
  48.                  *                  
  49.                  * @author Patrick Nicolas
  50.                  * @since 0.98 February 11, 2014
  51.                  * @version 0.98
  52.                  * @see Scala for Machine Learning  Chapter 5 "Naive Bayes Models" / Naive Bayes Classifiers
  53.                  */
  54. object BinNaiveBayesModel {
  55.                 /**
  56.                  * Default constructor for the binary Naive Bayes model as instance of BinNaiveBayesModel
  57.                  * @param pos  Priors for the class of positive outcomes.
  58.                  * @param neg  Priors for the class of negatives outcomes.
  59.                  */
  60.         def apply[T <: AnyVal](pos: Likelihood[T], neg: Likelihood[T])
  61.                         (implicit f: T => Double): BinNaiveBayesModel[T] =
  62.                 new BinNaiveBayesModel(pos, neg)
  63. }
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

藤椅
smartchen 发表于 2015-1-24 02:28:43 |只看作者 |坛友微信交流群
  1. package org.scalaml.supervised.bayes

  2. import org.scalaml.core.Types.ScalaMl._
  3. import org.scalaml.util.FormatUtils
  4. import NaiveBayesModel._, Likelihood._, FormatUtils._

  5.                 /**
  6.                  * Class that represents a likelihood for each feature for Naive Bayes classifier.
  7.                  *
  8.                  * The prior consists of a label (index), the mean of the prior of each dimension of the model,
  9.                  * the standard deviation of the prior of each dimension of the model and the class likeliHood.
  10.                  *
  11.                  * The Naive Bayes assume that the dimension of the model are independent, making the log of
  12.                  * the prior additive.
  13.                  * @tparam T type of features or variable in observations
  14.                  * @constructor Create a likelihood for a specific class.
  15.                  * @throws IllegalArgumentException if the array of mean and standard deviation of the
  16.                  * likelihood is undefined or if the class likelihood (prior) is out of range ]0,1]
  17.                  * @param label  Name or label of the class or prior for which the likelihood is computed.
  18.                  * @param muSigma Vector of tuples (mean, standard deviation) of the prior observations for
  19.                  * the model
  20.                  * @param prior  Probability of occurrence for the class specified by the label.
  21.                  *
  22.                  * @author Patrick Nicolas
  23.                  * @since 0.98 March 11, 2014
  24.                  * @version 0.98.3
  25.                  * @see Scala for Machine Learning Chapter 5 "Naive Bayes Models"
  26.                  */
  27. protected class Likelihood[T <: AnyVal](
  28.             val label: Int,
  29.             val muSigma: Vector[DblPair],
  30.             val prior: Double)(implicit f: T => Double) {
  31.   
  32.         check(muSigma, prior)
  33.   
  34.                 /**
  35.                  * Compute the log p(C|x of log of the conditional probability of the class given an
  36.                  * observation, obs and a probability density distribution
  37.                  *
  38.                  * The default density probability function is Normal(0, 1)
  39.                  * @param obs parameterized observation
  40.                  * @param logDensity Logarithm formulation of the probability density function (default Gauss)
  41.                  * @throws IllegalArgumentException if the observations are undefined
  42.                  * @return log of the conditional probability p(C|x)
  43.                  */
  44.         final def score(obs: Array[T], logDensity: LogDensity): Double = {
  45.                 require( obs.length > 0, "Likelihood.score Undefined observations")
  46.                
  47.                         // Compute the Log of sum of the likelihood and the class prior probability
  48.                         // The log likelihood is computed by adding the log of the density for each dimension.
  49.                         // Sum {log p(xi|C) }
  50.                 (obs, muSigma).zipped
  51.                                 .map{ case(x, (mu, sig)) => (x, mu, sig)}./:(0.0)((post, entry) => {
  52.                                   
  53.                                         val mean = entry._2                // mean
  54.                                         val stdDev = entry._3        // standard deviation
  55.                                         val likelihood = logDensity(mean, stdDev, entry._1)

  56.                                                 // Avoid large value by setting a minimum value for the density probability
  57.                                         post + Math.log(if(likelihood <  MINLOGARG) MINLOGVALUE else likelihood)
  58.                 }) + Math.log(prior) // Add the class likelihood p(C)
  59.         }
  60.        
  61.                 /**
  62.                  * Display the content of this Likelihood class with associated labels.
  63.                  * @param labels Label of variables used to display content
  64.                  */
  65.         def toString(labels: Array[String]): String = {
  66.                 import org.scalaml.core.Types.ScalaMl
  67.                
  68.                 val _muSigma: Vector[(Double, Double)] =
  69.                                 muSigma.map(musig => (musig._1, if(musig._2 > 0.0) musig._2 else -1.0))
  70.                
  71.                         // Format the tuple muSigma= (mean, standard deviation) and the Prior
  72.                 val first = format(_muSigma, "Label\tMeans", "Standard Deviation\n", MEDIUM, labels)
  73.                 s"$first ${format(prior, "\nClass likelihood", MEDIUM)}"
  74.         }
  75.        
  76.         override def toString: String = toString(Array.empty)
  77. }


  78.                 /**
  79.                  * Companion object for the Naive Bayes Likelihood class. The singleton
  80.                  * is used to define the constructor apply for the class.
  81.                  * @author Patrick Nicolas
  82.                  * @since 0.98.1 March 11, 2014
  83.                  * @version 0.98.1
  84.                  * @see Scala for Machine Learning Chapter 5 Naive Bayes Models
  85.                  */
  86. object Likelihood {
  87.         private val MINLOGARG = 1e-32
  88.         private val MINLOGVALUE = -MINLOGARG

  89.                 /**
  90.                  * Default constructor for he class Likelihood.
  91.                  * @param label  Name or label of the class or prior for which the likelihood is computed.
  92.                  * @param muSigma Array of tuples (mean, standard deviation) of the prior observations
  93.                  * for the model
  94.                  * @param prior  Probability of occurrence for the class specified by the label.
  95.                  */
  96.         def apply[T <: AnyVal](label: Int, muSigma: Vector[DblPair], prior: Double)
  97.                         (implicit f: T => Double): Likelihood[T] =
  98.                 new Likelihood[T](label, muSigma, prior)
  99.    
  100.         private def check(muSigma: Vector[DblPair], prior: Double): Unit =  {
  101.                 require( muSigma.nonEmpty,
  102.                                 "Likelihood.check Historical mean and standard deviation is undefined")
  103.                 require(prior > 0.0  && prior <= 1.0,
  104.                                 s"Likelihood.check Prior for the NB prior $prior is out of range")
  105.         }
  106. }


  107. // --------------------------------  EOF
复制代码

使用道具

板凳
zhouxiaohui888 在职认证  发表于 2016-3-8 22:01:00 |只看作者 |坛友微信交流群
谢谢嘻嘻嘻嘻嘻嘻

使用道具

报纸
ydb8848 发表于 2016-3-10 08:27:33 |只看作者 |坛友微信交流群
  1. package org.scalaml.supervised.bayes

  2. import NaiveBayesModel._


  3.                 /**
  4.                  * Defines a Multi-class (or multinomial) Naive Bayes model for n classes.
  5.                  * The number of classes is defined as likelihoodSet.size. The binomial Naive Bayes model,
  6.                  * BinNaiveBayesModel, should be used for the two class problem.
  7.                  * @tparam T type of features in each observation
  8.                  * @constructor Instantiates a multi-nomial Naive Bayes model (number classes > 2)
  9.                  * @throws IllegalArgumentException if any of the class parameters is undefined
  10.                  * @param likelihoodSet  List of likelihood or priors for every classes in the model.
  11.                  * @author Patrick Nicolas
  12.                  * @since 0.98 February 11, 2014
  13.                  * @version 0.99.1
  14.                  * @see Scala for Machine Learning  Chapter 5 "Naive Bayes Models" / Naive Bayes Classifiers
  15.                  */
  16. protected class MultiNaiveBayesModel[T <: AnyVal](
  17.                 likelihoodSet: Seq[Likelihood[T]])(implicit f: T => Double) extends NaiveBayesModel[T] {
  18.   
  19.         require( likelihoodSet.nonEmpty,
  20.                         "MultiNaiveBayesModel Cannot classify using Multi-NB with undefined classes")
  21.           
  22.                 /**
  23.                  * Classify a new observation (or vector) using the Mult-inomial Naive Bayes model.
  24.                  * @param x new observation
  25.                  * @return the class ID the new observations has been classified.
  26.                  * @throws IllegalArgumentException if any of the observation is undefined.
  27.                  */
  28.         override def classify(x: Array[T], logDensity: LogDensity): Int = {
  29.                 require( x.length > 0, "MultiNaiveBayesModel.classify Vector input is undefined")
  30.                
  31.                         // The classification is performed by ordering the class according to the
  32.                         // log of their posterior probability and selecting the top one (highest
  33.                         // posterior probability)
  34.                
  35.                 val <<< = (p1: Likelihood[T], p2: Likelihood[T]) =>
  36.                                 p1.score(x, logDensity) > p1.score(x, logDensity)
  37.                          
  38.                 likelihoodSet.sortWith(<<<).head.label
  39.         }

  40.        
  41.         override def likelihoods: Seq[Likelihood[T]] = likelihoodSet
  42.        
  43.         override def toString(labels: Array[String]): String = {
  44.                 require( labels.length > 0, "MultiNaiveBayesModel.toString Vector input is undefined")
  45.                          
  46.                 likelihoodSet.zipWithIndex
  47.                                 .map{ case (lp, n) => s"\nclass$n : ${lp.toString(labels)}" }
  48.                                 .mkString(",")
  49.         }
  50.        
  51.         override def toString: String = likelihoodSet.mkString("\n")
  52. }

  53.                 /**
  54.                  * Companion object for the multinomial Naive Bayes Model. The singleton
  55.                  * is used to define the constructor of MultiNaiveBayesModel
  56.                  *
  57.                  * @author Patrick Nicolas
  58.                  * @since 0.98 February 10, 2014
  59.                  * @version 0.98
  60.                  * @see Scala for Machine Learning  Chapter 5 "Naive Bayes Models" / Naive Bayes Classifiers
  61.                  */
  62. object MultiNaiveBayesModel {
  63.                 /**
  64.                  * Default constructor for the multinomial Naive Bayes model as instance of
  65.                  * MultiNaiveBayesModel
  66.                  * @tparam T type of features in each observation
  67.                  * @param likelihoodSet  List of likelihood or priors for every classes in the model.
  68.                  */
  69.         def apply[T <: AnyVal](
  70.                         likelihoodSet: Seq[Likelihood[T]])(implicit f: T => Double): MultiNaiveBayesModel[T] =
  71.                                         new MultiNaiveBayesModel[T](likelihoodSet)
  72. }


  73. // --------------------------------  EOF --------------------------------------------------------------
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

地板
Crsky7 发表于 2016-3-10 10:00:49 |只看作者 |坛友微信交流群
  1. package org.scalaml.supervised.bayes

  2. // Scala classes
  3. import scala.language.implicitConversions
  4. import scala.util.Try

  5. import org.apache.log4j.Logger

  6. import org.scalaml.core.Types.ScalaMl._
  7. import org.scalaml.stats.{XTSeries, Stats}
  8. import org.scalaml.validation.MultiFValidation
  9. import org.scalaml.core.ITransform
  10. import org.scalaml.supervised.Supervised
  11. import org.scalaml.util.LoggingUtils._
  12. import NaiveBayesModel._, XTSeries._, Stats._, NaiveBayes._

  13.    
  14.                 /**
  15.                  * Generic Binomial Naive Bayes classification class. The class is used for both training
  16.                  * and run-time classification. The training of the model is executed during the instantiation
  17.                  * of the class to avoid having an uninitialized model. A conversion from a parameterized
  18.                  * array, ''Array[T]'' to an array of double, ''DblArray'', has to be implicitly defined for
  19.                  * training the model.
  20.                  *
  21.                  * The implemantation follows the standard design of supervised learning algorithm:
  22.                  * - The classifier implements the '''ITransform''' implicit monadic data transformation
  23.                  * - The constructor triggers the training of the classifier, making the model immutable
  24.                  * - The classifier implements the '''Monitor''' interface to collect profile information for
  25.                  * debugging purpose
  26.                  *
  27.                  * As a classifier, the method implement a data transformation '''ITransform''' with a model
  28.                  * explicitly extracted from a training set
  29.                  * {{{
  30.                  *   Naive Bayes formula:
  31.                  *   p(C}x) = p(x|C).p(C)/p(x) => p(C|x) = p(x1|C).p(x2|C). .. p(xn|C).p(C)
  32.                  * }}}
  33.                  * @tparam T type of features in each observation
  34.                  * @constructor Instantiate a parameterized NaiveBayes model
  35.                  * @param smoothing Laplace or Lidstone smoothing factor
  36.                  * @param xt  Input labeled time series used for training
  37.                  * @param expected in the training of a model
  38.                  * @param classes Number of classes used in the Naive Bayes
  39.                  *
  40.                  * @throws IllegalArgumentException if one of the class parameters is undefined
  41.                  * @author Patrick Nicolas
  42.                  * @since 0.98 February 13, 2014
  43.                  * @version 0.99.1
  44.                  * @see Scala for Machine learning Chapter 5 "Naive Bayes Models" / Naive Bayes classifiers
  45.                  * @see org.scalaml.core.ITransform
  46.                  */
  47. @throws(classOf[IllegalArgumentException])
  48. private[scalaml] final class NaiveBayes[T <: AnyVal](
  49.                 smoothing: Double,
  50.                 xt: XVSeries[T],
  51.                 expected: Vector[Int],
  52.                 logDensity: LogDensity,
  53.                 classes: Int)(implicit f: T => Double)
  54.         extends ITransform[Array[T]](xt) with Supervised[T, Array[T]] with Monitor[T] {

  55.   type V = Int
  56.         check(smoothing, xt, expected, classes)
  57.        
  58.         protected val logger = Logger.getLogger("NaiveBayes")
  59.        
  60.                 /**
  61.                  * The model is instantiated during training for both
  62.                  * classes if the training is successful. It is None otherwise
  63.                  */
  64.         val model: Option[NaiveBayesModel[T]] = Try {
  65.                 if( classes == 2)
  66.                         BinNaiveBayesModel[T](train(1), train(0))
  67.                 else
  68.                         MultiNaiveBayesModel[T](List.tabulate(classes)( train(_)))
  69.         }
  70.   ._toOption("NaiveBayes.train", logger)



  71.                 /**
  72.                  * Run-time classification of a time series using the Naive Bayes model. The method invoke
  73.                  * the actual classification method in one of the NaiveBayes models.
  74.                  * @throws MatchError if the input time series is undefined or have no elements or the
  75.                  * model was not properly trained
  76.                  * @return PartialFunction of time series of elements of type T as input to the Naive Bayes
  77.                  * and array of class indices as output
  78.                  */
  79.         override def |> : PartialFunction[Array[T], Try[V]] = {
  80.                 case x: Array[T] if x.length > 0 && model.isDefined =>
  81.                         Try( model.map(_.classify(x, logDensity)).get )
  82.         }
  83.        
  84.                 /**
  85.                  * Compute the F1 statistics for the Naive Bayes.
  86.                  * @param xt Time series of features of type Array[T], and class indices as labels
  87.                  * @param expected expected value or label
  88.                  * @return F1 measure if the model has been properly trained, Failure otherwise
  89.                  */
  90.         override def validate(xt: XVSeries[T], expected: Vector[V]): Try[Double] = Try {
  91.                 val predict = model.get.classify(_ : Array[T], logDensity)
  92.                 MultiFValidation[T](expected, xt, classes)(predict).score
  93.         }
  94.           
  95.                 /**
  96.                  * Compute the F1 statistics for the Naive Bayes.
  97.                  * @param labeled Time series of features and expected values
  98.                  * @return F1 measure if the model has been properly trained, Failure otherwise
  99.                  */
  100.   def validate(labeled: Vector[(Array[T], V)]): Try[Double] = Try {
  101.           val predict = model.get.classify(_: Array[T], logDensity)
  102.           MultiFValidation[T](labeled, classes)(predict).score
  103.         }

  104.                 /**
  105.                  * Textual representation of the Naive Bayes classifier with labels for features.
  106.                  * It returns "No Naive Bayes model" if no model exists
  107.                  * @return Stringized features with their label if model exists.
  108.                  */
  109.         def toString(labels: Array[String]): String =
  110.                 if( model.isDefined) "No model"
  111.                 else
  112.                         if(labels.length > 0) model.get.toString(labels) else model.get.toString
  113.                

  114.                 /**
  115.                  * Default textual representation of the Naive Bayes classifier with labels for features.
  116.                  * It returns "No Naive Bayes model" if no model exists
  117.                  * @return Stringized features with their label if model exists.
  118.                  */
  119.         override def toString: String = toString(Array.empty[String])
  120.    
  121.                 /**
  122.                  * Train the Naive Bayes model on one of the the classes
  123.                  */
  124.         @throws(classOf[IllegalStateException])
  125.         private def train(index: Int): Likelihood[T] = {
  126.                 val xv: XVSeries[Double] = xt
  127.                
  128.                                 // Extract then filter each observation to be associated to a specific label.
  129.                                 // The implicit conversion from Array of type T to Array of type Double is invoked
  130.                 val values = xv.zip(expected).filter( _._2 == index).map(_._1)
  131.                 if( values.isEmpty )
  132.                   throw new IllegalStateException("NaiveBayes.train Filtered value is undefined")
  133.        
  134.                         // Gets the dimension of a feature
  135.                 val dim = dimension(xv)
  136.                
  137.                         // Create a likelihood instance for this class 'label'. The
  138.                         // tuple (mean, standard deviation) (2nd argument) is computed
  139.                         // by invoking XTSeries.statistics then the Lidstone mean adjustment.
  140.                         // The last argument, class likelihood p(C) is computed as the ratio of the
  141.                         // number of observations associated to this class/label over total number of observations.
  142.                 Likelihood(index,
  143.                         statistics(values).map(stat => (stat.lidstoneMean(smoothing, dim), stat.stdDev) ),  
  144.                         values.size.toDouble/xv.size)
  145.         }
  146. }

  147.                 /**
  148.                  * Singleton that define the constructors for the NaiveBayes classifier and
  149.                  * validate its parameters
  150.                  * @author Patrick Nicolas
  151.                  * @since 0.98 February 13, 2014
  152.                  * @version 0.98
  153.                  * @see Scala for Machine learning Chapter 5 "Naive Bayes Model"
  154.                  */
  155. object NaiveBayes {       
  156.   
  157.                 /**
  158.                  * Implicit conversion from a NaiveBayes[T} to a Try[NaiveBayes[T}} type.
  159.                  */
  160.         implicit def naiveBayes2Try[T <: AnyVal](nb: NaiveBayes[T])(implicit f: T => Double): Try[NaiveBayes[T]] = Try(nb)
  161.        

  162.                 /**
  163.                  * Default constructor for the NaiveBayes class
  164.                  * @tparam T type of features in the time series xt
  165.                  * @param smoothing Laplace or Lidstone smoothing factor
  166.                  * @param xt Input time series of observations used for training
  167.                  * @param expected Input labeled time series used for training
  168.                  * @param logDensity  logarithm formulation of the probability density function
  169.                  * @param classes Number of classes used in the Naive Bayes model
  170.                  */
  171.         def apply[T <: AnyVal](
  172.                         smoothing: Double,
  173.                         xt: XVSeries[T],
  174.                         expected: Vector[Int],
  175.                         logDensity: LogDensity,
  176.                         classes: Int)(implicit f: T => Double): NaiveBayes[T] =
  177.            new NaiveBayes[T](smoothing, xt, expected, logDensity, classes)
  178.        

  179.                 /**
  180.                  * Constructor for the NaiveBayes class using single input data set {observations, expected}
  181.                  * @tparam T type of features in the time series xt
  182.                  * @param smoothing Laplace or Lidstone smoothing factor
  183.                  * @param xty Input time series of pair (observations, expected outcome) used for training
  184.                  * @param logDensity  logarithm formulation of the probability density function
  185.                  * @param classes Number of classes used in the Naive Bayes model
  186.                  */
  187.         def apply[T <: AnyVal] (
  188.                         smoothing: Double,
  189.                         xty: Vector[(Array[T], Int)],
  190.                         logDensity: LogDensity = logGauss,
  191.                         classes: Int = 2)(implicit f: T => Double): NaiveBayes[T] = {
  192.        
  193.                 val xy: (Vector[Array[T]], Vector[Int]) = xty.unzip
  194.                 new NaiveBayes[T](smoothing, xy._1, xy._2, logDensity, classes)
  195.         }

  196.                          
  197.                 /**
  198.                  * Constructor for the Binomial NaiveBayes class using the Laplace smoothing factor, and the
  199.                  * gaussian distribution
  200.                  * @tparam T type of features in the time series xt
  201.                  * @param xt Input time series of observations used for training
  202.                  * @param expected Input labeled time series used for training
  203.                  */
  204.         def apply[T <: AnyVal](xt: XVSeries[T], expected: Vector[Int])(implicit f: T => Double): NaiveBayes[T] =
  205.                 new NaiveBayes[T](1.0, xt, expected, logGauss, 2)

  206.        
  207.        
  208.         private def check[T](       
  209.                         smoothing: Double,
  210.                         xt: XVSeries[T],
  211.                         expected: Vector[Int],
  212.                         classes: Int): Unit = {
  213.           
  214.                 require(smoothing > 0.0 && smoothing <= 1.0,
  215.                         s"NaiveBayes: Found smoothing $smoothing required 0 < smoothing <= 1")
  216.                 require( xt.nonEmpty,
  217.                         "NaiveBayes: Time series input for training Naive Bayes is undefined")
  218.                 require( expected.nonEmpty,
  219.                         "NaiveBayes: labeled values for training Naive Bayes is undefined")
  220.                 require( classes > 1,
  221.                         s"NaiveBayes: Naive Bayes found $classes required classes > 1")
  222.                 require( xt.size == expected.size,
  223.                         s"NaiveBayes: observations set ${xt.size} and labels set ${expected.size} should have same size")
  224.         }
  225. }
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

7
420948492 发表于 2016-3-10 11:53:38 |只看作者 |坛友微信交流群
{:3_42:}

使用道具

8
russelllee_ 发表于 2016-3-10 16:20:42 |只看作者 |坛友微信交流群
好啊好

使用道具

9
leon_9930754 发表于 2016-3-10 19:05:49 |只看作者 |坛友微信交流群
谢谢分享

使用道具

10
tonyme2 在职认证  发表于 2016-3-11 12:18:01 |只看作者 |坛友微信交流群
support

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-11-5 14:40