Skip to content

Commit

Permalink
get test works
Browse files Browse the repository at this point in the history
  • Loading branch information
ympaik87 committed Jan 12, 2022
1 parent da64bc5 commit ca778cf
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ aws_keys.csv
starter/data/census.csv
starter/data/census_cleaned.csv

.vscode
.DS_Store
.ipynb_checkpoints
.pytest_cache
Expand Down
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

58 changes: 17 additions & 41 deletions starter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,22 @@


class InferenceRequest(BaseModel):
age: int
workclass: str
fnlgt: int
education: str
# education_num: int = Field(alias="education-num")
# marital_status: str = Field(alias="marital-status")
education_num: int
marital_status: str
occupation: str
relationship: str
race: str
sex: str
# capital_gain: int = Field(alias="capital-gain")
# capital_loss: int = Field(alias="capital-loss")
# hours_per_week: int = Field(alias="hours-per-week")
# native_country: str = Field(alias="native-country")
capital_gain: int
capital_loss: int
hours_per_week: int
native_country: str
age: int = Field(..., example=25)
workclass: str = Field(..., example="Never-married")
fnlgt: int = Field(..., example=77516)
education: str = Field(..., example="Bachelors")
education_num: int = Field(..., alias="education-num", example=13)
marital_status: str = Field(
..., alias="marital-status", example="Divorced")
occupation: str = Field(..., example="Adm-clerical")
relationship: str = Field(..., example="Husband")
race: str = Field(..., example="White")
sex: str = Field(..., example="Male")
capital_gain: int = Field(..., alias="capital-gain", example=0)
capital_loss: int = Field(..., alias="capital-loss", example=0)
hours_per_week: int = Field(..., alias="hours-per-week", example=40)
native_country: str = Field(
..., alias="native-country", example="United-States")


@app.get('/')
Expand All @@ -54,38 +50,18 @@ async def create_item(item: dict):

@app.post('/predict')
async def get_prediction(request_data: InferenceRequest):
# async def get_prediction(request_data: InferenceRequest = Body(
# ...,
# example={
# "age": 39,
# "workclass": "State-gov",
# "fnlgt": 77516,
# "education": "Bachelors",
# "education-num": 13,
# "marital-status": "Never-married",
# "occupation": "Adm-clerical",
# "relationship": "Not-in-family",
# "race": "White",
# "sex": "Male",
# "capital-gain": 2174,
# "capital-loss": 0,
# "hours-per-week": 40,
# "native-country": "United-States"
# }
# )):
cwd_p = os.getcwd()
trained_model = joblib.load(f"{cwd_p}/starter/model/model_trained.joblib")
encoder = joblib.load(f"{cwd_p}/starter/model/encoder.joblib")
labels = joblib.load(f"{cwd_p}/starter/model/lb.joblib")

request_dict = request_data.dict(by_alias=True)
request_df = pd.DataFrame(request_dict, index=[0])
processed_data, _, _, _ = process_data(
request_df, categorical_features=CAT_FEATURES, label=None,
training=False, encoder=encoder, lb=labels
)
preds = inference(trained_model, np.array(processed_data))
return {"Predicted salary": preds[0]}
return {"Predicted salary": str(preds[0])}


if __name__ == "__main__":
Expand Down
Binary file modified starter/model/encoder.joblib
Binary file not shown.
Binary file modified starter/model/model_trained.joblib
Binary file not shown.
180 changes: 90 additions & 90 deletions starter/model/slice_output.txt
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
feature,value,precision,recall,fbeta_score
workclass,State-gov,0.9339080459770115,0.9447674418604651,0.9393063583815029
workclass,Self-emp-not-inc,0.9560117302052786,0.9131652661064426,0.9340974212034385
workclass,Private,0.9535229202037352,0.9214520098441346,0.9372131831455988
workclass,Federal-gov,0.9482288828337875,0.9534246575342465,0.9508196721311476
workclass,Local-gov,0.9330065359477124,0.9376026272577996,0.9352989352989352
workclass,Self-emp-inc,0.964824120603015,0.96,0.9624060150375939
workclass,State-gov,0.8006644518272426,0.7005813953488372,0.7472868217054263
workclass,Self-emp-not-inc,0.7546468401486989,0.5686274509803921,0.6485623003194888
workclass,Private,0.8260173021467478,0.5287120590648072,0.6447417781668124
workclass,Federal-gov,0.8471615720524017,0.5315068493150685,0.6531986531986532
workclass,Local-gov,0.7638603696098563,0.6108374384236454,0.6788321167883211
workclass,Self-emp-inc,0.8653061224489796,0.7066666666666667,0.7779816513761468
workclass,Without-pay,1.0,1.0,1.0
education,Bachelors,0.9478138222849083,0.9482596425211665,0.9480366799905948
education,HS-grad,0.9447368421052632,0.8880643166357453,0.9155243863563916
education,11th,0.9777777777777777,0.7457627118644068,0.8461538461538463
education,Masters,0.9759036144578314,0.9705882352941176,0.9732386673948662
education,9th,1.0,0.72,0.8372093023255813
education,Some-college,0.9393706830391404,0.9161676646706587,0.9276241000378931
education,Assoc-acdm,0.9473684210526315,0.9140625,0.9304174950298211
education,7th-8th,1.0,0.8857142857142857,0.9393939393939393
education,Doctorate,0.968421052631579,0.9857142857142858,0.976991150442478
education,Assoc-voc,0.9395770392749244,0.9040697674418605,0.9214814814814815
education,Prof-school,0.9754299754299754,0.9778325123152709,0.976629766297663
education,5th-6th,1.0,0.9166666666666666,0.9565217391304348
education,10th,0.92,0.7796610169491526,0.8440366972477064
education,Bachelors,0.7413862991487636,0.8603010348071496,0.796429349009362
education,HS-grad,0.9823529411764705,0.20655534941249226,0.341338783852836
education,11th,1.0,0.288135593220339,0.4473684210526315
education,Masters,0.8195718654434251,0.8758169934640523,0.8467614533965244
education,9th,1.0,0.08,0.14814814814814814
education,Some-college,0.9318181818181818,0.27619760479041916,0.42609699769053117
education,Assoc-acdm,0.9238095238095239,0.37890625,0.5373961218836565
education,7th-8th,1.0,0.11428571428571428,0.20512820512820512
education,Doctorate,0.8650519031141869,0.8928571428571429,0.8787346221441126
education,Assoc-voc,0.9545454545454546,0.30523255813953487,0.46255506607929514
education,Prof-school,0.8827586206896552,0.9458128078817734,0.9131985731272295
education,5th-6th,1.0,0.3333333333333333,0.5
education,10th,1.0,0.13559322033898305,0.23880597014925373
education,Preschool,1.0,1.0,1.0
education,12th,1.0,0.8275862068965517,0.9056603773584906
education,1st-4th,1.0,1.0,1.0
marital-status,Never-married,0.9782082324455206,0.8595744680851064,0.9150622876557191
marital-status,Married-civ-spouse,0.9476182940338661,0.9357712142522269,0.9416574933165592
marital-status,Divorced,0.9739952718676123,0.911504424778761,0.9417142857142857
marital-status,Married-spouse-absent,1.0,0.8709677419354839,0.9310344827586207
marital-status,Separated,1.0,0.8181818181818182,0.9
marital-status,Married-AF-spouse,0.9090909090909091,1.0,0.9523809523809523
marital-status,Widowed,0.9859154929577465,0.875,0.9271523178807948
occupation,Adm-clerical,0.9386503067484663,0.9216867469879518,0.9300911854103343
occupation,Exec-managerial,0.958656330749354,0.9576664945792462,0.9581611570247933
occupation,Handlers-cleaners,0.9583333333333334,0.8313253012048193,0.8903225806451613
occupation,Prof-specialty,0.9532195927352779,0.9563776918829376,0.9547960308710034
occupation,Other-service,0.9823008849557522,0.8409090909090909,0.9061224489795919
occupation,Sales,0.9553191489361702,0.9257731958762887,0.9403141361256545
occupation,Transport-moving,0.9326599326599326,0.8683385579937304,0.8993506493506493
occupation,Farming-fishing,0.9789473684210527,0.808695652173913,0.8857142857142857
occupation,Machine-op-inspct,0.9555555555555556,0.8775510204081632,0.9148936170212767
occupation,Tech-support,0.9416058394160584,0.9280575539568345,0.9347826086956521
occupation,Craft-repair,0.9442467378410438,0.8766519823788547,0.9091947458595089
occupation,Protective-serv,0.9259259259259259,0.9523809523809523,0.9389671361502347
education,12th,1.0,0.20689655172413793,0.34285714285714286
education,1st-4th,1.0,0.3333333333333333,0.5
marital-status,Never-married,1.0,0.32978723404255317,0.496
marital-status,Married-civ-spouse,0.8026616760241214,0.6032192530082825,0.6887937187723054
marital-status,Divorced,0.9859154929577465,0.30973451327433627,0.47138047138047134
marital-status,Married-spouse-absent,1.0,0.3548387096774194,0.5238095238095238
marital-status,Separated,1.0,0.3484848484848485,0.5168539325842696
marital-status,Married-AF-spouse,1.0,0.2,0.33333333333333337
marital-status,Widowed,1.0,0.3,0.4615384615384615
occupation,Adm-clerical,0.7702127659574468,0.3634538152610442,0.4938608458390178
occupation,Exec-managerial,0.850909090909091,0.7248322147651006,0.7828268748257596
occupation,Handlers-cleaners,0.6774193548387096,0.25301204819277107,0.368421052631579
occupation,Prof-specialty,0.8050704225352112,0.7890668139149641,0.7969882877858337
occupation,Other-service,0.9583333333333334,0.17424242424242425,0.29487179487179493
occupation,Sales,0.7809239940387481,0.5402061855670103,0.6386349786715417
occupation,Transport-moving,0.8041237113402062,0.2445141065830721,0.375
occupation,Farming-fishing,0.8947368421052632,0.4434782608695652,0.5930232558139534
occupation,Machine-op-inspct,0.7971014492753623,0.22448979591836735,0.3503184713375796
occupation,Tech-support,0.8285714285714286,0.4172661870503597,0.5550239234449761
occupation,Craft-repair,0.7932098765432098,0.2830396475770925,0.4172077922077922
occupation,Protective-serv,0.8131868131868132,0.3523809523809524,0.4916943521594684
occupation,Armed-Forces,1.0,1.0,1.0
occupation,Priv-house-serv,1.0,1.0,1.0
relationship,Not-in-family,0.977088948787062,0.8809234507897934,0.926517571884984
relationship,Husband,0.9477718360071301,0.9362563831660503,0.9419789175303394
relationship,Wife,0.9437229437229437,0.9423631123919308,0.943042537851478
relationship,Own-child,0.98,0.765625,0.8596491228070174
relationship,Unmarried,0.9895287958115183,0.8873239436619719,0.9356435643564357
relationship,Other-relative,0.96875,0.8857142857142857,0.9253731343283582
race,White,0.9520814615154238,0.9296680801286737,0.9407412887475032
race,Black,0.9463276836158192,0.9153005464480874,0.9305555555555556
race,Asian-Pac-Islander,0.9658119658119658,0.9112903225806451,0.9377593360995851
race,Amer-Indian-Eskimo,0.9090909090909091,0.8823529411764706,0.8955223880597014
race,Other,0.8421052631578947,0.7619047619047619,0.8
sex,Male,0.9505187549880287,0.9310506566604128,0.9406839902061449
sex,Female,0.9591642924976258,0.908273381294964,0.9330254041570438
native-country,United-States,0.9504906986963527,0.927662616154396,0.9389379250470264
native-country,Cuba,1.0,1.0,1.0
native-country,Jamaica,1.0,0.9,0.9473684210526316
native-country,India,0.9743589743589743,0.95,0.9620253164556962
native-country,Mexico,0.96875,0.9393939393939394,0.9538461538461539
native-country,Puerto-Rico,0.9230769230769231,1.0,0.9600000000000001
relationship,Not-in-family,0.996309963099631,0.32806804374240583,0.4936014625228519
relationship,Husband,0.7969683050068902,0.611023067441451,0.6917173328017543
relationship,Wife,0.8555555555555555,0.5547550432276657,0.673076923076923
relationship,Own-child,1.0,0.28125,0.43902439024390244
relationship,Unmarried,0.9846153846153847,0.3004694835680751,0.4604316546762591
relationship,Other-relative,1.0,0.22857142857142856,0.3720930232558139
race,White,0.8166031342651419,0.5638251206316713,0.6670703226364502
race,Black,0.8673469387755102,0.4644808743169399,0.6049822064056939
race,Asian-Pac-Islander,0.75,0.6532258064516129,0.6982758620689654
race,Amer-Indian-Eskimo,0.8947368421052632,0.5,0.6415094339622641
race,Other,0.7692307692307693,0.47619047619047616,0.588235294117647
sex,Male,0.8068033144352377,0.5784865540963102,0.6738299034784193
sex,Female,0.8879310344827587,0.4631294964028777,0.6087470449172577
native-country,United-States,0.8198444397729662,0.557541100786276,0.6637168141592921
native-country,Cuba,0.8666666666666667,0.52,0.65
native-country,Jamaica,0.5,0.2,0.28571428571428575
native-country,India,0.6875,0.825,0.75
native-country,Mexico,0.9230769230769231,0.36363636363636365,0.5217391304347827
native-country,Puerto-Rico,0.7777777777777778,0.5833333333333334,0.6666666666666666
native-country,Honduras,1.0,1.0,1.0
native-country,England,0.9655172413793104,0.9333333333333333,0.9491525423728815
native-country,Canada,1.0,0.9166666666666666,0.9565217391304348
native-country,Germany,0.9333333333333333,0.9545454545454546,0.9438202247191012
native-country,Iran,0.9473684210526315,1.0,0.972972972972973
native-country,Philippines,0.9433962264150944,0.8333333333333334,0.8849557522123894
native-country,Poland,1.0,0.8181818181818182,0.9
native-country,Columbia,1.0,1.0,1.0
native-country,Cambodia,1.0,0.8571428571428571,0.923076923076923
native-country,Thailand,1.0,1.0,1.0
native-country,Ecuador,1.0,1.0,1.0
native-country,England,0.76,0.6333333333333333,0.6909090909090909
native-country,Canada,0.7307692307692307,0.5277777777777778,0.6129032258064515
native-country,Germany,0.8333333333333334,0.5681818181818182,0.6756756756756758
native-country,Iran,0.7142857142857143,0.8333333333333334,0.7692307692307692
native-country,Philippines,0.8297872340425532,0.65,0.7289719626168223
native-country,Poland,0.6666666666666666,0.5454545454545454,0.6
native-country,Columbia,0.5,1.0,0.6666666666666666
native-country,Cambodia,1.0,0.2857142857142857,0.4444444444444445
native-country,Thailand,1.0,0.3333333333333333,0.5
native-country,Ecuador,0.6666666666666666,0.5,0.5714285714285715
native-country,Laos,1.0,0.5,0.6666666666666666
native-country,Taiwan,1.0,1.0,1.0
native-country,Haiti,1.0,0.75,0.8571428571428571
native-country,Portugal,1.0,0.75,0.8571428571428571
native-country,Dominican-Republic,1.0,1.0,1.0
native-country,El-Salvador,1.0,0.8888888888888888,0.9411764705882353
native-country,France,1.0,0.9166666666666666,0.9565217391304348
native-country,Guatemala,1.0,1.0,1.0
native-country,Italy,1.0,0.875,0.9333333333333333
native-country,China,1.0,1.0,1.0
native-country,South,0.9285714285714286,0.9285714285714286,0.9285714285714286
native-country,Japan,1.0,1.0,1.0
native-country,Yugoslavia,1.0,1.0,1.0
native-country,Peru,1.0,1.0,1.0
native-country,Taiwan,0.8421052631578947,0.8421052631578947,0.8421052631578947
native-country,Haiti,1.0,0.5,0.6666666666666666
native-country,Portugal,1.0,0.25,0.4
native-country,Dominican-Republic,0.5,0.5,0.5
native-country,El-Salvador,1.0,0.7777777777777778,0.8750000000000001
native-country,France,0.8181818181818182,0.75,0.7826086956521738
native-country,Guatemala,1.0,0.3333333333333333,0.5
native-country,Italy,0.8571428571428571,0.5,0.631578947368421
native-country,China,0.7391304347826086,0.85,0.7906976744186046
native-country,South,0.5294117647058824,0.6428571428571429,0.5806451612903226
native-country,Japan,0.7727272727272727,0.7391304347826086,0.7555555555555555
native-country,Yugoslavia,0.8,0.6666666666666666,0.7272727272727272
native-country,Peru,1.0,0.5,0.6666666666666666
native-country,Outlying-US(Guam-USVI-etc),1.0,1.0,1.0
native-country,Scotland,1.0,1.0,1.0
native-country,Trinadad&Tobago,0.5,0.5,0.5
native-country,Greece,0.8888888888888888,1.0,0.9411764705882353
native-country,Nicaragua,0.6666666666666666,1.0,0.8
native-country,Vietnam,1.0,1.0,1.0
native-country,Hong,1.0,0.8333333333333334,0.9090909090909091
native-country,Ireland,1.0,1.0,1.0
native-country,Hungary,1.0,0.6666666666666666,0.8
native-country,Scotland,1.0,0.5,0.6666666666666666
native-country,Trinadad&Tobago,1.0,0.5,0.6666666666666666
native-country,Greece,0.75,0.75,0.75
native-country,Nicaragua,1.0,0.0,0.0
native-country,Vietnam,0.4,0.4,0.4000000000000001
native-country,Hong,0.8333333333333334,0.8333333333333334,0.8333333333333334
native-country,Ireland,1.0,0.6,0.7499999999999999
native-country,Hungary,1.0,0.3333333333333333,0.5
native-country,Holand-Netherlands,1.0,1.0,1.0
2 changes: 1 addition & 1 deletion starter/starter/ml/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def train_model(X_train, y_train):
Trained machine learning model.
"""
cv = StratifiedKFold(shuffle=True)
model = RandomForestClassifier()
model = RandomForestClassifier(max_depth=10)
model.fit(X_train, y_train)
scores = cross_val_score(model, X_train, y_train, scoring='f1',
cv=cv, n_jobs=-1)
Expand Down
32 changes: 16 additions & 16 deletions starter/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,40 @@ def test_get_prediction_negative():
"workclass": "State-gov",
"fnlgt": 77516,
"education": "Bachelors",
"education_num": 13,
"marital_status": "Married-civ-spouse",
"education-num": 13,
"marital-status": "Married-civ-spouse",
"occupation": "Adm-clerical",
"relationship": "Not-in-family",
"race": "White",
"sex": "Male",
"capital_gain": 2174,
"capital_loss": 0,
"hours_per_week": 40,
"native_country": "United-States"
"capital-gain": 2174,
"capital-loss": 0,
"hours-per-week": 40,
"native-country": "United-States"
}
response = client.post('/predict', json=input_dict)
assert response.status_code == 200, "Status code is not 200"
assert response.json() == {"Predicted salary": "<=50K"}, \
assert response.json() == {"Predicted salary": "0"}, \
"Wrong json output"


def test_get_prediction_positive():
input_dict = {
"age": 31,
"age": 41,
"workclass": "Private",
"fnlgt": 45781,
"education": "Masters",
"education_num": 14,
"marital_status": "Married-civ-spouse",
"education-num": 14,
"marital-status": "Married-civ-spouse",
"occupation": "Prof-specialty",
"relationship": "Not-in-family",
"race": "White",
"sex": "Female",
"capital_gain": 1020,
"capital_loss": 0,
"hours_per_week": 50,
"native_country": "United-States"
"sex": "Male",
"capital-gain": 2020,
"capital-loss": 0,
"hours-per-week": 50,
"native-country": "United-States"
}
response = client.post("/predict", json=input_dict)
assert response.status_code == 200, "Status code is not 200"
assert response.json() == {"Predicted salary": ">50K"}, "Wrong json output"
assert response.json() == {"Predicted salary": "1"}, "Wrong json output"

0 comments on commit ca778cf

Please sign in to comment.