SEP- AIA

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 . . .

 
  
 3. Write a program to demonstrate a Decision Tree Classifier to classify data (e.g., Apple vs.
Orange based on weight/texture) using hardcoded data

# 3. Python Program to Demonstrate Decision Tree Classifier

### Program

```python id="p9v2qk"
# Decision Tree Classifier
# Apple vs Orange Classification

from sklearn.tree import DecisionTreeClassifier

# Hardcoded dataset
# Features: [Weight in grams, Texture]
# Texture: 0 = Smooth, 1 = Rough

X = [
    [150, 0],
    [160, 0],
    [170, 0],
    [180, 0],
    [140, 1],
    [130, 1],
    [120, 1],
    [135, 1]
]

# 0 = Apple, 1 = Orange
y = [0, 0, 0, 0, 1, 1, 1, 1]

# Create Decision Tree model
model = DecisionTreeClassifier()

# Train the model
model.fit(X, y)

# Get input from user
weight = float(input("Enter weight of fruit (grams): "))
texture = int(input("Enter texture (0 = Smooth, 1 = Rough): "))

# Predict the fruit
prediction = model.predict([[weight, texture]])

if prediction[0] == 0:
    print("Prediction: Apple")
else:
    print("Prediction: Orange")
```

### Sample Output

```text id="r6d5j3"
Enter weight of fruit (grams): 155
Enter texture (0 = Smooth, 1 = Rough): 0
Prediction: Apple
```

### Another Sample Output

```text id="1j4h0b"
Enter weight of fruit (grams): 135
Enter texture (0 = Smooth, 1 = Rough): 1
Prediction: Orange
```

### Explanation

* `X` contains the input features: **weight** and **texture**.
* `y` contains the class labels: **0 = Apple** and **1 = Orange**.
* `DecisionTreeClassifier()` creates the decision tree model.
* `fit()` trains the model using the hardcoded dataset.
* `predict()` classifies a new fruit as **Apple** or **Orange**.