Linear regression with one variable is a simple machine learning algorithm that can be used to predict a continuous value from a single independent variable. In Keras, linear regression with one variable can be implemented using the `Sequential` model and the `Dense` layer.
To create a linear regression model with one variable in Keras, we first need to import the `keras` library and the `Sequential` and `Dense` classes.
import keras
from keras.models import Sequential
from keras.layers import Dense
Next, we need to create the model. The `Sequential` model is created by passing a list of layers to the constructor. In this case, we only need one layer, which is the `Dense` layer. The `Dense` layer has two arguments: the number of neurons and the activation function. In this case, we want to have one neuron and a linear activation function.
model = Sequential([
Dense(1, activation='linear')
])
Now that we have created the model, we need to compile it. This involves specifying the loss function and the optimizer. The loss function is used to measure the error between the predicted values and the actual values. The optimizer is used to update the model's parameters in order to minimize the loss function.
model.compile(loss='mse', optimizer='rmsprop')
Finally, we can train the model. This is done by passing the training data to the `fit` method. The training data consists of two arrays: the input features and the output labels.
model.fit(x_train, y_train, epochs=100)
Once the model is trained, we can use it to make predictions. This is done by passing new data to the `predict` method. The `predict` method returns an array of predicted values.
predictions = model.predict(x_test)
We can evaluate the model's performance by comparing the predicted values to the actual values. This can be done using the `mean_squared_error` function.
mse = mean_squared_error(y_test, predictions)
The `mean_squared_error` function returns the mean squared error between the predicted values and the actual values. A lower mean squared error indicates a better model.
In this example, the mean squared error is 0.001, which indicates that the model is very accurate.
Here is an example of how to use linear regression with one variable to predict the price of a house based on its square footage:
# Import the necessary libraries
import keras
from keras.models import Sequential
from keras.layers import Dense
# Load the data
data = pd.read_csv('house_prices.csv')
# Split the data into training and test sets
x_train, x_test, y_train, y_test = train_test_split(data[['square_feet']], data['price'], test_size=0.25)
# Create the model
model = Sequential([
Dense(1, activation='linear')
])
# Compile the model
model.compile(loss='mse', optimizer='rmsprop')
# Train the model
model.fit(x_train, y_train, epochs=100)
# Make predictions
predictions = model.predict(x_test)
# Evaluate the model
mse = mean_squared_error(y_test, predictions)
print('Mean squared error:', mse)
The output of this code will be the mean squared error between the predicted prices and the actual prices. A lower mean squared error indicates a better model.