Régression linéaire

Introduction à TensorFlow en Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Qu'est-ce qu'une régression linéaire ?

Ce graphique en nuage de points trace le logarithme naturel de la surface (pieds carrés) en fonction du logarithme naturel du prix de la maison (dollars).

Introduction à TensorFlow en Python

Qu'est-ce qu'une régression linéaire ?

Ce graphique montre une droite de régression ajustée au nuage de points du logarithme naturel de la surface (pieds carrés) en fonction du logarithme naturel du prix (dollars).

Introduction à TensorFlow en Python

Le modèle de régression linéaire

  • Un modèle de régression linéaire suppose une relation linéaire :
    • $price = intercept + size*slope + error$
  • Ceci est un exemple de régression univariée.
    • Il n'y a qu'une seule caractéristique, size.
  • Les régressions multiples utilisent plusieurs caractéristiques.
    • P. ex. size et location
Introduction à TensorFlow en Python

Régression linéaire avec TensorFlow

# Define the targets and features
price = np.array(housing['price'], np.float32)
size = np.array(housing['sqft_living'], np.float32)

# Define the intercept and slope
intercept = tf.Variable(0.1, np.float32)
slope = tf.Variable(0.1, np.float32)
# Define a linear regression model
def linear_regression(intercept, slope, features = size):
    return intercept + features*slope
# Compute the predicted values and loss
def loss_function(intercept, slope, targets = price, features = size):
    predictions = linear_regression(intercept, slope)
    return tf.keras.losses.mse(targets, predictions)
Introduction à TensorFlow en Python

Régression linéaire avec TensorFlow

# Define an optimization operation
opt = tf.keras.optimizers.Adam()
# Minimize the loss function and print the loss
for j in range(1000):
    opt.minimize(lambda: loss_function(intercept, slope),\
    var_list=[intercept, slope])
    print(loss_function(intercept, slope))
tf.Tensor(10.909373, shape=(), dtype=float32)
...
tf.Tensor(0.15479447, shape=(), dtype=float32)
# Print the trained parameters
print(intercept.numpy(), slope.numpy())
Introduction à TensorFlow en Python

Passons à la pratique !

Introduction à TensorFlow en Python

Preparing Video For Download...