test_size = int(len(df) * 0.2) # the test data will be 10% (0.1) of the entire data
train = df.iloc[:-test_size,:].copy()
# the copy() here is important, it will prevent us from getting: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead
test = df.iloc[-test_size:,:].copy()
print(train.shape, test.shape)
학습 및 훈련을 위해 데이터 분할
.copy() 는 같은 객체를 참조하기 때문에 하나를 바꾸면 나머지 하나도 바뀐다.
immutable 하기 때문에 다른 것으로 바뀐다.(수정이 아니라 , 대체)
test 와 train 데이터를 나누기 위해 이런 작업을 하는 것 같다
Xscaler = MinMaxScaler(feature_range=(0, 1)) # scale so that all the X data will range from 0 to 1
Xscaler.fit(X_train)
scaled_X_train = Xscaler.transform(X_train)
print(X_train.shape)
Yscaler = MinMaxScaler(feature_range=(0, 1))
Yscaler.fit(y_train)
scaled_y_train = Yscaler.transform(y_train)
print(scaled_y_train.shape)
scaled_y_train = scaled_y_train.reshape(-1) # remove the second dimention from y so the shape changes from (n,1) to (n,)
print(scaled_y_train.shape)
scaled_y_train = np.insert(scaled_y_train, 0, 0)
scaled_y_train = np.delete(scaled_y_train, -1)
y 의 모양을 keras 에 응용가능하게 만들어 주기 위해 좀 조작을 한다
n은 여기에서 행의 개수를 말하는 것이고
scaled_y_train=np.insert(scaled_y_train, 0, 0)
이부분은 0을 첫 공간에 더해줌으로서 한 스텝 앞으로 푸시 해주는 것이다
last time step 을 제거함(reshape(-1))으로서 모양을 유지한다.
이 부분이 오류가 나서 해결하고 싶다
이것은 y축에 독립변수 2차원이상이므로 [['sensor1]]이라고 넣어주니까 된다 !!!
x = tf.keras.utils.normalize(df, axis=1) # x becomes a tensor
x = df.astype('float32') #-->이 두 코드도 중요한 코드이다... 스택오버플로우에서 답을 찾았다...(자료형 오류 때문에)
X_train = train.drop('sensor1',axis=1).copy() y_train = train[['sensor1']].copy() # the double brakets here are to keep the y in a dataframe format, otherwise it will be pandas Series # print(X_train.shape, y_train.shape)
Xscaler = MinMaxScaler(feature_range=(0, 1)) # scale so that all the X data will range from 0 to 1
데이터를 0 에서 1로 scale 해준다 Xscaler.fit(X_train) scaled_X_train = Xscaler.transform(X_train) print(X_train.shape) Yscaler = MinMaxScaler(feature_range=(0, 1)) Yscaler.fit(y_train) scaled_y_train = Yscaler.transform(y_train) print(scaled_y_train.shape) # 18행 1열 (1열만 빼줬으므로 train 을 위해서) scaled_y_train = scaled_y_train.reshape(-1) # remove the second dimention from y so the shape changes from (n,1) to (n,) print(scaled_y_train.shape)
n_input = 25 #how many samples/rows/timesteps to look in the past in order to forecast the next sample n_features= X_train.shape[1] # how many predictors/Xs/features we have to predict y b_size = 32 # Number of timeseries samples in each batch