|
- import numpy as np
- import torch
- from torch import nn
- from torch.autograd import Variable
- from sklearn.preprocessing import MinMaxScaler
-
- # npz_file = np.load('/home/yuantk/ytkenv/data/anom_all_year_south1.npz')
- npz_file = np.load(r'D:\data\after_processed\complited_data\feature_selection_anom_sla_u_v_93_17_south.npz')
- # 查看压缩文件里面的文件
- print(npz_file.files) #['time', 'anom', ‘sst', 'sla', 'u', 'v']
-
- time = npz_file['time']
- anom = npz_file['anom']
-
- print(anom.shape)
- #
- anom1 = anom[:,0:3,0:3] #L1 0.125N - 8.125N; 105.125N - 113.625N
- print(anom1.shape) #(9131, 5, 5)
- #
- anom11111 = anom1
- # 数据归一化
- mm = MinMaxScaler()
- anom1 = anom1.reshape(-1,1)
- anom1 = mm.fit_transform(anom1)
-
- anom1 = anom1.reshape(9131,3,3)
- # print(anom1)
- print(anom1.shape)
- # print(anom1)
-
-
- # 3days for 1 days
- dim = anom1.shape[1]
- train_X, train_Y = [], []
-
- for i in range(anom1.shape[0]-3-2-1):
- a = anom1[i:(i+3),:]
- train_X.append(a)
- tempb = anom1[(i+3):(i+3+1),:]
- b = []
- for j in range(len(tempb)): #len(tempb) = 7
- # for k in range(dim):
- b.append(tempb[j])
- train_Y.append(b)
- train_X = np.array(train_X,dtype='float64')
- train_Y = np.array(train_Y,dtype='float64')
-
- print(train_X.shape) #(9125, 3, 5, 5)
- print(train_Y.shape) #(9125, 1, 5, 5)
-
- #划分训练集和测试集,70%做训练集
- train_size = int(len(train_X) * 0.7)
- test_size =len(train_X) - train_size
-
- train_x = train_X[:train_size]
- train_y = train_Y[:train_size]
- test_x = train_X[train_size:]
- test_y = train_Y[train_size:]
-
- train_x = torch.from_numpy(train_x)
- train_y = torch.from_numpy(train_y)
- test_x = torch.from_numpy(test_x)
- test_y = torch.from_numpy(test_y)
-
- anom111112 = anom11111[:train_size]
- print(train_x.shape) #torch.Size([6387, 3, 5, 5])
- print(train_y.shape) #torch.Size([6387, 1, 5, 5])
- print(test_x.shape) #torch.Size([2738, 3, 5, 5])
- print(test_y.shape) #torch.Size([2738, 1, 5, 5])
- #
- train_x = train_x.reshape(6387,3,1,3,3)
- train_y = train_y.reshape(6387,1,1,3,3)
- test_x = test_x.reshape(2738,3,1,3,3)
- test_y = test_y.reshape(2738,1,1,3,3)
- print(train_x.shape)
- print(train_y.shape)
- print(test_x.shape)
- print(test_y.shape)
-
- train_x1 = np.squeeze(train_x)
- print(train_x1.shape) #torch.Size([6387, 3, 5, 5])
- #
|