In [1]:
import $ivy.`org.scalanlp::breeze:0.13.1`
import $ivy.`org.scalanlp::breeze-natives:0.13.1`
import $ivy.`com.chuusai::shapeless:2.3.2`
Out[1]:
In [2]:
import breeze.linalg.{eig, eigSym, svd, DenseMatrix => BDM, DenseVector => BDV, Vector => BV}
import breeze.numerics.pow
import breeze.linalg._
import breeze.numerics._
// import shapeless._
Out[2]:
In [3]:
val dm = DenseMatrix((1.0,2.0,3.0,4.0), (5.0,6.0,7.0,8.0), (9.0,10.0,11.0,12.0), (13.0,14.0,15.0,16.0))
Out[3]:
In [4]:
dm(::,2)
Out[4]:
In [5]:
val indices = IndexedSeq(0,2)
Out[5]:
In [6]:
dm(::,indices)
Out[6]:
In [7]:
val v = sqrt(DenseVector(2,3))
val m = dm(::, indices).toDenseMatrix
Out[7]:
In [8]:
m.rows
Out[8]:
In [8]:
// m(::, *) :* v
In [9]:
val dm = DenseMatrix( (2, 4, 6), (3, 6, 9) )
val dv = DenseVector(2,3)
dm(::, *)
dm(::, *) :* dv
Out[9]:
In [10]:
val adder = DenseVector(1, 2, 3, 4)
val result = DenseMatrix.fill[Int](3, 4)(5).mapPairs({
case ((row, col), value) => {
print( row + ":" +col)
println("--->" + value + ":" + adder(col))
value + adder(col)
}
})
Out[10]:
In [11]:
val result = m.mapPairs({
case ((row, col), value) => {
value * v(col)
}
})
Out[11]:
In [12]:
val p = List(4,3,5,6,7)
val q = List(8,3,0,1,9)
val r = List(3,8,1,0,3)
val i = List(1,4,3,2,5)
Out[12]:
In [12]:
// (p,q,r,i).zipped Error
In [12]:
val list = 42 :: "foo" :: HNil
In [12]:
list.head
list.tail.head
list(1)
list
In [13]:
class BiMap[K, V]
implicit val stringToVec = new BiMap[String, BV[Double]]
implicit val stringToArrayDouble = new BiMap[String, Array[Double]]
implicit val stringToArrayString = new BiMap[String, Array[String]]
implicit val stringToString = new BiMap[String, String]
Out[13]:
In [13]:
val hm1 = HMap[BiMap]("saliney" -> BV[Double](1.0,2.0,3.0), "term" -> BV[Double](3.5,6.8,7.8))
In [13]:
hm1.get("saliney")
In [14]:
val tvalues: Array[Double] = Array(1.866393526974307, 2.864048126935307, 4.032486069215076, 7.876169953355888, 4.875333799256043, 14.316322626848278)
val pvalues: Array[Double] = Array(0.064020056478447, 0.004808399479386827, 8.914865448939047E-5, 7.489564524121306E-13, 2.8363794106756046E-6, 0.0)
Out[14]:
In [15]:
val zippedArray = Array(tvalues, pvalues).transpose
Out[15]:
In [16]:
val strngArray = Array.fill(tvalues.length)("Default")
Out[16]:
In [16]:
val zippedArray1 = Array(tvalues, pvalues, strngArray).transpose
In [17]:
val lambdaStep = 0.01
val lambdaSeq = BigDecimal("0.00") to BigDecimal("1.0") by BigDecimal(lambdaStep)
Out[17]:
In [18]:
val m = BDM(
(0.23, 0.20, 0.10, 0.92, 0.33, 0.42),
(0.10, 0.43, 0.23, 0.15, 0.22, 0.12),
(0.20, 0.13, 0.25, 0.85, 0.02, 0.32),
(0.43, 0.65, 0.23, 0.45, 0.10, 0.33),
(0.31, 0.87, 0.45, 0.63, 0.28, 0.16),
(0.12, 0.84, 0.33, 0.45, 0.56, 0.83),
(0.40, 0.22, 0.12, 0.87, 0.35, 0.78))
Out[18]:
In [24]:
// Let's work in a mix functional style and iterator working with columns
// look at this example
val a = m(::, 0) // get the firts column
.toArray // pass to scala array for functional usage, you can use then to List
.zipWithIndex // now you have and array like [(value0,0),(value1,1) ... (valuen,n)]
.sortWith((x, y) => x._1 > y._1) // sort by bigger number
.take(5) // get only 5 first numbers
.map(x => x._2) // finally get the indexes
Out[24]:
In [27]:
val a = Array(0.23, 0.2, 0.1, 0.92, 0.33, 0.42).zipWithIndex.sortWith((x, y) => x._1 > y._1)
.take(5)
.map(x => x._2)
Out[27]:
In [1]:
1 to 6
Out[1]:
In [29]:
//now we have to loop for each colum
// prepare the matrix and get the Vector(indexes,Array[Int],Array[Int])
val listsOfIndexes = for (i <- Range(0, m.rows))
yield m(i, ::).inner.toArray
.zipWithIndex
.sortWith((x, y) => x._1 > y._1)
.take(5)
.map(x => x._2)
//finally conver to a DenseMatrix
val mIndex = BDM(listsOfIndexes.map(_.toArray): _*)
println(mIndex)
Out[29]:
In [3]:
val a1 = Array(1,2,3)
val a2 = Array(1,2,3)
var curatedTermIndex: Array[Int] = Array()
Out[3]:
In [3]:
curatedTermIndex :: a1
In [7]:
curatedTermIndex = curatedTermIndex++:a1++:a2
In [8]:
curatedTermIndex
Out[8]:
In [2]:
import scala.reflect.runtime.universe._
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
case m: MethodSymbol if m.isCaseAccessor => m
}.toList
In [ ]: