Predictive model Flashcards
Predictive model
- Train and test
x = [scegli 15 valori]
y = [scegli 15 valori]
n_valori = 10
x_train = x[:n_valori]
y_train = y[:n_valori]
x_test = x[n_valori:]
y_test = y[n_valori:]
Predictive model
- equazione linea
def linea(x,m,c):
return (x*m)+c
plt.plot(linea(x,0.2,0))
Predictive model
- Model
def show_plot(y_train, model):
if len(y_train!=model):
print(“they need to be the same size”)
size = len(y_train)
x = np.arange(size)
plt.scatter(x,y_train)
plt.plot(x,model)
plt.show
model=linea(x_train,1,0)
show_plot(y_train, model)
Predictive model
- Model evaluation
def square_error(y,model):
error = y - model
sq_e = error **2
return sum(sq_e)
print(‘square error: ‘,square_error(y_train,linea(x,1,0)))
Predictive model
- Fit the model + data
steps = 200
epsilon = 0.01
def fit(y_train,m,c,steps,epsilon):
model = linea(-x,1,0)
sq_e = square_error(y_train, model)
for i in range(steps):
m1 = m + np.random.choice([-1,1],size=1)epsilon
c1 = m + np.random.choice([-1,1],size=1)epsilon
model1 = linea(-x,1,0)
sq:e1 = square_error(y_train, model1)
if sq_e1<sq_e:
m = m1
c = c1
sq_e = sq_e1
return m,c