-
Notifications
You must be signed in to change notification settings - Fork 2
/
Boxy.py
183 lines (133 loc) · 5.31 KB
/
Boxy.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
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Author: Niam Moltta
# UY - 2017
# License : MIT
# Fixig skewness with Box Cox
import pandas as pd
import matplotlib.pylab as plt
from sklearn import preprocessing
import re
import seaborn
import numpy as np
from scipy.stats import boxcox
from scipy.stats import skew
print ' '
print ' '
print ' Welcome to Boxy.py'
print ' -- by Niam Moltta --'
print ' ~~/\//V\ '
print ' '
print ' '
print 'Application: SKEWNESS REDUCTION WITH BOXCOX.\n\nINSTRUCTIONS:\n\n- You need to run this program in the same folder that contains your data.\n * Data must NOT contain negative values.\n- Select file, select column.\n- Returns original skewed data histogram. \n- Returns un-skewed data using sqrt histogram.\n- Returns un-skewed data using BoxCox histogram.\n- Select the un-skewed data that you prefer to print in a new file.\n- Create file with new data.\n\nWhen prompted:\n\n- Enter "Y" to create a .txt file with the BoxCox fixed data.\n- Enter "Ybut" to create a .txt file with the sqrt transformed data.\n- Enter "n" to work with another variable, or:\n- Enter "ya" to finish the program.\n\n * Sometimes using "sqrt" (square root) is enough to reduce skewness.\n'
fhand = raw_input('Enter file name: ')
filecsv = str(fhand)
if filecsv == '':
print ' '
print 'Arrivederci!'
print ' '
exit()
data = pd.read_csv(filecsv)
print ' '
frame = pd.DataFrame(data)
coolist = frame.columns
columns = np.asarray(coolist)
while True:
print ' '
print 'Columns in', re.findall('(.+?).csv', filecsv), 'are:\n'
print columns
print ' '
hand = raw_input('Enter column header:\n\n')
column = str(hand)
if (column == 'ya') | (column == ''):
break
else:
# Replace missing values with zeros in the selected [column]
data[column].fillna(0,inplace=True)
print ' '
print 'Missing values were replaced with zeros'
print ' '
# Scale method from scikit-learn to transform the distribution
Col = preprocessing.scale(np.sqrt(data[column]))
# Get rid of zeros:
Boxy = preprocessing.scale(boxcox(data[column]+1)[0])
PreBox = preprocessing.scale(data[column])
# Calculate skewness again:
skness = skew(Col)
sknessBoxy = skew(Boxy)
sknessPreBox = skew(PreBox)
print ' '
print 'Original Skewness =',sknessPreBox,'\nSqrt Skewness =', skness,'\nBoxCox Skewness =', sknessBoxy,'\n'
print ' '
#Draw histograms:
figure = plt.figure()
figure.add_subplot(131)
plt.hist(Col,facecolor='red',alpha=0.75)
label1 = str('Skewness = ') + str(round(skness, 4))
lab = str(label1)
plt.xlabel(lab)
plt.title("Transformed using 'sqrt'")
plt.text(2,100000,"Skewness: {0:.2f}".format(skness))
figure.add_subplot(132)
plt.hist(Boxy,facecolor='lightblue',alpha=0.75)
label2 = str('Skewness = ') + str(round(sknessBoxy, 4))
labe = str(label2)
plt.xlabel(labe)
plt.title("Transformed using 'BoxCox'")
plt.text(2,100000,"Skewness: {0:.2f}".format(sknessBoxy))
figure.add_subplot(133)
plt.hist(PreBox,facecolor='green',alpha=0.75)
label3 = str('Skewness = ') + str(round(sknessPreBox, 4))
label = str(label3)
plt.xlabel(label)
plt.title("Original Histogram")
plt.text(2,100000,"Skewness: {0:.2f}".format(sknessPreBox))
plt.show()
print ' '
print ' -- Save the figure, or close it to continue -- '
print ' '
user = raw_input('Enter Y/n to create file: ')
answer = str(user)
if answer == 'Y':
Boxy = preprocessing.scale(boxcox(data[column]+1)[0])
namef = raw_input('Enter the file name and make sure you add ".txt"\n\n')
name = str(namef)
# Create file with standarized data:
nfile = open(name, 'w')
# Fill it with data:
for value in Boxy:
val = str(value)
nfile.write(val)
nfile.write('\n')
nfile.close()
print ' '
print 'File created as', name
print ' '
elif answer == 'Ybut':
print ' '
print "You'll get the sqrt transformed data instead of the BoxCox"
print ' '
Col = preprocessing.scale(np.sqrt(data[column]))
namef = raw_input('Enter the file name and make sure you add ".txt"\n\n')
name = str(namef)
# Create file with standarized data:
nfile = open(name, 'w')
# Fill it with data:
for value in Col:
val = str(value)
nfile.write(val)
nfile.write('\n')
nfile.close()
print ' '
print 'File created as', name
print ' '
elif (answer == 'n')|(answer == ''):
print ' '
print 'Not a single f***ile was created.'
print ' '
else:
break
print ' '
print 'Hasta la vista, human.'
print ' '
exit()