I'm beginner working on python machine learning using google colab . i face issue i can't apply decision tree regression on excel file
price is output or Y
other fields on excel is input
so
How to apply decision tree regression for code below ?
i need to know how to write part related to decision tree regression after clean data ?
what i try
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
df = pd.read_excel('Data_Train.xlsx')
# For example, if there are missing values, you can fill them with the mean or median.
# df.fillna(df.mean(), inplace=True)
X = df.drop('price', axis=1) # Input features
Y = df['price'] # Output variable
# Here I split the data into training and testing sets
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
file attached with post
/forums/uploadfile/e2e758/01302024213902PM/Data_Train.zip
/forums/uploadfile/e2e758/01302024213902PM/Data_Train.xlsx
Tuhin PaulPosted Feb 2, 2024, 3:14 PM
To apply Decision Tree Regression on your dataset using Python and scikit-learn, check once these things:
Jayraj ChhayaPosted Feb 1, 2024, 5:53 AM
First, you need to import the
DecisionTreeRegressorclass from thesklearn.treemodule. You can do this by adding the following line of code at the beginning of your code:Next, you can create an instance of the
DecisionTreeRegressorclass and specify any parameters you want to use. For example, you can set themax_depthparameter to control the maximum depth of the decision tree. Here's an example of how you can create the decision tree regressor:After creating the regressor, you can fit it to your training data using the
fitmethod. This will train the decision tree on your training data. Here's how you can fit the regressor:Once the regressor is trained, you can use it to make predictions on your test data using the
predictmethod. Here's how you can make predictions:Finally, you can evaluate the performance of your model by calculating the mean squared error between the predicted values and the actual values. You can use the
mean_squared_errorfunction from thesklearn.metricsmodule to do this. Here's how you can calculate the mean squared error:Now that we have added the decision tree regression part to your code, here's the updated code:
Now you should be able to apply decision tree regression to your Excel file and obtain predictions for the output variable. Remember to adjust the parameters of the
DecisionTreeRegressorclass according to your specific needs.