gpt4 book ai didi

java - Scala:在类中找不到主要方法

转载 作者:可可西里 更新时间:2023-11-01 16:21:32 29 4
gpt4 key购买 nike

我是 scala 和 java 的新手。我正在为 K-Means 聚类编写以下代码,但出现以下错误:

Error: Main method not found in class kmeans, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

请帮助我解决这个问题。

我的 .scala 代码:

object kmeans {


import scala.math.pow
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf

def distanceSquared(p1: (Double, Double), p2: (Double, Double)) = {
pow(p1._1 - p2._1, 2) + pow(p1._2 - p2._2, 2)
}

def addPoints(p1: (Double, Double), p2: (Double, Double)) = {
(p1._1 + p2._1, p1._2 + p2._2)
}

def closestPoint(p: (Double, Double), points: Array[(Double, Double)]): Int = {
var index = 0
var bestIndex = 0
var closest = Double.PositiveInfinity

for (i <- 0 until points.length) {
val dist = distanceSquared(p, points(i))
if (dist < closest) {
closest = dist
bestIndex = i
}
}
bestIndex
}


def main(args: Array[String]) {

val conf = new SparkConf().setAppName("First Scala app").setMaster("local[*]")
val sc = new SparkContext(conf)

val filename = "loudacre/*"

val K = 5

val convergeDist = .1

val fileRdd = sc.textFile(filename)
val pairLatLongRdd = fileRdd.map(line => line.split(',')).map(pair => (pair(3).toDouble, pair(4).toDouble)).filter(point => !((point._1 == 0) && (point._2 == 0))).
persist()

println(pairLatLongRdd.count())

for ((a, b) <- pairLatLongRdd.take(2)) {

println("Lat: " + a + " Long : " + b);

}

var kPoints = pairLatLongRdd.takeSample(false, K, 42)

println("K Center points initialized :");

for ((a, b) <- kPoints) {

println("Lat: " + a + " Long : " + b);

}
var tempDist = Double.PositiveInfinity

while (tempDist > convergeDist) {

val closestToKpointRdd = pairLatLongRdd.map(point => (closestPoint(point, kPoints), (point, 1)))

val pointCalculatedRdd = closestToKpointRdd.reduceByKey { case ((point1, n1), (point2, n2)) => (addPoints(point1, point2), n1 + n2) }

val newPoints = pointCalculatedRdd.map { case (i, (point, n)) => (i, (point._1 / n, point._2 / n)) }.collectAsMap()

tempDist = 0.0

for (i <- 0 until K) {
tempDist += distanceSquared(kPoints(i), newPoints(i))

}

println("Distance between iterations: " + tempDist);

for (i <- 0 until K) {

kPoints(i) = newPoints(i)

}
}

println("Final center points :");

for (point <- kPoints) {
println(point);
}

val deviceRdd = fileRdd.map(line => line.split(',')).map(pair => (pair(1), (pair(3).toDouble, pair(4).toDouble))).filter(device => !((device._2._1 == 0) && (device._2._2 == 0))).
persist()

var points = deviceRdd.takeSample(false, 10, 42)

for ((device, point) <- points) {

val k = closestPoint(point, kPoints)
println("device: " + device + " to K: " + k);

}

sc.stop()
}

}

最佳答案

您可能在主声明后缺少 =。

关于java - Scala:在类中找不到主要方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628165/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com