楼主: ReneeBK
1490 14

Mike de Waard:Machine Learning for Developers using Scala [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8104
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

本帖隐藏的内容

Machine Learning for Developers Mike de Waard.pdf (14.29 MB, 需要: 1 个论坛币)


二维码

扫码加我 拉你入群

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

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

关键词:Developers developer Learning earning machine

已有 1 人评分经验 收起 理由
william9225 + 60 精彩帖子

总评分: 经验 + 60   查看全部评分

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-4-21 09:43:48 |只看作者 |坛友微信交流群
  1. object KNNExample {
  2.    def main(args: Array[String]): Unit = {
  3.     val basePath = "/.../KNN_Example_1.csv"
  4.     val testData = getDataFromCSV(new File(basePath))
  5.     }

  6.   def getDataFromCSV(file: File): (Array[Array[Double]], Array[Int]) = {
  7.     val source = scala.io.Source.fromFile(file)
  8.     val data = source
  9.         .getLines()
  10.         .drop(1)
  11.         .map(x => getDataFromString(x))
  12.         .toArray

  13.     source.close()
  14.     val dataPoints = data.map(x => x._1)
  15.     val classifierArray = data.map(x => x._2)
  16.     return (dataPoints, classifierArray)        
  17.   }

  18.   def getDataFromString(dataString: String): (Array[Double], Int) = {

  19.     //Split the comma separated value string into an array of strings
  20.     val dataArray: Array[String] = dataString.split(',')

  21.     //Extract the values from the strings
  22.     val xCoordinate: Double = dataArray(0).toDouble
  23.     val yCoordinate: Double = dataArray(1).toDouble
  24.     val classifier: Int = dataArray(2).toInt

  25.     //And return the result in a format that can later
  26.     //easily be used to feed to Smile
  27.     return (Array(xCoordinate, yCoordinate), classifier)
  28.   }
  29. }
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-4-21 09:44:41 |只看作者 |坛友微信交流群
  1. object KNNExample extends SimpleSwingApplication {
  2.   def top = new MainFrame {
  3.     title = "KNN Example"
  4.     val basePath = "/.../KNN_Example_1.csv"

  5.     val testData = getDataFromCSV(new File(basePath))

  6.     val plot = ScatterPlot.plot(testData._1,
  7.                                  testData._2,
  8.                                  '@',
  9.                                  Array(Color.red, Color.blue)
  10.                                 )
  11.     peer.setContentPane(plot)
  12.     size = new Dimension(400, 400)

  13.   }
  14.   ...
复制代码

使用道具

板凳
ReneeBK 发表于 2016-4-21 09:45:30 |只看作者 |坛友微信交流群
  1. def main(args: Array[String]): Unit = {
  2.    val basePath = "/.../KNN_Example_1.csv"
  3.     val testData = getDataFromCSV(new File(basePath))

  4.     //Define the amount of rounds, in our case 2 and
  5.     //initialise the cross validation
  6.     val cv = new CrossValidation(testData._2.length, validationRounds)

  7.     val testDataWithIndices = (testData
  8.                                 ._1
  9.                                 .zipWithIndex,
  10.                                 testData
  11.                                 ._2
  12.                                 .zipWithIndex)

  13.     val trainingDPSets = cv.train
  14.       .map(indexList => indexList
  15.       .map(index => testDataWithIndices
  16.       ._1.collectFirst { case (dp, `index`) => dp}.get))

  17.     val trainingClassifierSets = cv.train
  18.       .map(indexList => indexList
  19.       .map(index => testDataWithIndices
  20.       ._2.collectFirst { case (dp, `index`) => dp}.get))

  21.     val testingDPSets = cv.test
  22.       .map(indexList => indexList
  23.       .map(index => testDataWithIndices
  24.       ._1.collectFirst { case (dp, `index`) => dp}.get))

  25.     val testingClassifierSets = cv.test
  26.       .map(indexList => indexList
  27.       .map(index => testDataWithIndices
  28.       ._2.collectFirst { case (dp, `index`) => dp}.get))


  29.     val validationRoundRecords = trainingDPSets
  30.       .zipWithIndex.map(x => (  x._1,           
  31.                                 trainingClassifierSets(x._2),
  32.                                 testingDPSets(x._2),
  33.                                 testingClassifierSets(x._2)
  34.                                )
  35.                         )

  36.     validationRoundRecords
  37.         .foreach { record =>

  38.       val knn = KNN.learn(record._1, record._2, 3)

  39.       //And for each test data point make a prediction with the model
  40.       val predictions = record
  41.         ._3
  42.         .map(x => knn.predict(x))
  43.         .zipWithIndex

  44.       //Finally evaluate the predictions as correct or incorrect
  45.       //and count the amount of wrongly classified data points.

  46.       val error = predictions
  47.         .map(x => if (x._1 != record._4(x._2)) 1 else 0)
  48.         .sum

  49.       println("False prediction rate: " + error / predictions.length * 100 + "%")
  50.     }
  51.   }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-4-21 09:46:18 |只看作者 |坛友微信交流群
  1. val knn = KNN.learn(record._1, record._2, 3)
  2. val unknownDataPoint = Array(5.3, 4.3)
  3. val result = knn.predict(unknownDatapoint)
  4. if (result == 0)
  5. {
  6.     println("Internet Service Provider Alpha")
  7. }
  8. else if (result == 1)
  9. {
  10.     println("Internet Service Provider Beta")
  11. }
  12. else
  13. {
  14.     println("Unexpected prediction")
  15. }
复制代码

使用道具

地板
ReneeBK 发表于 2016-4-21 09:47:26 |只看作者 |坛友微信交流群
  1. def getMessage(file : File)  : String  =
  2.   {
  3.     //Note that the encoding of the example files is latin1,
  4.     // thus this should be passed to the fromFile method.
  5.     val source = scala.io.Source.fromFile(file)("latin1")
  6.     val lines = source.getLines mkString "\n"
  7.     source.close()
  8.     //Find the first line break in the email,
  9.     //as this indicates the message body
  10.     val firstLineBreak = lines.indexOf("\n\n")
  11.     //Return the message body filtered by only text from a-z and to lower case

  12.     return lines
  13.         .substring(firstLineBreak)
  14.         .replace("\n"," ")
  15.         .replaceAll("[^a-zA-Z ]","")
  16.         .toLowerCase()
  17.   }
复制代码

使用道具

7
ReneeBK 发表于 2016-4-21 09:48:05 |只看作者 |坛友微信交流群
  1. def getFilesFromDir(path: String):List[File] = {
  2.     val d = new File(path)
  3.     if (d.exists && d.isDirectory) {
  4.       //Remove the mac os basic storage file,
  5.       //and alternatively for unix systems "cmds"

  6.       d .listFiles
  7.         .filter(x => x  .isFile &&
  8.                      !x .toString
  9.                         .contains(".DS_Store")  &&
  10.                      !x .toString
  11.                         .contains("cmds"))
  12.                         .toList
  13.     }
  14.     else {
  15.       List[File]()
  16.     }
  17.   }
复制代码

使用道具

8
ReneeBK 发表于 2016-4-21 09:48:48 |只看作者 |坛友微信交流群
  1. def main(args: Array[String]): Unit = {
  2.     val basePath = "/Users/../Downloads/data"
  3.     val spamPath = basePath + "/spam"
  4.     val spam2Path = basePath + "/spam_2"
  5.     val easyHamPath = basePath + "/easy_ham"
  6.     val easyHam2Path = basePath + "/easy_ham_2"

  7.     val amountOfSamplesPerSet = 500
  8.     val amountOfFeaturesToTake = 100
  9.     //First get a subset of the filenames for the spam
  10.     // sample set (500 is the complete set in this case)
  11.     val listOfSpamFiles =   getFilesFromDir(spamPath)
  12.         .take(amountOfSamplesPerSet)
  13.     //Then get the messages that are contained in these files
  14.     val spamMails = listOfSpamFiles.map(x => (x, getMessage(x)))

  15.      //Get a subset of the filenames from the ham sample set
  16.      // (note that in this case it is not necessary to randomly
  17.      // sample as the emails are already randomly ordered)

  18.     val listOfHamFiles =   getFilesFromDir(easyHamPath)
  19.         .take(amountOfSamplesPerSet)
  20.     //Get the messages that are contained in the ham files
  21.     val hamMails  = listOfHamFiles
  22.         .map{x => (x,getMessage(x)) }
  23.   }
复制代码

使用道具

9
soccy 发表于 2016-4-21 10:09:29 |只看作者 |坛友微信交流群
......

使用道具

10
80丶90年代 发表于 2016-4-21 10:47:05 |只看作者 |坛友微信交流群
别问我为什么就为了签个到

使用道具

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

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

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

GMT+8, 2024-4-25 15:37