32. Features and Labels in Machine Learning
32.4 Building a Tiny Feature Table in pandas
Why. Your hands need one tiny pandas build before sklearn models arrive.
What. Build X and y from a clean fictional table.
How.
import pandas as pd
raw = pd.DataFrame({
"order_id": ["BLK-1", "BLK-2", "BLK-3", "BLK-4", "BLK-5", "BLK-6"],
"city": ["Pune", "Solapur", "Pune", "Nashik", "Kolhapur", "Nagpur"],
"items": [2, 6, 3, 5, 4, 8],
"distance_km": [1.1, 7.5, 2.0, 3.4, 4.2, 5.1],
"rain_mm": [0, 10, 0, 0, 2, 0],
"was_late": [0, 1, 0, 0, 1, 1],
})
feature_cols = ["items", "distance_km", "rain_mm"]
X = raw[feature_cols].copy()
y = raw["was_late"].copy()
print(X)
print(y.value_counts())
What you should see. X has only allowed inputs. y is the 0/1 label. order_id and city stayed out for this tiny numeric-only demo (city can return later as a category feature).
Ravindra Bagale's Tip
Write your feature column names once, in one list such as feature_cols, and store it in a single variable. Use this same list for train_test_split, for training the model and for making predictions. When you reuse one list, you avoid copy-paste mistakes in column names.
Ravindra Bagale's Tip – मराठी
तुमच्या feature columns ची नावं feature_cols सारख्या एका list मध्ये, एकाच variable मध्ये ठेवा. हीच list train_test_split साठी, model train करण्यासाठी आणि predictions करण्यासाठी वापरा. एकच list पुन्हा पुन्हा वापरली की column नावांच्या copy-paste चुका टळतात.
Ravindra Bagale's Tip – हिंदी
अपने feature columns के नाम feature_cols जैसी एक list में, एक ही variable में रखो. यही list train_test_split के लिए, model train करने के लिए और predictions के लिए इस्तेमाल करो. जब तुम एक ही list बार-बार इस्तेमाल करते हो, तो column नामों की copy-paste गलतियाँ नहीं होतीं.