我有一个像这样声明的ListBuffer:
var distances_buffer: ListBuffer[List[Any]] = ListBuffer.empty[List[Any]]
我正在尝试用这样的数据填充它:
for(current <- 0 to training_list_length - 1){
//A and B are examples
distances_buffer(current) ++= List[Any](A,B)
}
但是我收到以下错误:
java.lang.IndexOutOfBoundsException: 0
我错过了什么?
编辑!更多信息:
我有一个点及其类别的列表(命名:training_list
(:training_list : List[((Double, Double, String))]
我还有一个额外的点,给出了一个 x 和一个 y 值。
我的目标是计算额外点与训练列表中每个点的欧氏距离,并创建一个如下所示的结果:
//example
List((x: Double, y: Double, class: String), distance: String)
List((4.3,3.0,Iris-setosa), 1.2529964086141665), (4.4,3.0,Iris-setosa), 1.341640786499874)...
如您所见,在列表中,我想包括点的坐标(从training_list开始(、点的类别以及距离。
for(current <- 0 to training_list_length - 1){
val dist = eDistance(test_x, test_y, training_x(current), training_y(current))
distances_buffer += ListBuffer[Any](training_result(current),dist)
}
创建此列表后,我想根据距离对其进行排序。也卡在这里!
从你命名事物的方式来看,你似乎来自Python背景。
我建议你先研究一下Scala,因为我们是非常不同的。
不仅在样式上(例如骆驼大小写与破折号大小写(,而且在更基本的事情上,例如:
- 一个强大的静态类型系统。因此,像
Any
这样的东西通常是一种代码气味,对于99.99%的情况,完全没有必要。 - OOP和FP的混合。所以,你不必成为FP专家,但即使在Scala的OOP方面,也有一些东西是惯用的,比如不变性和常见的操作(高阶函数(,如
map
、flatMap
、filter
和reduce
。 - 我们的列表与 Python 列表非常不同,通过索引访问元素是
O(n)
(在Python中会是O(1)
(。Python列表更像是一个可以调整大小的数组。 - 此外,我们实际上没有
for
循环。我们有一些东西需要理解,这只不过是调用map
、flatMap
、filter
的句法糖,在某些情况下是foreach
。
这个清单可以继续下去,但我认为我的观点是明确的。
Scala不仅是新的语法,更是一种不同的编程方式。
无论如何,这是解决您问题的惯用方法。
(不一定这是最好的方法,有很多变化可以做(
// First lets create some custom types / classes to represent your data.
final case class Point(x: Double, y: Double)
final case class ClassifiedPoint(point: Point, clazz: String)
// Lets define the Euclidean distance function.
def euclideanDistance(p1: Point, p2: Point): Double =
math.sqrt(
math.pow((p1.x - p2.x), 2) +
math.pow((p1.y - p2.y), 2)
)
}
// This is what you need.
// Note that I made it somewhat more generic that is has to be.
// For example, instead of using the euclidean distance function directly on the body,
// we receive the distance function to use.
// Also, I use a technique called currying to split the arguments in different lists,
// This allows the caller to partially apply them.
def computeDistance(distanceFun: (Point, Point) => Double)
(trainingList: List[ClassifiedPoint])
(referencePoint: Point): List[(ClassifiedPoint, Double)] =
trainingList.map { classifiedPoint =>
val distance = distanceFun(classifiedPoint.point, referencePoint)
classifiedPoint -> distance
}
你可以像这样使用。
val trainingList = List(
ClassifiedPoint(Point(x = 4.3d, y = 3.0d), clazz = "Iris-setosa"),
ClassifiedPoint(Point(x = 4.4d, y = 3.0d), clazz = "Iris-setosa")
)
// Partial application to create a new function.
val computeEuclideanDistance = computeDistance(euclideanDistance) _
computeEuclideanDistance(trainingList, Point(x = 3.0d, y = 0.0d))
// res: List[(ClassifiedPoint, Double)] =
// List(
// (ClassifiedPoint(Point(4.3, 3.0), "Iris-setosa"), 3.269556544854363),
// (ClassifiedPoint(Point(4.4, 3.0), "Iris-setosa"), 3.3105890714493698)
// )
正如 Luis 所建议的那样,如果可能的话,应避免Any
和var
,因此这里有一个示例,可能会促使您考虑不同的方法
case class Point(x: Double, y: Double, `class`: String)
def distance(a: Point, b: Point): Double =
math.hypot(a.x - b.x, a.y - b.y)
val targetPoint = Point(1,2,"Extra-point")
val training_list : List[((Double, Double, String))] = List((4.3,3.0,"Iris-setosa"), (4.4,3.0,"Iris-setosa"))
val points = training_list.map(Point.tupled)
val unsortedPoints: List[(Point, Double)] = points.map(point => (point, distance(point, targetPoint)))
unsortedPoints.sortBy(_._2)
哪些输出
res0: List[(Point, Double)] = List((Point(4.3,3.0,Iris-setosa),3.4481879299133333), (Point(4.4,3.0,Iris-setosa),3.5440090293338704))
我从泽维尔那里复制了距离计算。