-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans_specieswise.py
More file actions
34 lines (26 loc) · 1.46 KB
/
Copy pathkmeans_specieswise.py
File metadata and controls
34 lines (26 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#this code is sent by vineet and it has species recorded individually
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.clustering import KMeans
from pyspark.sql.functions import col
from pyspark.sql import SparkSession
# Create Spark session
spark = SparkSession.builder.appName("KMeansWildlifeClustering").getOrCreate()
# Load cleaned data from the previous step
file_path = 'dataset/output.csv/part-00000-f8f7d0bc-a5bc-40e3-a806-dc5dfb385e15-c000.csv' # Path to cleaned CSV data
df_cleaned = spark.read.csv(file_path, header=True, inferSchema=True)
# Step 1: Assemble the features (latitude and longitude)
assembler = VectorAssembler(inputCols=['decimalLatitude', 'decimalLongitude'], outputCol='features')
df_features = assembler.transform(df_cleaned)
# Step 2: Create and train the KMeans model
k = 6 # Example value, you can adjust based on your elbow plot analysis
kmeans = KMeans(k=k, seed=1, featuresCol='features', predictionCol='prediction')
model = kmeans.fit(df_features)
# Step 3: Make predictions
df_predictions = model.transform(df_features)
# Step 4: Select relevant columns (including species and cluster prediction)
df_result = df_predictions.select('species', 'decimalLatitude', 'decimalLongitude', 'prediction')
# Step 5: Save the result to a new CSV file
output_path = 'dataset/kmeans_specieswise.csv'
df_result.coalesce(1).write.csv(output_path, header=True, mode='overwrite')
# Stop Spark session
spark.stop()