# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="XtzD0O2JZ2vI"
# # ML 101
#
# ## RDP and line simplification
#
# RDP is a line simplifaction algorithm that can be used to reduce the number of points.

# + id="GkcFqqXpYbF-"
# !pip install git+git://github.com/mariolpantunes/knee@main#egg=knee

# + id="-3lXwxOCaCcq"
# %matplotlib inline
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [16, 9]
import numpy as np
import knee.linear_fit as lf
import knee.rdp as rdp

# generate some data
x = np.arange(0, 100, 0.1)
y = np.sin(x)

print(len(x))

plt.plot(x,y)
plt.show()

# + id="up00OKbCas7Q"
# generate the points
points = np.array([x,y]).T
reduced, removed = rdp.rdp(points, 0.01, cost=lf.Linear_Metrics.rpd)
reduced_points = points[reduced]

print(f'Lenght = {len(reduced_points)}')

plt.plot(x,y)
x = reduced_points[:,0]
y = reduced_points[:,1]
plt.plot(x,y, 'ro')
plt.show()

# + [markdown] id="pAUoQ0ATjaw0"
# ## Knee detection
#
# Elbow/knee detection is a method to select te ideal cut-off point in a performance curve

# + id="sSKtjKj3jnkq"
# generate some data
x = np.arange(0.1, 10, 0.1)
y = 1/(x**2)

plt.plot(x,y)
plt.show()

# + id="bpW1ujGyj0LQ"
import knee.lmethod as lmethod

# generate the points
points = np.array([x,y]).T

idx = lmethod.knee(points)

plt.plot(x,y)
plt.plot(x[idx], y[idx], 'ro')
plt.show()

# + id="NPwzleXkkfDF"
import knee.dfdt as dfdt

# generate the points
points = np.array([x,y]).T

idx = dfdt.knee(points)

print(idx)

plt.plot(x,y)
plt.plot(x[idx], y[idx], 'ro')
plt.show()

StarcoderdataJupyterScriptsDedupFiltered