Home > Backend Development > Python Tutorial > How to use neural networks for regression analysis in Python?

How to use neural networks for regression analysis in Python?

王林
Release: 2023-06-05 12:21:18
Original
2243 people have browsed it

With the development of artificial intelligence, neural networks have shown excellent performance in many fields, including regression analysis. The Python language is often used for machine learning and data analysis tasks, and provides many open source machine learning libraries, such as Tensorflow and Keras. This article will introduce how to use neural networks for regression analysis in Python.

1. What is regression analysis?

In statistics, regression analysis is a method of analyzing causal relationships by using a mathematical model of continuous variables to describe the relationship between independent variables and dependent variables. In regression analysis, a linear equation is usually used to describe this relationship, for example:

y = a bx

where y is the dependent variable, x is the independent variable, and a and b are circles The constants in parentheses represent the intercept and slope of the linear relationship. Regression analysis can predict the value of a dependent variable by fitting a linear equation. For data with complex or non-linear relationships, more complex models can be used.

2. Application of neural network in regression analysis

Neural network is a complex mathematical model composed of multiple nodes. It learns the patterns and rules of input data to analyze new data. Make predictions. The application of neural networks in regression analysis is by inputting dependent variables and independent variables into the network and training the neural network to find the relationship between them.

Different from traditional regression analysis, neural networks do not need to define a linear or nonlinear equation in advance when analyzing data. Neural networks can automatically find patterns and patterns, and learn and analyze based on the details of the input data set. This enables neural networks to exhibit excellent performance on large-scale data sets, data with complex patterns and non-linearity.

3. Use Python for regression analysis

Python's Scikit-learn and Keras are two very popular Python libraries that provide many tools for neural networks and regression analysis. Here, we will use the Sequential model in Keras to build a simple neural network and use Scikit-learn's train_test_split method to divide the known data set to evaluate our model.

Step 1: Data preprocessing

Before starting to use neural networks for regression analysis, you need to prepare the data. In this article, we will use the fuel efficiency dataset on the online learning platform Kaggle. This dataset contains vehicle economy fuel data from the U.S. National Highway Traffic Safety Administration. Factors included in the data, such as yardage, cylinder count, displacement, horsepower and acceleration, all affect fuel efficiency.

We will use the Pandas library to read and process the dataset:

import pandas as pd

#导入数据
df = pd.read_csv('auto-mpg.csv')
Copy after login

Step 2: Data Preprocessing

We need to convert the dataset into something that the neural network can read form. We will use the get_dummies() method of the Pandas library to decompose the categorical variables into binary fields that can be used:

dataset = df.copy()
dataset = pd.get_dummies(dataset, columns=['origin'])
Copy after login

Next, we need to partition the dataset into a training set and a test set to evaluate our model. Here, we choose to use Scikit-learn's train_test_split method:

from sklearn.model_selection import train_test_split

train_dataset, test_dataset = train_test_split(dataset, test_size=0.2, random_state=42)

#获取训练集的目标变量
train_labels = train_dataset.pop('mpg')

#获取测试集的目标变量
test_labels = test_dataset.pop('mpg')
Copy after login

Step 3: Build the neural network model

We will use Keras's Sequential model to build the neural network model, which contains Two fully connected hidden layers and use a ReLU layer with activation function. Finally, we use an output layer with a single node to predict fuel efficiency.

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=[len(train_dataset.keys())]),
    layers.Dense(64, activation='relu'),
    layers.Dense(1)
])
Copy after login

Step 4: Compile and train the model

Before training the model, we need to compile the model. Here we will specify the loss function and optimizer as well as the evaluation metrics.

optimizer = keras.optimizers.RMSprop(0.001)

model.compile(loss='mse',
            optimizer=optimizer,
            metrics=['mae', 'mse'])
Copy after login

Next, we will use the fit() method to train the model and save it to the history object for subsequent analysis.

history = model.fit(
  train_dataset, train_labels,
  epochs=1000, validation_split=0.2, verbose=0,
  callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)])
Copy after login

Step 5: Evaluate the model

Finally, we will use the test dataset to evaluate our model and save the results into the y_pred variable.

test_predictions = model.predict(test_dataset).flatten()

print('测试集的平均误差: ', round(abs(test_predictions - test_labels).mean(), 2))
Copy after login

In this example, the model we used produced a prediction result with an average error of about 2.54, and we can see the loss on the test set and validation set in the history object.

4. Summary

In this article, we introduced how to use neural networks in Python for regression analysis. We started with data preprocessing, then leveraged Keras and Scikit-learn libraries to build and train our model, and evaluated the model's performance. Neural networks have powerful performance and show extremely high results in processing large-scale data sets and complex nonlinear problems. For your next regression problem, why not try using a neural network to solve it?

The above is the detailed content of How to use neural networks for regression analysis in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template