decision tree(soyabean)

PHOTO EMBED

Mon Nov 18 2024 10:20:34 GMT+0000 (Coordinated Universal Time)

Saved by @wtlab

# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the dataset
data = pd.read_csv('/content/Soybean (1).csv')  # Replace 'soybean.csv' with your actual file name

# Split the data into features (X) and target (y)
X = data.drop(columns=['Class'])  # 'Class' is the target column
y = data['Class']

# 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=42)

# Create and train the Decision Tree model
model = DecisionTreeClassifier(random_state=42)
model.fit(X_train, y_train)

# Predict on the test set
y_pred = model.predict(X_test)

# Calculate and print the accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy of the Decision Tree model: {accuracy * 100:.2f}%")
content_copyCOPY