AI programming using python
- Get link
- X
- Other Apps
Sure. Here is a simple Python Machine Learning example with results that is useful for beginners.
Example: Predict Student Pass/Fail using Machine Learning
We will use Logistic Regression to predict whether a student will pass based on study hours.
1. Install libraries
pip install pandas scikit-learn
2. Python Code
import pandas as pd from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score # Dataset data = { "StudyHours": [1, 2, 2.5, 3, 4, 5, 5.5, 6, 7, 8], "Result": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1] } df = pd.DataFrame(data) print("Dataset:") print(df) # Input and output X = df[["StudyHours"]] y = df["Result"] # Split data X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) # Create model model = LogisticRegression() # Train model model.fit(X_train, y_train) # Test model y_pred = model.predict(X_test) # Accuracy accuracy = accuracy_score(y_test, y_pred) print("\nAccuracy:", accuracy) # Predict new students new_students = pd.DataFrame({ "StudyHours": [2, 3.5, 5, 7] }) predictions = model.predict(new_students) print("\nPredictions:") for hours, result in zip(new_students["StudyHours"], predictions): if result == 1: print(hours, "hours -> PASS") else: print(hours, "hours -> FAIL")
3. Sample Result
Dataset: StudyHours Result 0 1.0 0 1 2.0 0 2 2.5 0 3 3.0 0 4 4.0 1 5 5.0 1 6 5.5 1 7 6.0 1 8 7.0 1 9 8.0 1 Accuracy: 1.0 Predictions: 2.0 hours -> FAIL 3.5 hours -> FAIL 5.0 hours -> PASS 7.0 hours -> PASS
How Machine Learning works here
Training Data → Algorithm → Trained Model → Prediction
Study Hours ↓ Machine Learning Model (Logistic Regression) ↓ Result PASS / FAIL
For example:
| Study Hours | Prediction |
|---|---|
| 2 hours | ❌ FAIL |
| 3.5 hours | ❌ FAIL |
| 5 hours | ✅ PASS |
| 7 hours | ✅ PASS |
Note: This is a teaching example with a very small artificial dataset, so the 100% accuracy should not be interpreted as real-world performance.
If you want, I can also give you a real-world Python ML project with dataset, code, output/results, graphs, and explanation such as House Price Prediction, Student Placement Prediction, Diabetes Prediction, or Sales Prediction.
Absolutely. Here is a beginner-friendly Python AI programming example with actual output.
🤖 AI Example: Simple Chatbot using Python
This program creates a basic AI-style chatbot that understands common user questions and responds accordingly.
Python Code
import random # AI chatbot responses responses = { "hello": [ "Hello! How can I help you?", "Hi! Nice to meet you.", "Hey! What can I do for you?" ], "how are you": [ "I'm doing great!", "I'm fine, thank you!", "I'm ready to help you." ], "your name": [ "My name is Python AI Bot.", "You can call me AI Assistant." ], "bye": [ "Goodbye! Have a great day!", "Bye! See you again." ] } def ai_chatbot(message): message = message.lower() # Find matching keywords for key in responses: if key in message: return random.choice(responses[key]) return "Sorry, I don't understand. Please ask me something else." # Chat with AI print("🤖 Python AI Chatbot") print("Type 'bye' to exit.") while True: user = input("You: ") answer = ai_chatbot(user) print("AI:", answer) if "bye" in user.lower(): break
Result
🤖 Python AI Chatbot Type 'bye' to exit. You: hello AI: Hi! Nice to meet you. You: how are you AI: I'm doing great! You: what is your name AI: My name is Python AI Bot. You: tell me something AI: Sorry, I don't understand. Please ask me something else. You: bye AI: Goodbye! Have a great day!
How it works
User Input ↓ Python Program ↓ Keyword Matching ↓ AI Response Selection ↓ Response to User
Another practical AI example
For teaching AI / ML using Python, a stronger next project would be:
Student Placement Prediction
Student Details ↓ Python AI Model ↓ Skills + Marks + Internship ↓ Placement Prediction ↓ "Likely to be Placed" / "Needs Improvement"
I can also provide the complete Student Placement AI project with dataset + Python code + training + accuracy + prediction results + graph.
- Get link
- X
- Other Apps
Comments
Post a Comment