9951 explained code solutions for 126 technologies


python-kerasHow can I use Python and Keras to build a regression model?



To build a regression model with Python and Keras, you will need to install the Keras library and import it into your Python script. The following example code will demonstrate how to build a simple regression model with Keras:

import keras
from keras.models import Sequential
from keras.layers import Dense

# Defining the model
model = Sequential()
model.add(Dense(1, input_dim=1, activation='linear'))

# Compiling the model
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mse'])

# Fitting the model
model.fit(x_train, y_train, epochs=50, batch_size=32, verbose=1)

The code above consists of the following parts:

  1. Importing the necessary libraries: import keras and from keras.models import Sequential and from keras.layers import Dense
  2. Defining the model: model = Sequential() and model.add(Dense(1, input_dim=1, activation='linear'))
  3. Compiling the model: model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mse'])
  4. Fitting the model: model.fit(x_train, y_train, epochs=50, batch_size=32, verbose=1)

After running this code, the model will be trained and ready to be used for predictions.

Helpful links

Edit this code on GitHub