-
Notifications
You must be signed in to change notification settings - Fork 164
/
preprocess_data.py
53 lines (42 loc) · 1.55 KB
/
preprocess_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pandas as pd
# Import sklearn.preprocessing.StandardScaler
from sklearn.preprocessing import MinMaxScaler
def get_normalised_data(data):
"""
Normalises the data values using MinMaxScaler from sklearn
:param data: a DataFrame with columns as ['index','Open','Close','Volume']
:return: a DataFrame with normalised value for all the columns except index
"""
# Initialize a scaler, then apply it to the features
scaler = MinMaxScaler()
numerical = ['Open', 'Close', 'Volume']
data[numerical] = scaler.fit_transform(data[numerical])
return data
def remove_data(data):
"""
Remove columns from the data
:param data: a record of all the stock prices with columns as ['Date','Open','High','Low','Close','Volume']
:return: a DataFrame with columns as ['index','Open','Close','Volume']
"""
# Define columns of data to keep from historical stock data
item = []
open = []
close = []
volume = []
# Loop through the stock data objects backwards and store factors we want to keep
i_counter = 0
for i in range(len(data) - 1, -1, -1):
item.append(i_counter)
open.append(data['Open'][i])
close.append(data['Close'][i])
volume.append(data['Volume'][i])
i_counter += 1
# Create a data frame for stock data
stocks = pd.DataFrame()
# Add factors to data frame
stocks['Item'] = item
stocks['Open'] = open
stocks['Close'] = pd.to_numeric(close)
stocks['Volume'] = pd.to_numeric(volume)
# return new formatted data
return stocks