- Scala for Data Science
- Pascal Bugnion
- 310字
- 2021-07-23 14:33:05
Diving into Breeze
Let's get started. We will work in the Scala console, but a program similar to this example is available in BreezeDemo.scala
in the examples corresponding to this chapter. Create a build.sbt
file with the following lines:
scalaVersion := "2.11.7" libraryDependencies ++= Seq( "org.scalanlp" %% "breeze" % "0.11.2", "org.scalanlp" %% "breeze-viz" % "0.11.2", "org.scalanlp" %% "breeze-natives" % "0.11.2" )
Start an sbt
console:
$ sbt console scala> import breeze.linalg._ import breeze.linalg._ scala> import breeze.plot._ import breeze.plot._ scala> import breeze.numerics._ import breeze.numerics._
Let's start by plotting a sigmoid curve, . We will first generate the data using Breeze. Recall that the
linspace
method creates a vector of doubles, uniformly distributed between two values:
scala> val x = linspace(-4.0, 4.0, 200) x: DenseVector[Double] = DenseVector(-4.0, -3.959798... scala> val fx = sigmoid(x) fx: DenseVector[Double] = DenseVector(0.0179862099620915,...
We now have the data ready for plotting. The first step is to create a figure:
scala> val fig = Figure() fig: breeze.plot.Figure = breeze.plot.Figure@37e36de9
This creates an empty Java Swing window (which may appear on your taskbar or equivalent). A figure can contain one or more plots. Let's add a plot to our figure:
scala> val plt = fig.subplot(0) plt: breeze.plot.Plot = breeze.plot.Plot@171c2840
For now, let's ignore the 0
passed as argument to .subplot
. We can add data points to our plot
:
scala> plt += plot(x, fx) breeze.plot.Plot = breeze.plot.Plot@63d6a0f8
The plot
function takes two arguments, corresponding to the x and y values of the data series to be plotted. To view the changes, you need to refresh the figure:
scala> fig.refresh()
Look at the Swing window now. You should see a beautiful sigmoid, similar to the one below. Right-clicking on the window lets you interact with the plot and save the image as a PNG:

You can also save the image programmatically as follows:
scala> fig.saveas("sigmoid.png")
Breeze-viz currently only supports exporting to PNG.
- Delphi程序設(shè)計基礎(chǔ):教程、實驗、習(xí)題
- Mastering Selenium WebDriver
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優(yōu)化計算
- 編譯系統(tǒng)透視:圖解編譯原理
- Learning Vaadin 7(Second Edition)
- Java程序設(shè)計與項目案例教程
- Mastering Concurrency Programming with Java 9(Second Edition)
- SQL Server 2012 數(shù)據(jù)庫應(yīng)用教程(第3版)
- Deep Learning for Natural Language Processing
- 和孩子一起學(xué)編程:用Scratch玩Minecraft我的世界
- Scratch編程從入門到精通
- Android初級應(yīng)用開發(fā)
- Python網(wǎng)絡(luò)爬蟲從入門到實踐
- VBA Automation for Excel 2019 Cookbook
- HTML 5+CSS 3+jQuery Mobile移動網(wǎng)站與APP開發(fā)實戰(zhàn)