-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python-XKCD-2048.py
298 lines (227 loc) · 8.36 KB
/
Python-XKCD-2048.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#
# Python Version of XKCD cominc 2048
# by Douglas Weadon Higinbotham
#
import matplotlib.pyplot as plt
import numpy as np
from lmfit import Model, Parameters
# The Data
xx = np.array([ 27.5, 53.2, 56.8, 64.2, 72.1, 77.6, 83.3, 91.2, 116. ,
121.9, 132.1, 134.1, 141.5, 143.1, 162.1, 186.8, 196.7, 211.2,
227. , 227.1, 227.6, 231.2, 245.7, 256.2, 262. , 267. , 267.9,
271.3, 273.5, 279.8, 284.3])
yy = np.array([ 44.8, 117.4, 62.7, 181.5, 52.1, 82.4, 73.1, 84.4, 102.2,
143.3, 81.1, 62.3, 72.6, 58.1, 45.1, 49.7, 102.9, 142.5,
151.5, 258.4, 120.9, 138.5, 221.2, 184.4, 20.9, 164.6, 120.3,
190.2, 203. , 220.3, 184.2])
# The Definitions
def llog(x,e0,e1,e2):
return e1*np.log(x)+e0
def expp(x,e1,e0):
return e0*np.exp(x*e1)
def const(x,e0):
return e0
def linear(x,e0,e1):
return x*e1+e0
def quad(x,e0,e1,e2):
return e2*x**2+e1*x+e0
def sigmoid(x, x0, k,a):
y = a+ a / (1 + np.exp(-k*(x-x0)))
return y
def epoly(q2,**params): # this calculates 1+param[0]*q^2+param[1]*q^4...
value=params['e0']+params['e1']*q2+params['e2']*q2**2+params['e3']*q2**3+params['e4']*q2**4+params['e5']*q2**5+params['e6']*q2**6+params['e7']*q2**7+params['e8']*q2**8+params['e9']*q2**9+params['e10']*q2**10
return value
# Polynomial Fit Parameters
parame=Parameters()
parame.add('e0',value=0,vary=1)
parame.add('e1',value=0,vary=1)
parame.add('e2',value=0,vary=1)
parame.add('e3',value=0,vary=1)
parame.add('e4',value=0,vary=1)
parame.add('e5',value=0,vary=1)
parame.add('e6',value=0,vary=1)
parame.add('e7',value=0,vary=1)
parame.add('e8',value=0,vary=1)
parame.add('e9',value=0,vary=1)
parame.add('e10',value=0,vary=1)
# The Fits Using LMFIT
model=Model(linear)
lresult=model.fit(yy,x=xx,e0=1,e1=-1)
model=Model(linear)
l1result=model.fit(yy[0:15],x=xx[0:15],e0=1,e1=-1)
model=Model(linear)
l2result=model.fit(yy[15:],x=xx[15:],e0=1,e1=-1)
model=Model(quad)
qresult=model.fit(yy,x=xx,e0=1,e1=-1,e2=0)
model=Model(const)
cresult=model.fit(yy,x=xx,e0=1)
model=Model(epoly)
eresult=model.fit(yy,q2=xx,params=parame)
model=Model(llog)
logresult=model.fit(yy,x=xx,e0=1,e1=1)
model=Model(expp)
exppresult=model.fit(yy,x=xx,e1=0.1,e0=1)
model=Model(sigmoid)
istresult=model.fit(yy,x=xx,x0=180,k=10,a=2)
# The Twelve Plots
myx=np.linspace(20,295,3000)
plt.figure(dpi=75,figsize=(14,10))
plt.xkcd()
plt.subplot(3,4,1)
plt.xkcd()
plt.suptitle('CURVE-FITTING METHODS AND THE MESSAGES THEY SEND')
plt.xkcd()
plt.plot(xx,yy,'o',color='black')
plt.plot(myx,linear(myx,**lresult.best_values),color='red')
plt.annotate('Linear',xy=(20,240),color='grey')
plt.xlabel('"HEY! I DID A \n REGRESSION."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,2)
plt.plot(xx,yy,'o',color='black')
plt.plot(myx,quad(myx,**qresult.best_values),color='red')
plt.annotate('Quadratic',xy=(20,240),color='grey')
plt.xlabel('"I WANTED A CURVED \n LINE, SO A MADE ONE \n WITH MATH."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,3)
plt.plot(xx,yy,'o',color='black')
plt.plot(myx,llog(myx,**logresult.best_values),color='red')
plt.annotate('LOGARITHMIC',xy=(20,240),color='grey')
plt.xlabel('"LOOK, IT\'S \n TAPPERING OFF"')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,4)
plt.plot(xx,yy,'o',color='black')
plt.plot(myx,expp(myx,**exppresult.best_values),color='red')
plt.annotate('EXPONENTIAL',xy=(20,240),color='grey')
plt.xlabel('"LOOK, IT\'S GROWING \n UNCONTROLLABLY"')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
# Using Statsmodel For Local Regression
import statsmodels.api as sm
lowess = sm.nonparametric.lowess
nnn=lowess(yy,xx,frac=0.4)
plt.xkcd()
plt.subplot(3,4,5)
plt.plot(nnn[:,0],nnn[:,1],'-',color='red')
plt.plot(xx,yy,'o',color='black')
plt.annotate('LOESS',xy=(20,240),color='grey')
plt.xlabel('"I\'M SOPHISTICATED, NOT \n LIKE THOSE BUMBLING \n POLYNOMIAL PEOPLE."')
plt.annotate('by Douglas Higinbotham in Python inspired by https://xkcd.com/2048',xy=(20,-520),rotation=0,annotation_clip=False,fontsize=10)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,6)
plt.plot(xx,yy,'o',color='black')
plt.plot([np.min(myx),np.max(myx)],[cresult.best_values['e0'],cresult.best_values['e0']],color='red',label='Linear Regression')
plt.annotate('Linear',xy=(20,240),color='grey')
plt.annotate('No Slope',xy=(20,210),color='grey')
plt.xlabel('"I\'M MAKING A \n SCATTER PLOT BUT \n I DON\'T WANT TO"')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,7)
plt.plot(xx,yy,'o',color='black')
plt.plot(xx,sigmoid(xx,**istresult.best_values),color='red')
plt.annotate('SIGMOID',xy=(20,235),color='grey')
plt.xlabel('"I NEEDED TO CONNECT \n THESE TWO LINES."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,8)
a=lresult.eval_uncertainty(x=xx,sigma=3)
plt.fill_between(xx,qresult.best_fit-a,qresult.best_fit+a,color='pink')
plt.plot(xx,qresult.best_fit-a,'-',color='red')
plt.plot(xx,qresult.best_fit+a,color='red')
plt.plot(xx,yy,'o',color='black')
plt.annotate('95% Confidence',xy=(20,240),color='grey')
plt.annotate('Interval',xy=(20,210),color='grey')
plt.xlabel('"LISTEN, SCIENCE IS HARD \n BUT I\'M A SERIOUS PERSON \n DOING MY BEST."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,9)
plt.plot(xx,yy,'o',color='black')
plt.plot(xx[0:15],linear(xx[0:15],**l1result.best_values),color='red')
plt.plot(xx[15:],linear(xx[15:],**l2result.best_values),color='red')
plt.annotate('PIECEWISE',xy=(20,235),color='grey')
plt.xlabel('"NOW I JUST NEED TO \n RENORMALIZE THE DATA."')
aa=l1result.eval_uncertainty(x=xx[0:15],sigma=1)
bb=l2result.eval_uncertainty(x=xx[15:],sigma=1)
plt.plot(xx[0:15],linear(xx[0:15],**l1result.best_values)+aa,color='red',lw=1)
plt.plot(xx[0:15],linear(xx[0:15],**l1result.best_values)-aa,color='red',lw=1)
plt.plot(xx[15:],linear(xx[15:],**l2result.best_values)+bb,color='red',lw=1)
plt.plot(xx[15:],linear(xx[15:],**l2result.best_values)-bb,color='red',lw=1)
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,10)
plt.plot(xx,yy,color='red')
plt.plot(xx,yy,'o',color='black')
plt.annotate('CONNECT',xy=(20,240),color='grey')
plt.annotate('THE DOTS',xy=(20,210),color='grey')
plt.xlabel('"REGRESSION?! JUST USE \n THE DEFAULT PLOTTING."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,11)
import matplotlib.image as mpimg
img=mpimg.imread('Figures/elephant.png')
plt.imshow(img,extent=(10,330,20,270))
plt.annotate('Elephant',xy=(20,240),color='grey')
plt.xlabel('"AND WITH FIVE \n PARAMETERS I CAN MAKE \n ITS TRUNK WIGGLE."')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.xkcd()
plt.subplot(3,4,12)
#plt.xkcd()
plt.plot(xx,yy,'o',color='black')
plt.plot(myx,epoly(myx,**eresult.best_values),color='red')
plt.annotate('House of Cards',xy=(20,235),color='grey')
plt.xlabel('"AS YOU CAN SEE, THIS \n MODEL SMOOTHLY FITS \n THE --- NO NO WAIT DON\'T \n EXTEND IT AAAAA!"')
frame1 = plt.gca()
frame1.axes.xaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticklabels([])
frame1.axes.yaxis.set_ticks([])
frame1.axes.xaxis.set_ticks([])
plt.tight_layout(rect=[0, 0.01, 1, 0.975])
plt.savefig('Figures/Python-XKCD-2048.png')
plt.savefig('Figures/Python-XKCD-2048.pdf')
plt.show()