File size: 1,404 Bytes
3eda752
8a2ad21
 
 
 
 
 
3eda752
 
 
 
 
 
 
 
 
 
 
 
 
 
bc29722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
language:
- en
pipeline_tag: text-classification
library_name: sklearn
---
---
license: mit

Summary of Code Functionality:

The code takes numbers from 1 to 100 and converts them into English words using the inflect library.

Then, it encodes those words into numerical labels using LabelEncoder.

A DecisionTreeClassifier is trained to learn the mapping from numbers (1–100) to their word forms.

Finally, it predicts the word for any given number (like 45) and decodes it back to its word using the encoder--

Example:

45 → Model predicts label → Decoder converts to "forty-five"






Usage:

from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import LabelEncoder

# Create input numbers from 1 to 100
X = [[i] for i in range(1, 1000)]

# Create corresponding output words
def number_to_word(n):
    import inflect
    p = inflect.engine()
    return p.number_to_words(n)

y = [number_to_word(i) for i in range(1, 1000)]

# Encode the output words to numbers
le = LabelEncoder()
y_encoded = le.fit_transform(y)

# Train the ML model
model = DecisionTreeClassifier()
model.fit(X, y_encoded)

# Predict: Try with any number from 1 to 100
input_number = [[345]]  # Change this value as needed
predicted_encoded = model.predict(input_number)
predicted_word = le.inverse_transform(predicted_encoded)

print(f"Input: {input_number[0][0]} → Output: {predicted_word[0]}")