<jupyter_start><jupyter_text>Setup<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns

import matplotlib.pyplot as plt

%matplotlib inline 
from sklearn import (datasets,
                     linear_model,
                     metrics,
                     model_selection as skms,
                     neighbors)
import warnings
warnings.filterwarnings('ignore')
np.random.seed(42)
pd.options.display.float_format = '{:20,.4f}'.format<jupyter_output><empty_output><jupyter_text>Optimization<jupyter_code>tgt = np.array([3,5,8,10,12,15])
# random guessing
num_guesses = 10
results = []
for g in range(num_guesses):
    guess = np.random.uniform(low=tgt.min(),
                              high=tgt.max())
    total_sse = np.sum((tgt-guess)**2)
    results.append((total_sse, guess))
best_guess = sorted(results)[0][1]
best_guess
# random step, if it's good
num_steps = 100
step_size = 0.05

best_guess = np.random.uniform(low=tgt.min(),
                               high=tgt.max())
best_sse = np.sum((tgt-best_guess)**2)

for s in range(num_steps):
    new_guess = best_guess + (np.random.choice([+1, -1]) * step_size)
    new_sse   = np.sum((new_guess-tgt)**2)
    if new_sse < best_sse:
        best_guess = new_guess
        best_sse   = new_sse
print(best_guess)
# smart steps
num_steps = 100
step_size = 0.02

best_guess = np.random.uniform(low=tgt.min(),
                               high=tgt.max())
best_sse = np.sum((tgt-best_guess)**2)
print('start:', best_guess)

for s in range(num_steps):
    guesses = best_guess + (np.array([-1, +1]) * step_size)
    sses    = np.sum((tgt[:,np.newaxis] - guesses)**2, axis=0)
    guess_idx = np.argmin(sses)
    
    if sses[guess_idx] < best_sse:
        best_guess = guesses[guess_idx]
        best_sse   = sses[guess_idx]
    else:
        break

print('end:', best_guess)
# 7.5, 5.1, 8.3
np.mean(tgt)<jupyter_output><empty_output><jupyter_text>Learning Performance of Regressors<jupyter_code># stand alone code
from sklearn import (datasets, 
                     linear_model, 
                     model_selection as skms,
                     metrics,
                     neighbors)

# dataset
diabetes = datasets.load_diabetes()

# tts
tts =  skms.train_test_split(diabetes.data,
                             diabetes.target, 
                             test_size=.25)
(diabetes_train, diabetes_test, 
 diabetes_train_tgt, diabetes_test_tgt) = tts

# define some models
models = {'3-NN': neighbors.KNeighborsRegressor(n_neighbors=3),
          '5-NN': neighbors.KNeighborsRegressor(n_neighbors=5),
          '10-NN': neighbors.KNeighborsRegressor(n_neighbors=10),
          '20-NN': neighbors.KNeighborsRegressor(n_neighbors=20),
          'linreg' : linear_model.LinearRegression()}

for name, model in models.items():
    fit   = model.fit(diabetes_train, diabetes_train_tgt)
    preds = fit.predict(diabetes_test)
    score = np.sqrt(metrics.mean_squared_error(diabetes_test_tgt, 
                                               preds))
    print("{:>6s} : {:0.2f}".format(name,score))<jupyter_output>3-NN : 62.83
  5-NN : 60.42
 10-NN : 57.46
 20-NN : 56.16
linreg : 54.63<jupyter_text>Resource Evaluation<jupyter_code>!cat scripts/perf_02.py
!python scripts/perf_02.py mem lr
!python scripts/perf_02.py time lr
!python scripts/perf_02.py mem knn
!python scripts/perf_02.py time knn<jupyter_output>knn_go: ~0.3438 MiB
knn_go: ~0.1123 sec
{'fraction_non_alphanumeric': 0.12226437117011964, 'fraction_numerical': 0.03180624452874234, 'mean_word_length': 2.787845303867403, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '447461', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1250, 'n_words': 220, 'path': 'part_i/06_beginning_regression_part_2.ipynb', 'repo_name': 'mfenner1/mlwpy_video_code', 'size': '3427'}