-
Notifications
You must be signed in to change notification settings - Fork 0
/
numerMoreComplex.py
200 lines (158 loc) · 5.89 KB
/
numerMoreComplex.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from __future__ import print_function
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import matplotlib as mpl
import matplotlib.ticker as tkr
import matplotlib.dates as mdates
import datetime
import pandas as pd
from pandas.tools.plotting import scatter_matrix
import seaborn as sns
import sklearn
import numpy as np
import tensorflow as tf
from six.moves import cPickle as pickle
from scipy import stats
from sklearn import preprocessing
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import math
import csv
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
import heapq
from decimal import Decimal
from sklearn.model_selection import train_test_split
from cleanf import *
class NanDropper(BaseEstimator, TransformerMixin):
def __init__(self, column):
self.column = column
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.drop('sku', axis=1)
return X.dropna(subset=[self.column])
class DataFrameSelector(BaseEstimator, TransformerMixin):
def __init__(self, feature_names):
self.feature_names = feature_names
def fit(self, X, y=None):
return self
def transform(self, X):
return X[self.feature_names].values
class OutlierHandler(BaseEstimator, TransformerMixin):
def __init__(self, percentile):
self.percentile = percentile
def fit(self, X, y=None):
return self
def transform(self, X):
return X
def normalize_minmax(X):
"""Normalize features to 0-1 range"""
scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))
return scaler.fit_transform(X)
def prepare_matrix(df, is_training=True):
"""Convert dataframe to numpy matrices"""
X = df.drop('target', axis=1).values
X = np.reshape(X, (X.shape[0], -1)).astype(np.float32)
if is_training:
y = df['target'].values
y = np.reshape(y, (y.shape[0], 1)).astype(np.float32)
else:
y = []
return X, y
def split_data(df, scaler=None):
"""Split data into features and labels and optionally scale"""
X, y = prepare_matrix(df, is_training=True)
return X, y, scaler
def split_test_data(df, scaler=None):
"""Prepare test data"""
X, _ = prepare_matrix(df, is_training=False)
return X, scaler
def print_full_dataframe(df):
"""Print full dataframe without truncation"""
pd.set_option('display.max_rows', len(df))
print(df)
pd.reset_option('display.max_rows')
def save_predictions(predictions, output_file, input_file, ids):
"""Save predictions to CSV file"""
print(predictions.shape)
np.set_printoptions(suppress=True)
# Add IDs column
results = np.c_[ids, predictions]
np.savetxt(output_file, results, fmt="%s,%10.8f", delimiter=",")
# Calculate log loss if validation data available
df_val = pd.read_csv(input_file)
df_val = df_val.dropna()
df_val = clean_dataframe(df_val, remove_id_era=True)
X_val, y_val = split_data(df_val)[0:2]
n_rows = y_val.shape[0]
pred_val = predictions[:n_rows, 1]
print(f"Log loss for {output_file}: {sklearn.metrics.log_loss(y_val, pred_val)}")
# Add header
with open(output_file, 'r') as f:
data = f.read()
with open(output_file, 'w') as f:
f.write("id,probability\n" + data)
return True
def prepare_input_data(csv_prefix, df):
"""Prepare and merge input data"""
df.reset_index(drop=True, inplace=True)
correlation = df.corr()
print_full_dataframe(correlation["target"].sort_values(ascending=False))
print(df.info())
print(df.describe())
# Merge prediction files
df_pred1 = pd.read_csv(f'{csv_prefix}1.csv')
df_pred2 = pd.read_csv(f'{csv_prefix}2.csv')
df_pred3 = pd.read_csv(f'{csv_prefix}3.csv')
df = df_pred1.merge(df_pred2, on='id')
df = df.merge(df_pred3, on='id')
df = df.merge(df, on='id')
print(df.info())
print(df.describe())
df = clean_dataframe(df, remove_id_era=False)
print(df.shape)
correlation = df.corr()
print_full_dataframe(correlation["target"].sort_values(ascending=False))
print("Dataset ready!")
return df
# Load and prepare data
df = pd.read_csv('numerai_training_data.csv')
df.reset_index(drop=True)
df.info()
df.describe()
df_old = df.copy()
correlation = df.corr()
print_full_dataframe(correlation["target"].sort_values(ascending=False))
df = clean_dataframe(df, remove_id_era=False)
print(df.shape)
# Split validation set
validation_size = 0.15
df_validation = df.groupby('era', as_index=False).apply(
lambda x: x.loc[np.random.choice(x.index, int(x.shape[0]*validation_size), False), :]
)
df = df[~df.id.isin(df_validation.id)]
# Remove era and id columns
df = df.drop(['era', 'id'], axis=1)
df_validation = df_validation.drop(['era', 'id'], axis=1)
# Prepare training data
X_train, y_train, scaler = split_data(df, scaler=None)
X_val, y_val, _ = split_data(df_validation, scaler=None)
# Prepare test data
df_test = pd.read_csv('numerai_tournament_data.csv')
test_ids = df_test['id'].values.reshape(-1, 1).astype(str)
df_test = clean_dataframe(df_test, remove_id_era=True)
X_test, _ = split_test_data(df_test, scaler=None)
# Prepare old test data
old_ids = df_old['id'].values.reshape(-1, 1).astype(str)
df_old = clean_dataframe(df_old, remove_id_era=True)
X_old, _ = split_test_data(df_old, scaler=None)
# Save predictions
save_predictions(predictions, "predictions.csv", "numerai_tournament_data.csv", test_ids)
save_predictions(predictions_old, "predictions_old.csv", "numerai_training_data.csv", old_ids)