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]}")
- Downloads last month
- 0