<jupyter_start><jupyter_text>Split data 3-ways. We will train the model using all players drafted before 1990, validate the model on players drafted between 1990-1999, and test on players drafted in 2000 or later.<jupyter_code>train= df[df['draft_yr'] <1990]
val= df[(df['draft_yr'] >=1990) & (df['draft_yr'] <2000)]
test= df[df['draft_yr'] >=2000]
train.head()
val.head()
test.head()<jupyter_output><empty_output><jupyter_text>Drop target from features<jupyter_code>X_train = train.drop(columns=['yrs', 'player'])
y_train = train['yrs']
X_val = val.drop(columns=['yrs', 'player'])
y_val = val['yrs']
X_test = test.drop(columns=['yrs', 'player'])
y_test = test['yrs']
X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Get baseline linear regression model<jupyter_code>model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_val)
score = mean_absolute_error(y_val, y_pred)
score
y_pred = model.predict(X_test)
score_test = mean_absolute_error(y_test, y_pred)
score_test<jupyter_output><empty_output><jupyter_text>Using random forest regressor<jupyter_code>model1 = RandomForestRegressor(n_estimators=1000, random_state=42)
model1.fit(X_train, y_train)
y_pred1 = model1.predict(X_val)
score1 = mean_absolute_error(y_val, y_pred1)
print('Validation score:', score1)
y_pred2 = model1.predict(X_test)
score2 = mean_absolute_error(y_test, y_pred2)
print('Test score:', score2)<jupyter_output>Validation score: 1.5035857740585772
Test score: 1.8228343685300208<jupyter_text>Pickle The Model<jupyter_code>joblib.dump(model1, 'nba_model')<jupyter_output><empty_output><jupyter_text>Testing the API<jupyter_code>url = 'https://nba-project.herokuapp.com/api'
data = json.dumps({'all_nba': 2, 'all_star': 9, 'draft_yr': 1976, 'pk': 8, 'fg_percentage': 0.537,
'tp_percentage': 0, 'ft_percentage': 0.721, 'minutes_per_game': 28.4, 
'points_per_game': 14.5, 'trb_per_game': 9.1, 'assists_per_game': 1.4,
'ws_per_game': 0.154, 'bpm': 1.6, 'vorp': 41.5, 'attend_college': 1})
print(data)
send = requests.post(url, data)
send.json()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15205223880597016, 'fraction_numerical': 0.05690298507462686, 'mean_word_length': 8.75, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '402804', 'license': 'MIT', 'n_tokens_mistral': 940, 'n_tokens_neox': 845, 'n_words': 156, 'path': 'machine_learning/nba_project.ipynb', 'repo_name': 'JLDaniel77/DS', 'size': '2144'}