将以下代码用python重写 attributes = mapminmax(attributes); % 2. 数据归一化 P_train = attributes(:,1:35); % 训练集——35个样本 P_test = attributes(:,36:end); % 测试集——4个样本 net = newc(minmax(P_train),4,0.01,0.01);% 1. 创建网络 net.trainParam.epochs = 1500; %你可以设置训练次数,比如500,1000等,默认是1000 net = train(net,P_train); %开始训练 y= sim(net,P_train); %将训练样本的分类结果以列矩阵列出 y1 = vec2ind(y); %将列矩阵转换成数字类型 y2= sim(net,P_test); %将待检测的分类结果以列矩阵列出 y3= vec2ind(y2); %将待检测的列矩阵转换成数字类型
时间: 2024-03-07 20:46:26 浏览: 89
Class_3_Code.rar_bp数据归一化_neural strength_混凝土 MATLAB_神经网络归一_随机 土
5星 · 资源好评率100%
Here is the Python code equivalent of the given MATLAB code:
```
import numpy as np
from sklearn import preprocessing
from minisom import MiniSom
# Load the dataset
attributes = np.loadtxt('data.txt', delimiter=',')
# Normalize the dataset
attributes = preprocessing.minmax_scale(attributes)
# Split the dataset into training and testing sets
P_train = attributes[:, :35]
P_test = attributes[:, 36:]
# Create a 4x4 self-organizing map
som = MiniSom(4, 4, 35, sigma=0.01, learning_rate=0.01)
# Train the SOM
som.train(P_train, 1500)
# Classify the training set
y_train = np.array([som.winner(p) for p in P_train])
y1 = [np.ravel_multi_index(y_train[i], (4, 4)) for i in range(len(y_train))]
# Classify the testing set
y_test = np.array([som.winner(p) for p in P_test])
y3 = [np.ravel_multi_index(y_test[i], (4, 4)) for i in range(len(y_test))]
```
Note that this code uses the `sklearn` and `minisom` libraries to perform data normalization and self-organizing map clustering, respectively. Also, the input data is assumed to be stored in a file called `data.txt` in comma-separated format.
阅读全文