0% found this document useful (0 votes)
3 views

Decision Tree

The document outlines a Python script that uses a Decision Tree Classifier to predict rainy days based on historical rainfall data. It involves loading and preprocessing data, extracting relevant features, and splitting the dataset into training and test sets. The model is trained, evaluated for accuracy, and used to predict rainfall for a future date.

Uploaded by

popsthenu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Decision Tree

The document outlines a Python script that uses a Decision Tree Classifier to predict rainy days based on historical rainfall data. It involves loading and preprocessing data, extracting relevant features, and splitting the dataset into training and test sets. The model is trained, evaluated for accuracy, and used to predict rainfall for a future date.

Uploaded by

popsthenu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Decision tree:

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier

from sklearn.metrics import accuracy_score, classification_report

# Load and preprocess data

data = pd.read_csv('tn-rainfall.csv')

# Convert date column to datetime with explicit format or dayfirst

data['date'] = pd.to_datetime(data['date'], format='%d-%m-%Y')

# Extract features

data['Month'] = data['date'].dt.month

data['Year'] = data['date'].dt.year

# Create target variable based on threshold

threshold = 5

data['Rainy Day'] = (data['value'] >= threshold).astype(int)

# Prepare features (X) and target (y)

X = data[['Month', 'Year']]

y = data['Rainy Day']

# Split data into training and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train Decision Tree model

model = DecisionTreeClassifier()

model.fit(X_train, y_train)
# Evaluate model

y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)

report = classification_report(y_test, y_pred)

print('Accuracy:', accuracy)

print(report)

# Predict for a future date

future_date = pd.to_datetime('2025-02-03')

future_features = pd.DataFrame({'Month': [future_date.month], 'Year': [future_date.year]})

future_prediction = model.predict(future_features)

print('Predicted class for', future_date.date(), ':', future_prediction[0])

You might also like