-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumerGeneralized.py
155 lines (124 loc) · 4.59 KB
/
numerGeneralized.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
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
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, log_loss
from sklearn.model_selection import train_test_split, 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
from decimal import Decimal
from cleanf import *
class NanDropper(BaseEstimator, TransformerMixin):
"""Transformer that drops rows with NaN values in specified column"""
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):
"""Transformer that selects specified columns from dataframe"""
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):
"""Transformer that handles outliers based on percentile"""
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 = X.reshape(X.shape[0], -1).astype(np.float32)
if is_training:
y = df['target'].values
y = y.reshape(-1, 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="%d,%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}: {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