python机器学习——简单神经网络算法回归分析

利用python实现简单的神经网络算法回归分析
2023年亚太杯数学建模C题可以使用这个代码进行分析

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense

# 读取 Excel 文件数据

data = pd.read_excel('这里输入你的excel路径')

# 提取输入特征和输出标签

X = data[['这里输入你的特征标签']].values
y = data[['这里输入你的输出标签']].values

# 数据标准化

scaler_x = StandardScaler()
scaler_y = StandardScaler()
X = scaler_x.fit_transform(X)
y = scaler_y.fit_transform(y)

# 划分训练集和测试集

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 构建神经网络模型

input_layer = Input(shape=(X.shape[1],))
dense1 = Dense(64, activation='relu')(input_layer)
dense2 = Dense(32, activation='relu')(dense1)
output_layer = Dense(3)(dense2)  # 输出层有3个节点,对应 a1, h1, J1 三个输出

model = Model(inputs=input_layer, outputs=output_layer)

# 编译模型

model.compile(optimizer='adam', loss='mean_squared_error')

# 训练模型

model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test))

# 打印模型的输入对输出分别的影响程度

weights = model.get_weights()
input_weights = weights[0]
output_weights = weights[-1]

input_importance = np.abs(input_weights).sum(axis=1) / np.abs(input_weights).sum()
output_importance = np.abs(output_weights).sum(axis=0) / np.abs(output_weights).sum()

# 计算特征与输出之间的相关系数

correlation_matrix = np.corrcoef(X.T, y.T)
feature_output_correlation = correlation_matrix[:X.shape[1], X.shape[1]:]

print('Input Importance:', input_importance)
print('Output Importance:', output_importance)
print('Feature-Output Correlation:')
print(feature_output_correlation)

你可能感兴趣的:(机器学习,python,神经网络)