# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Mengimport library dan package machine learning

import findspark
findspark.init()
import pyspark

# +
# mengimport modul yang dibutuhkan
from pyspark.ml.clustering import KMeans
from pyspark.ml.feature import VectorAssembler
from pyspark.sql import SparkSession
from pyspark.sql import SQLContext

#membuat session
appName = "Clustering di Apache Spark 2"
spark = SparkSession \
.builder \
.appName(appName) \
.config("spark.some.config.option", "some-value") \
.getOrCreate()
# -

# # Memuat data kostumer dari file

# memuat data dari file ke DataFrame dengan infer skema
latihan = spark.read.csv('latihan.csv', inferSchema=True, header=True)
latihan.show(10)

# # Menyiapkan data training

# membuat assembler untuk mengubah fitur menjadi satu kolom fitur
assembler = VectorAssembler(inputCols = ["Fitur_X", "Fitur_Y"],
outputCol="features")
train = assembler.transform(latihan).select('Data', 'features')
train.show(truncate = False, n=10)

# # Membuat model k-Means Clustering

# +
# mendefinisikan algoritma clustering
kmeans = KMeans(featuresCol = assembler.getOutputCol(), predictionCol="cluster", k=3, maxIter=3, seed=0)

# mentraining model dengan perintah ".fit()"
model = kmeans.fit(train)
print("Model selesai dibuat!")
# -

# # Mencari nilai titik tengah dari setiap cluster

centers = model.clusterCenters()
print("Cluster Centers: ")
for center in centers:
    print(center)

# # Memprediksi cluster

prediction = model.transform(train) # melakukan prediksi klaster
prediction.groupBy("cluster").count().orderBy("cluster").show()
prediction.select('Data', 'cluster').show(10)

# +
import pyspark.sql.functions as F
from pyspark.sql.functions import *

prediction.withColumn("cluster",col("cluster")+1).show(10)
# -



StarcoderdataJupyterScriptsDedupFiltered