Neural Network Model--Mathematical Modeling

Table of contents

1. Introduction to Neural Network Model

2. The use of neural networks in mathematical modeling

3. Application cases of neural network in mathematical modeling

3.1 Traffic Flow Forecast

3.2 Stock Price Prediction

3.3 Image recognition

3.4 Natural Language Processing

3.5 Intelligent Control


 

1. Introduction to Neural Network Model

A neural network is an artificial intelligence algorithm inspired by biological neural networks. Similar to biological neural networks, neural networks are also composed of many interconnected simple units called neurons.

Neural networks are usually divided into three main parts: input layer, hidden layer, and output layer. The input layer accepts the input data, the output layer outputs the result, and the hidden layer processes information between the input and output layers.

Each neuron receives inputs from other neurons, sums these inputs weightedly, and passes through an activation function to produce an output. The activation function can be a linear function or a nonlinear function, such as sigmoid, ReLU, etc.

Neural networks are trained to learn the relationship between inputs and outputs. During training, the network goes through the backpropagation algorithm to adjust the weights and biases so that the network produces more accurate output. The backpropagation algorithm updates the weights and biases by computing the error between the output and the actual result and backpropagating to each neuron in the network.

Neural networks are widely used in many fields, including pattern recognition, speech recognition, natural language processing, image processing, game AI, etc. For example, in image processing, a neural network can identify and classify different objects in an image; in natural language processing, a neural network can perform sentiment analysis on text, machine translation, etc.; in game AI, a neural network can be trained to learn Player behavior patterns and generate optimal strategies.

2. The use of neural networks in mathematical modeling

  1. Predictive Models: Neural networks can be used in predictive models such as stock price forecasts, weather forecasts, traffic flow forecasts, etc. By training a neural network to learn the relationship between historical data and forecasted targets, an accurate forecasting model can be obtained.

  2. Classification models: Neural networks can be used for classification problems such as image classification, text classification, audio classification, etc. An effective classification model can be obtained by training a neural network to learn the differences between categories.

  3. Clustering models: Neural networks can be used for clustering problems such as grouping similar data points. By training a neural network to learn the similarity between data points, an effective clustering model can be obtained.

  4. Optimization problems: Neural networks can be used for optimization problems such as minimizing a cost function, maximizing profit, etc. by tuning parameters. By training a neural network to learn the relationship between parameters, an efficient optimization model can be obtained.

In general, neural networks are widely used in mathematical modeling and can be used to solve a variety of problems, such as classification, prediction, clustering, optimization, etc. Neural networks can process large amounts of data and automatically learn complex patterns and relationships from the data, so they have advantages when dealing with large amounts of data and highly nonlinear problems.

3. Application cases of neural network in mathematical modeling

3.1 Traffic Flow Forecast

Use neural networks to predict traffic flow, such as traffic jams in cities or traffic flow on highways. Neural networks can learn from historical traffic data to predict future traffic conditions.

Since traffic flow forecasting involves issues such as data acquisition and preprocessing, only the code implementation of the neural network model is provided here.

The following is a code implementation of a simple traffic flow forecasting neural network model:

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM

# 加载数据集
data = pd.read_csv('traffic.csv')

# 数据预处理
def prepare_data(data, lags=1):
    # 将数据集转换为numpy数组
    values = data.values
    # 将数据集中的所有数据转换为浮点数类型
    values = values.astype('float32')
    # 标准化数据
    mean = np.mean(values, axis=0)
    std = np.std(values, axis=0)
    values = (values - mean) / std
    # 将数据集转换为监督学习问题
    X, y = [], []
    for i in range(lags, len(values)):
        X.append(values[i-lags:i, :])
        y.append(values[i, -1])
    X, y = np.array(X), np.array(y)
    return X, y, mean, std

# 定义模型
def build_model(lags):
    model = Sequential()
    model.add(LSTM(50, input_shape=(lags, X_train.shape[2])))
    model.add(Dense(1))
    model.compile(loss='mse', optimizer='adam')
    return model

# 准备数据
lags = 3
X, y, mean, std = prepare_data(data, lags)
# 划分训练集和测试集
train_size = int(len(X) * 0.7)
X_train, X_test, y_train, y_test = X[:train_size], X[train_size:], y[:train_size], y[train_size:]
# 构建模型
model = build_model(lags)
# 训练模型
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=2)
# 预测
y_pred = model.predict(X_test)
# 反标准化
y_pred = (y_pred * std[-1]) + mean[-1]
y_test = (y_test * std[-1]) + mean[-1]
# 计算误差
mse = np.mean(np.square(y_pred - y_test))
print('MSE:', mse)

Among them, traffic.csvis a CSV file containing traffic flow data, each row represents data at a point in time, including multiple features, such as date, time, weather, holidays, etc., and traffic flow. In this example, we use only one feature, traffic flow.

The main flow of the code is as follows:

  1. Load the dataset and preprocess it, convert the dataset to numpy array and normalize it.
  2. Transform the data set into a supervised learning problem, that is, the traffic flow at several past time points is used as a feature, and the traffic flow at the current time point is used as a label.
  3. Divide training set and test set.
  4. Build an LSTM neural network model.
  5. Train the model.
  6. Predict on the test set

3.2 Stock Price Prediction

Using neural networks to predict stock price movements can help investors make better investment decisions. Neural networks can learn from historical stock prices and other market data to predict future stock prices.

The following is a simple stock price prediction case code, using a neural network model:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM

# 导入数据
data = pd.read_csv('stock_prices.csv')
# 只保留收盘价
data = data[['Close']]
# 将数据缩放到0-1范围内
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(np.array(data).reshape(-1, 1))

# 准备数据
train_size = int(len(data) * 0.7)
test_size = len(data) - train_size
train_data, test_data = data[0:train_size,:], data[train_size:len(data),:]

def create_dataset(dataset, time_step=1):
    X, Y = [], []
    for i in range(len(dataset) - time_step - 1):
        a = dataset[i:(i + time_step), 0]
        X.append(a)
        Y.append(dataset[i + time_step, 0])
    return np.array(X), np.array(Y)

time_step = 100
X_train, Y_train = create_dataset(train_data, time_step)
X_test, Y_test = create_dataset(test_data, time_step)

# 建立神经网络模型
model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(100, 1)))
model.add(Dropout(0.2))
model.add(LSTM(50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(50))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

# 训练模型
model.fit(X_train, Y_train, validation_data=(X_test, Y_test), epochs=100, batch_size=64, verbose=1)

# 测试模型
train_predict = model.predict(X_train)
test_predict = model.predict(X_test)

# 将预测数据缩放回原始范围
train_predict = scaler.inverse_transform(train_predict)
Y_train = scaler.inverse_transform([Y_train])
test_predict = scaler.inverse_transform(test_predict)
Y_test = scaler.inverse_transform([Y_test])

# 画出预测结果
plt.plot(Y_test[0], label='True')
plt.plot(test_predict[:,0], label='Predicted')
plt.legend()
plt.show()

Among them, the input data is a historical closing price sequence of a stock. After preprocessing and training, the stock price prediction is realized by predicting the closing price at the future time point. The LSTM neural network model is used in this code, and the mean square error is used as the loss function, and the Adam optimizer is used for training.

3.3 Image recognition

Use neural networks to identify information such as objects, scenes, and faces in images. Neural networks can learn from a large amount of image data, and then recognize information in new images.

Image recognition is an important field of neural network applications. The following is a simple image recognition case code for recognizing pictures of handwritten digits:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 导入手写数字数据集
mnist = keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# 归一化数据集
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

# 构建模型
model = keras.Sequential(
    [
        keras.Input(shape=(28, 28)),
        layers.Reshape(target_shape=(28 * 28)),
        layers.Dense(256, activation="relu"),
        layers.Dense(128, activation="relu"),
        layers.Dense(10),
    ]
)

# 编译模型
model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.RMSprop(),
    metrics=["accuracy"],
)

# 训练模型
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.2)

# 评估模型
model.evaluate(x_test, y_test, batch_size=64)

# 预测手写数字图片
predictions = model.predict(x_test[:5])
print(np.argmax(predictions, axis=1))

This code uses the TensorFlow framework, uses the neural network model to train the handwritten digit data set, and realizes the recognition of handwritten digit pictures. Among them, the model uses two fully connected layers, and each layer uses the ReLU activation function. After training, the model is evaluated using the test dataset and the model is used to make predictions on the first five test datasets.

3.4 Natural Language Processing

Use neural networks to process natural language, such as machine translation, sentiment analysis, text classification, and more. Neural networks can learn from large amounts of text data to process new natural language data.

The following is a simple natural language processing case code for text classification based on sentiment analysis:

import pandas as pd
import numpy as np
import re
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
 
# 读取数据
data = pd.read_csv("sentiment.csv", encoding='latin-1')
 
# 数据清洗
def preprocess_text(text):
    text = re.sub(r'[^\w\s]', '', text)  # 删除标点符号
    text = text.lower()  # 小写化
    text = [word for word in text.split() if word not in stopwords.words('english')]  # 删除停用词
    text = " ".join(text)  # 连接成字符串
    return text
 
data['text'] = data['text'].apply(preprocess_text)
 
# 特征工程
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(data['text'])
y = data['sentiment']
 
# 数据划分
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
# 建立模型
model = MultinomialNB()
model.fit(X_train, y_train)
 
# 预测并评估模型
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)
print("Accuracy:", accuracy)
print("Confusion Matrix:\n", conf_matrix)

The code implements sentiment analysis on a dataset called "sentiment.csv", using a Naive Bayesian model and a count vectorizer. The data set contains two columns, "text" and "sentiment", the former contains some texts, and the latter indicates the sentiment category to which each text belongs. The code first undergoes data cleaning, including removing punctuation, lowercase, removing stop words, etc. Next, use a count vectorizer to convert the text into vector form as input to the Naive Bayesian model. Finally, the data set is randomly divided into training set and test set, and the Naive Bayesian model is trained using the training set, and predicted and evaluated on the test set. Evaluation metrics include accuracy and confusion matrix.

3.5 Intelligent Control

Use neural networks to control robots, cars, industrial systems, and more. Neural networks can learn the relationship between the environment and tasks to achieve autonomous control and decision-making.

The following is a case code of intelligent control in mathematical modeling, which uses fuzzy control algorithm to control the output power of wind turbines. Fuzzy control is a control method based on fuzzy logic, which can deal with nonlinear and complex system control problems. In this case, the fuzzy control algorithm is used to optimize the speed and blade angle of the wind turbine to achieve the maximum output power.

import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl

# 输入变量
wind_speed = ctrl.Antecedent(np.arange(0, 25, 1), 'wind_speed')
blade_angle = ctrl.Antecedent(np.arange(0, 45, 1), 'blade_angle')

# 输出变量
power_output = ctrl.Consequent(np.arange(0, 5000, 1), 'power_output')

# 自定义隶属度函数
wind_speed['low'] = fuzz.trimf(wind_speed.universe, [0, 0, 7])
wind_speed['medium'] = fuzz.trimf(wind_speed.universe, [0, 7, 15])
wind_speed['high'] = fuzz.trimf(wind_speed.universe, [7, 25, 25])

blade_angle['low'] = fuzz.trimf(blade_angle.universe, [0, 0, 20])
blade_angle['medium'] = fuzz.trimf(blade_angle.universe, [0, 20, 40])
blade_angle['high'] = fuzz.trimf(blade_angle.universe, [20, 45, 45])

power_output['low'] = fuzz.trimf(power_output.universe, [0, 0, 2500])
power_output['medium'] = fuzz.trimf(power_output.universe, [0, 2500, 5000])
power_output['high'] = fuzz.trimf(power_output.universe, [2500, 5000, 5000])

# 规则定义
rule1 = ctrl.Rule(wind_speed['low'] & blade_angle['low'], power_output['low'])
rule2 = ctrl.Rule(wind_speed['low'] & blade_angle['medium'], power_output['low'])
rule3 = ctrl.Rule(wind_speed['low'] & blade_angle['high'], power_output['low'])
rule4 = ctrl.Rule(wind_speed['medium'] & blade_angle['low'], power_output['medium'])
rule5 = ctrl.Rule(wind_speed['medium'] & blade_angle['medium'], power_output['medium'])
rule6 = ctrl.Rule(wind_speed['medium'] & blade_angle['high'], power_output['high'])
rule7 = ctrl.Rule(wind_speed['high'] & blade_angle['low'], power_output['high'])
rule8 = ctrl.Rule(wind_speed['high'] & blade_angle['medium'], power_output['high'])
rule9 = ctrl.Rule(wind_speed['high'] & blade_angle['high'], power_output['high'])

# 控制系统定义
power_output_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9])
power_output_simulation = ctrl.ControlSystemSimulation(power_output_ctrl)

# 进行模糊控制
power_output_simulation.input['wind_speed'] = 10
power_output_simulation.input['blade_angle'] = 30
power_output_simulation.compute()

#

Finally share:

30+ algorithm models and case code knowledge sharing (pure dry goods):

Link: https://pan.baidu.com/s/1Pg_PgPJ8-EJ0RMjZ6_dF3Q?pwd=fid3 
Extraction code: fid3 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_51533426/article/details/130431122