Mixed Columns
train has columns age and income (numbers, some missing), city (a name) and bought (0 or 1). new has the same columns without bought, and may hold cities never seen in training.
Build one pipeline that:
1. fills missing numbers with the column median and then standardizes them,
2. one-hot encodes city, ignoring unknown cities,
3. fits a logistic regression on the result.
Fit it on train and return its predictions for new.
Example 1
train {"age":[22,25,null,31,35,38,41,44,47,52,55,58,23,29,33,36,43,49,53,60],"income":[30,32,35,null,41,44,60,62,65,70,72,75,28,36,40,43,61,66,null,80],"city":["A","B","A","B","A","B","A","B","A","B","A","B","B","A","B","A","B","A","B","A"],"bought":[0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1]}
new {"age":[24,57,null],"income":[29,null,78],"city":["A","C","B"]}
returns [0,1,1]
Older, better paid people bought. The first new person is young and poorly paid. The other two are old or well paid, and their missing numbers and unknown city do not get in the way.
Constraints
20 <= len(train) <= 601 <= len(new) <= 8- No new row is a close call
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More scikit-learn problems
- Pick the PenaltyHard
- Right Amount of BendHard
- Scored in Time OrderHard
- Spam or NotHard
- Tune Then TestHard
- Hold Some BackBeginner
- Learn Then PredictBeginner
- Names to NumbersBeginner