0% found this document useful (0 votes)
3 views2 pages

Decision Tree Regression in Python

Uploaded by

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

Decision Tree Regression in Python

Uploaded by

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

# Importing necessary libraries

import pandas as pd # For data manipulation and analysis


from sklearn.model_selection import train_test_split # To split the dataset into
training and testing sets
from [Link] import DecisionTreeRegressor # Importing Decision Tree Regressor
model for regression tasks
from sklearn import metrics # For evaluating the performance of the model

# Load the dataset from a CSV file


dataset = pd.read_csv("E:\\Msc\\Advanced Artificial Intelligence\\Python\\Desicsion
Tree Regression\\[Link]")
# Load a CSV file into a Pandas DataFrame, replace the file path with the actual
path to your dataset

# Separate the features (X) and target variable (Y)


x = [Link][:, :7].astype(int) # Select the first 7 columns as features and
convert data to integers
print(x) # Display the features

y = [Link][:, 7].astype(int) # Select the 8th column as the target variable


and convert to integers
print(y) # Display the target variable

# Split the dataset into training and testing sets


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3,
random_state=1)
# `test_size=0.3` means 30% of the data is used for testing, and `random_state=1`
ensures reproducibility

# Initialize and train the Decision Tree Regressor


regressor = DecisionTreeRegressor() # Create a Decision Tree Regressor instance
regressor1 = [Link](x_train, y_train) # Fit the model to the training data

# Make predictions on the testing set


y_predict = [Link](x_test) # Predict the target variable for the test
set
print(y_predict) # Display the predicted values

# Evaluate the model's performance


print("Accuracy:", metrics.accuracy_score(y_test, y_predict))
# `accuracy_score` evaluates the performance.
# Note: This is incorrect for regression tasks; you should use metrics like
`r2_score` or `mean_squared_error`.

# Visualize the Decision Tree


from matplotlib import pyplot as plt # For plotting
from sklearn import tree # To visualize the decision tree

fig = [Link]() # Create a new figure for plotting


tree.plot_tree(regressor1) # Plot the decision tree
[Link]() # Display the tree plot
[Link]("[Link]") # Save the tree plot as an image file named "[Link]"

# Predict output for a new data point


new_input = [Link]([[90, 80, 10, 0, 28, 0.2, 35]], columns=[Link])
# Create a new DataFrame for a single input example with values matching the
feature columns
new_output = [Link](new_input) # Predict the target variable for the
new input
print(new_output) # Display the predicted output

You might also like