TensorFlow

TensorFlow Deep Learning (II) -

2023-01-17  本文已影响0人  ElliotG

1. What is regression

Regression is normally the first algorithm that people in machine learning work with. It allows us to make predictions from data by learning about the relationship between a given set of dependent and independent variables.

 

2. Linear regression

Linear regression is one of the most widely known modeling techniques.
Linear regression assumes a linear relationship between the input variable (X) and the output variable (Y).
The basic idea of linear regression is building a model, using training data that can predict the output given the input.

A simple linear regression sample

image.png image.png
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Generate a random data
np.random.seed(0)
area = 2.5 * np.random.randn(100) + 25
price = 25 * area + 5 + np.random.randint(20,50, size = len(area))
data = np.array([area, price])
data = pd.DataFrame(data = data.T, columns=['area','price'])
plt.scatter(data['area'], data['price'])
plt.show()

Output


image.png
image.png image.png
W = sum(price*(area-np.mean(area))) / sum((area-np.mean(area))**2)
b = np.mean(price) - W*np.mean(area)
print("The regression coefficients are", W,b)

Output:
The regression coefficients are 24.815544052284988 43.4989785533412


# Predicting the new prices using the obtained weight and bias
y_pred = W * area + b

# Plot the predicted prices along with the actual price
plt.plot(area, y_pred, color='red',label="Predicted Price")
plt.scatter(data['area'], data['price'], label="Training Data")
plt.xlabel("Area")
plt.ylabel("Price")
plt.legend()

Output


image.png

 

3. Multivariate linear regression

There can be cases where the independent variables affect more than one dependent variable.

image.png
上一篇 下一篇

猜你喜欢

热点阅读