Choose an AI Type
Type | Description | Example |
---|---|---|
Rule-Based AI | Follows predefined if-else rules | Chatbot, Decision Tree |
Machine Learning (ML) AI | Learns from data | Image classifier, Spam detector |
Chatbot (NLP) | Uses natural language processing | Customer support bot |
1. Build a Simple Rule-Based AI (No ML)
A basic AI that responds based on rules (like a chatbot).
Example: A Weather Advice Bot
python
def weather_advisor(weather): weather = weather.lower() if weather == "sunny": return "Wear sunscreen and sunglasses!" elif weather == "rainy": return "Take an umbrella and a raincoat." elif weather == "cold": return "Wear a warm jacket and gloves." else: return "I'm not sure, check the weather again." # Test the AI user_input = input("What's the weather today? (sunny/rainy/cold): ") print(weather_advisor(user_input))
Output:
text
What's the weather today? (sunny/rainy/cold): sunny Wear sunscreen and sunglasses!
2. Build a Simple ML-Based AI (Using Scikit-Learn)
A machine learning model that predicts outcomes from data.
Example: A Spam Detector
Step 1: Install Required Libraries
bash
pip install scikit-learn pandas numpy
Step 2: Train a Model
python
import pandas as pd from sklearn.feature_extraction.text import CountVectorizer from sklearn.naive_bayes import MultinomialNB # Sample dataset (message, label: 0=Not Spam, 1=Spam) data = { "message": [ "Free prize! Click now!", "Meeting at 3 PM", "Win a million dollars!", "Project update" ], "label": [1, 0, 1, 0] } df = pd.DataFrame(data) # Convert text to numbers (Bag of Words) vectorizer = CountVectorizer() X = vectorizer.fit_transform(df["message"]) # Train a Naive Bayes classifier model = MultinomialNB() model.fit(X, df["label"]) # Test the AI test_message = ["Free vacation offer!"] test_X = vectorizer.transform(test_message) prediction = model.predict(test_X) print("Spam" if prediction[0] == 1 else "Not Spam")
Output:
text
Spam
3. Build a Simple AI Chatbot (Using NLP)
A chatbot that responds to user input (using NLTK or ChatterBot).
Example: A Basic Chatbot with ChatterBot
bash
pip install chatterbot chatterbot_corpus
python
from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer # Create a chatbot chatbot = ChatBot("SimpleBot") # Train it on English data trainer = ChatterBotCorpusTrainer(chatbot) trainer.train("chatterbot.corpus.english") # Chat with the AI while True: user_input = input("You: ") if user_input.lower() == "exit": break response = chatbot.get_response(user_input) print("Bot:", response)
Output:
text
You: Hello Bot: Hi there! You: How are you? Bot: I am doing well, thank you! You: exit
4. Next Steps
- Improve with more data (for ML models).
- Use deep learning (TensorFlow/PyTorch) for complex AI.
- Deploy as a web app (Flask, FastAPI).