将以下代码用python重写,读取excel表 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 07:46:29 浏览: 93
如何用python读取excel数据
Here is the Python code equivalent of the given MATLAB code, assuming that the input data is stored in an Excel file called `data.xlsx`:
```
import numpy as np
import pandas as pd
from sklearn import preprocessing
from minisom import MiniSom
# Load the dataset from Excel file
data = pd.read_excel('data.xlsx', header=None)
attributes = data.values
# 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 `pandas`, `sklearn`, and `minisom` libraries to read data from an Excel file, perform data normalization, and self-organizing map clustering, respectively. The input data is assumed to be in the first sheet of the Excel file, with no header row.
阅读全文