This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importamos las librerías necesarias | |
from pyspark.sql import SparkSession | |
from pyspark.ml.feature import VectorAssembler | |
from pyspark.ml.classification import GBTClassifier | |
from pyspark.ml.evaluation import MulticlassClassificationEvaluator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creamos una sesión de Spark | |
spark = SparkSession.builder.appName("GBTClassifier").getOrCreate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cargamos el conjunto de datos (usaremos el conjunto de datos "Iris" de UCI) | |
data = spark.read.format("csv").option("header", "true").option("inferSchema", "true").load("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Seleccionamos las columnas necesarias y las renombramos | |
data = data.selectExpr("_c0 as sepal_length", "_c1 as sepal_width", "_c2 as petal_length", "_c3 as petal_width", "_c4 as species") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convertimos las variables de entrada en un vector de características | |
assembler = VectorAssembler(inputCols=["sepal_length", "sepal_width", "petal_length", "petal_width"], outputCol="features") | |
data = assembler.transform(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convertimos la variable objetivo en un índice numérico | |
labelIndexer = StringIndexer(inputCol="species", outputCol="label") | |
data = labelIndexer.fit(data).transform(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dividimos el conjunto de datos en conjuntos de entrenamiento y prueba | |
(trainingData, testData) = data.randomSplit([0.7, 0.3], seed=123) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Creamos un modelo de Gradient Boosted Trees | |
gbt = GBTClassifier(labelCol="label", featuresCol="features", maxIter=10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Entrenamos el modelo con el conjunto de entrenamiento | |
model = gbt.fit(trainingData) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Obtenemos las predicciones con el conjunto de prueba | |
predictions = model.transform(testData) |
OlderNewer