0% found this document useful (0 votes)
2 views3 pages

Script de Python

Uploaded by

jlvilchez1978
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Script de Python

Uploaded by

jlvilchez1978
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

script de Python

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from [Link] import LabelEncoder, StandardScaler

from [Link] import confusion_matrix, classification_report

from [Link] import KNeighborsClassifier

from [Link] import SVC

from [Link] import DecisionTreeClassifier

# =========================

# a) Carregar dades

# =========================

df = pd.read_csv("ObesityDataSet_raw_and_data_sinthetic.csv")

print("Primeres files del dataset:")

print([Link]())

print("\nInformació general:")

print([Link]())

# =========================

# b) Transformació a numèric

# =========================

cols_categoricals = [

"family_history_with_overweight", "FAVC", "CAEC",

"SMOKE", "SCC", "CALC", "MTRANS", "NObeyesdad", "Gender"

]
le = LabelEncoder()

for col in cols_categoricals:

df[col] = le.fit_transform(df[col])

# FCVC ja és numèrica però si cal assegurar:

df["FCVC"] = pd.to_numeric(df["FCVC"], errors='coerce')

# =========================

# c) Eliminar columna Weight

# =========================

df = [Link](columns=["Weight"])

# =========================

# d) Train / Test split

# =========================

X = [Link]("NObeyesdad", axis=1)

y = df["NObeyesdad"]

# Escalat (important per KNN i SVM)

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(

X_scaled, y, test_size=0.2, random_state=42

# =========================

# e) Models

# =========================
# --- KNN ---

knn = KNeighborsClassifier(n_neighbors=5)

[Link](X_train, y_train)

y_pred_knn = [Link](X_test)

print("\n===== KNN =====")

print(confusion_matrix(y_test, y_pred_knn))

print(classification_report(y_test, y_pred_knn))

# --- SVM ---

svm = SVC(kernel='rbf')

[Link](X_train, y_train)

y_pred_svm = [Link](X_test)

print("\n===== SVM =====")

print(confusion_matrix(y_test, y_pred_svm))

print(classification_report(y_test, y_pred_svm))

# --- Decision Tree ---

tree = DecisionTreeClassifier(random_state=42)

[Link](X_train, y_train)

y_pred_tree = [Link](X_test)

print("\n===== Decision Tree =====")

print(confusion_matrix(y_test, y_pred_tree))

print(classification_report(y_test, y_pred_tree))

You might also like