Python machine learning
- Get link
- X
- Other Apps
Python machine learning example with results
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.
- Get link
- X
- Other Apps
Comments
Post a Comment