我相對較新的斯卡拉,一直使用R和MATLAB之前。我在Scala中編寫了以下代碼片段。我已經在R和MATLAB中編寫了相同的代碼,並且兩者都很好,但由於我對Scala的經驗不足,下面的代碼無法工作。斯卡拉參考函數在另一個函數
import breeze.linalg._
import breeze.numerics.exp
import scala.math.log
val data = breeze.stats.distributions.Uniform(0, 1)
val ep: DenseMatrix[Double] = DenseMatrix.rand(39, 3, data).t
val a = DenseVector(1.0)
val out: DenseMatrix[Double] = tile(a, 1, 39)
val fout: DenseVector[Double] = out.toDenseVector
val A: Double = 0.0
val B: Double = 1.0
val eta: Double = 2.0/Math.pow(B - A, 2.0)
val nCol: Int = 39
val nRow: Int = 3
var gA = 0.0
var gB = 0.0
var gamma = 0.0
def SubstFunction(predictions: DenseVector[Double], expertsPrediction: DenseVector[Double]): Double = {
gA = -(1/eta) * log(predictions dot exp(-eta * (expertsPrediction :- A)) :^ 2.0)
gB = -(1/eta) * log(predictions dot exp(-eta * (expertsPrediction :- B)) :^ 2.0)
gamma = (0.5 * (B + A)) - ((gB - gA)/2 * (B - A))
gamma
}
def prediction(Input: DenseMatrix[Double], outcomes: DenseVector[Double]): DenseVector[Double] = {
var weights = DenseVector(1.0,1.0,1.0)
val AAprediction = DenseVector.fill(nCol)(0.0)
//DenseVector.ones[Double](nCol).t
for (l<-0 to Input.cols) {
val normalisedWeights = weights/sum(weights)
AAprediction(l) = SubstFunction(normalisedWeights, Input(::,l))
weights = normalisedWeights :* exp(eta :* (Input(::,l) :- outcomes(l)) :^ 2.0).toDenseVector
}
AAprediction: DenseVector[Double]
}
prediction(ep,fout)
我覺得問題可能是預測在其中調用sbstFunction
。我正在使用intelliJ中的Scala工作表。當我運行的代碼,我沒有得到任何錯誤,但我沒有得到一個數字輸出,而不是我得到:
<function1> res1: Unit =()
更新:我有固定的代碼,現在我收到以下錯誤:
Column must be in bounds for slice!
有人可以幫我理解我做錯了什麼嗎?
通過這樣做,我得到了我的代碼幾個問題,也許這意味着我的代碼也不正確。感謝你們對我的幫助。 – Jamil