Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Simple python trainable AI program for beginners

Posted on March 28, 2025March 28, 2025 by Habibie

Here’s a simple trainable AI program using Python that utilizes a basic machine learning model. This example demonstrates a text classifier using the sklearn library, where the AI can be trained to classify text into different categories.

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
import joblib

def train_model():
    # Sample training data (text and labels)
    texts = ["hello", "hi", "greetings", "bye", "farewell", "see you"]
    labels = ["greeting", "greeting", "greeting", "farewell", "farewell", "farewell"]
    
    # Create a text classification pipeline
    model = make_pipeline(CountVectorizer(), MultinomialNB())
    
    # Train the model
    model.fit(texts, labels)
    
    # Save the trained model
    joblib.dump(model, "text_classifier.pkl")
    print("Model trained and saved as text_classifier.pkl")

def predict(text):
    # Load the trained model
    model = joblib.load("text_classifier.pkl")
    
    # Make a prediction
    label = model.predict([text])[0]
    print(f"Predicted category: {label}")
    return label

if __name__ == "__main__":
    train_model()
    
    # Test the model
    user_input = input("Enter a text to classify: ")
    predict(user_input)

This program:

  1. Trains a simple text classifier using the Naïve Bayes algorithm.
  2. Saves the trained model for future use.
  3. Loads the model to classify user input.

Try running the script and entering words like “hello” or “bye” to see how it predicts the category.

Post Views: 236
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme