-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMLGeneral.py
executable file
·1581 lines (1513 loc) · 59 KB
/
MLGeneral.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Library for performing machine learning tasks on diffusional fingerprints
Henrik Dahl Pinholt
"""
import matplotlib
from matplotlib import animation
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.decomposition import PCA
# import pandas as pd
import math
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets, svm, metrics
from pomegranate import *
import pandas as pd
def histogram(
data,
range=(None, None),
bins=None,
remove0=False,
plot=True,
savefig="",
labels=None,
bars=False,
ax=None,
color="r",
alpha=1,
remove_num=None,
calibration=None,
legend=None,
logbin=False,
normalize=False,
elinewidth=5,
capsize=4,
):
"""Get data for histogram
--------
Params
--------
data: 1D ndarray
- Raw Data
xmin: float
- Minimum value for data
xmax: float
- Maximum value for data
nbins: integer
- Number of bins in histogram
plot: Boolean
- Wether to plot histogram
savefig: string
- Wether to save the plotted histogram
labels: dictionary
- Values to pass to ax.set()
legend: optional, str
- What to call the plot in a legend
logbin: optional, Boolean
- wether to bin the histogram logarithmically
"""
xmin, xmax = range
if xmin is None:
xmin = np.min(data)
if xmax is None:
xmax = np.max(data)
if bins is None:
bins = np.max([int(len(data) / 15), 5])
if logbin:
N = bins
inputs = np.logspace(np.log10(xmin), np.log10(xmax), N)
if normalize:
hist = np.histogram(data, bins=inputs, normed=True)
hist2 = np.histogram(data, bins=inputs)
else:
hist = np.histogram(data, bins=inputs)
else:
if normalize:
hist = np.histogram(data, bins=bins, range=(xmin, xmax), normed=True)
hist2 = np.histogram(data, bins=bins, range=(xmin, xmax))
else:
hist = np.histogram(data, bins=bins, range=(xmin, xmax))
counts, bin_edges = hist
bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1])
if calibration is None:
mask1 = (xmin < bin_centers) & (bin_centers <= xmax)
if remove0:
mask2 = counts > 0
if remove_num is not None:
mask3 = counts > remove_num
mask_final = mask1 & mask2 & mask3
else:
mask_final = mask1 & mask2
else:
mask_final = mask1
x, y, sy = (
bin_centers[mask_final],
counts[mask_final],
np.sqrt(counts[mask_final]),
)
if normalize:
counts1, bin_edges1 = hist2
bin_centers1 = 0.5 * (bin_edges1[1:] + bin_edges1[:-1])
mask1 = (xmin < bin_centers1) & (bin_centers1 <= xmax)
if remove0:
mask2 = counts1 > 0
if remove_num is not None:
mask3 = counts > remove_num
mask_final = mask1 & mask2 & mask3
else:
mask_final = mask1 & mask2
else:
mask_final = mask1
x1, y1, sy1 = (
bin_centers1[mask_final],
counts1[mask_final],
np.sqrt(counts1[mask_final]),
)
relerr = y1 / sy1
sy = y / relerr
else:
counts = counts - calibration(bin_centers)
mask1 = (xmin < bin_centers) & (bin_centers <= xmax)
if remove0:
mask2 = counts > 0
if remove_num is not None:
mask3 = counts > remove_num
mask_final = mask1 & mask2 & mask3
else:
mask_final = mask1 & mask2
else:
mask_final = mask1
x, y, sy = (
bin_centers[mask_final],
counts[mask_final],
np.sqrt(counts[mask_final]),
)
if plot:
if ax is None:
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
if logbin is None:
Binwidth = (xmax - xmin) / bins
else:
Binwidth = (bin_edges[1:] - bin_edges[:-1])[mask_final]
if not bars:
if legend is None:
ax.errorbar(
x,
y,
yerr=sy,
xerr=Binwidth / 2,
linestyle="",
ecolor=color,
fmt=".",
mfc=color,
mec=color,
capsize=capsize,
elinewidth=elinewidth,
)
else:
ax.errorbar(
x,
y,
yerr=sy,
xerr=Binwidth / 2,
linestyle="",
ecolor=color,
fmt=".",
mfc=color,
mec=color,
capsize=capsize,
label=legend,
elinewidth=elinewidth,
)
else:
if legend is None:
ax.bar(
x,
y,
width=Binwidth,
color=color,
yerr=sy,
capsize=capsize,
alpha=alpha,
error_kw={"elinewidth": elinewidth},
)
else:
ax.bar(
x,
y,
width=Binwidth,
color=color,
yerr=sy,
capsize=capsize,
alpha=alpha,
label=legend,
error_kw={"elinewidth": elinewidth},
)
if np.abs(np.mean(Binwidth)) < 100:
ax.set(xlabel="x", ylabel=f"Frequency / {np.abs(np.mean(Binwidth)):4.2e}")
else:
ax.set(xlabel="x", ylabel=f"Frequency / {np.abs(np.mean(Binwidth)):4.2e}")
if labels is not None:
ax.set(**labels)
if logbin:
ax.set_xscale("log")
if legend is not None:
ax.legend()
if savefig != "":
plt.savefig(savefig + ".pdf")
if ax is None:
plt.show()
return x, y, sy
def radarplot(values, colors, labels, savefig=False, show=True):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (C) 2011 Nicolas P. Rougier
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the glumpy Development Team nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# -----------------------------------------------------------------------------
import numpy as np
import matplotlib
import matplotlib.path as path
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cm as cm
print(len(values))
# Data to be represented
# ----------
properties = ["Precision", "Recall", "F1"]
# ----------
# Choose some nice colors
matplotlib.rc("axes", facecolor="white")
# Make figure background the same colors as axes
fig = plt.figure(figsize=(10, 10), facecolor="white")
# Use a polar axes
axes = plt.subplot(111, polar=True)
for v, n, l in zip(values, colors, labels):
v = v
# Set ticks to the number of properties (in radians)
t = np.arange(0, 2 * np.pi, 2 * np.pi / len(properties))
plt.xticks(t, [])
# Set yticks from 0 to 10
plt.yticks(np.linspace(0, 1, 11))
# Draw polygon representing values
points = [(x, y) for x, y in zip(t, v)]
points.append(points[0])
points = np.array(points)
codes = (
[path.Path.MOVETO]
+ [path.Path.LINETO] * (len(v) - 1)
+ [path.Path.CLOSEPOLY]
)
_path = path.Path(points, codes)
_patch = patches.PathPatch(
_path, fill=True, color=n, linewidth=0, alpha=0.4, label=l
)
axes.add_patch(_patch)
_patch = patches.PathPatch(_path, fill=False, linewidth=2)
axes.add_patch(_patch)
# Draw circles at value points
plt.scatter(
points[:, 0],
points[:, 1],
linewidth=2,
s=50,
color="white",
edgecolor="black",
zorder=10,
)
# Set axes limits
plt.ylim(0, 1)
# Draw ytick labels to make sure they fit properly
for i in range(len(properties)):
angle_rad = i / float(len(properties)) * 2 * np.pi
angle_deg = i / float(len(properties)) * 360
ha = "right"
if angle_rad < np.pi / 2 or angle_rad > 3 * np.pi / 2:
ha = "left"
plt.text(
angle_rad,
1.1,
properties[i],
size=14,
horizontalalignment=ha,
verticalalignment="center",
)
# A variant on label orientation
# plt.text(angle_rad, 11, properties[i], size=14,
# rotation=angle_deg-90,
# horizontalalignment='center', verticalalignment="center")
# Done
plt.legend()
if savefig != False:
plt.savefig(savefig + ".pdf", facecolor="white", dpi=500)
if show:
plt.show()
def Meancalc(data, targetnum, n_comp, groups):
"""calculates mean of all target groups
Parameters
------------
data : np.array shape(N_datapoints,N_features)
input data, rows are datapoints, and columns are features
targetnum : np.array of ints
datapoint labels
n_comp :
"""
import numpy as np
means = np.zeros((n_comp, groups))
nums = np.zeros(groups)
for i in range(len(targetnum)):
means[:, targetnum[i]] += data[i]
nums[targetnum[i]] += 1
for i in range(n_comp):
means[i] /= nums
return means
def Rfinder(X, targetnum, means, sumlist, frac, chull=False, vertices=False):
"""Finds the convex hull of a fraction of the points found from going away
from the meancenter and counting points returns the radius of the sphere
that contains frac of points from mean"""
from scipy.spatial import ConvexHull
# import and grab target group points
t = targetnum
triangles, vlist, simplist, rlist, dlist, Tgroups = [], [], [], [], [], []
for i in range(len(sumlist)):
Tgroups.append(X[t == i])
for i in range(len(sumlist)):
d = np.array(
[
np.sqrt(
np.sum(
[
(Tgroups[i][j, v] - means[:, i][v]) ** 2
for v in range(len(X[0]))
]
)
)
for j in range(len(Tgroups[i]))
]
)
sorter = np.argsort(d)
pnum = int(sumlist[i] * frac)
rlist.append(d[sorter][pnum])
# Generate convex hull from points up til pnum
data = np.array(Tgroups[i][sorter][:pnum])
if len(data) > 0:
Chull = ConvexHull(data)
triangles.append(Chull.simplices)
vlist.append(Chull.vertices)
dlist.append(data)
else:
return "No points"
if vertices:
return triangles, dlist, rlist, vlist
else:
return triangles, dlist, rlist
SMALL_SIZE = 15
MEDIUM_SIZE = 18
BIGGER_SIZE = 22
plt.rc("font", size=SMALL_SIZE) # controls default text sizes
plt.rc("axes", titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc("axes", labelsize=SMALL_SIZE) # fontsize of the x and y labels
plt.rc("xtick", labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc("ytick", labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc("legend", fontsize=SMALL_SIZE) # legend fontsize
plt.rc("figure", titlesize=BIGGER_SIZE) # fontsize of the figure title
class ML:
def __init__(self, X, y, center=True):
"""Initialize ML object
---------
Params
---------
X: ndarray of shape (n_features,n_samples)
-Input training data
y: ndarray of shape(n_samples)
-Input labels for training data
"""
self.X = X
self.stringy = y
# save list of unique labels
# Convert to number labels as well
tlist = pd.unique(y)
self.to_string = dict(zip(range(len(tlist)), tlist)) # Make int->string dict
self.to_int = {v: k for k, v in self.to_string.items()} # Make string->int dict
self.y = np.array([self.to_int[t] for t in y]) # Convert input
self.unique = pd.unique(self.y)
self.stringyunique = pd.unique(self.stringy)
if center:
self.Center()
def Center(self, center=True, verbose=False):
"""Center the column means of the data (important for dimension reduction)
---------
Params
---------
verbose: Boolean
- Wether to print progress
center: Boolean
- Wether to center internal X (true) or return centered data (false)
---------
Returns (if center=False)
---------
X: ndarray
- Column centered data
T: StandardScaler object
- Object used to do the rescaling of original data
"""
if verbose:
s = np.sum(np.mean(self.X, axis=1))
print("Centering column means, current colmean sum is %.3f" % s)
scaler = StandardScaler()
scaler.fit(self.X)
X = scaler.transform(self.X)
if center:
self.X = X
self.scaler = scaler
else:
return X, scaler
if verbose:
s = sum([np.mean(self.X[:, i]) for i in range(len(self.X[0, :]))])
print("sum of colmeans is now %.3f" % s)
def Reduce(self, method, n_components=2, reduce=True, verbose=False):
"""Reduce dimensionality of the data
---------
Params
---------
method: string {pca","lin"} or pca or lin object with .transform() property
- type of transformation, or the object to transform with
n_components: integer, default=2
- Number of components to transform to, has no effect if a method
input is a transformation object
reduce: Boolean
- Wether to reduce internal X (true) or return reduced data (false)
verbose: Boolean
- Wether to print progress
---------
Returns (if reduce=False)
---------
X: ndarray
- Reduced data
T: pca or LinearDiscriminantAnalysis object
- Object used to do the rescaling of original data
"""
if method == "pca":
if verbose:
print(
"----Transforming data to a %dD hyperplane with PCA-----"
% n_components
)
L = PCA(n_components=n_components)
X = L.fit_transform(self.X)
elif method == "Tsne":
from sklearn.manifold import TSNE
L = TSNE(n_components=n_components, perplexity=50, verbose=1)
X = L.fit_transform(self.X)
elif method == "lin":
if verbose:
print(
"----Transforming data to a %dD hyperplane with LDA-----"
% n_components
)
L = LinearDiscriminantAnalysis(n_components=n_components)
X = L.fit_transform(self.X, self.y)
elif type(method.transform) == type(self.Center):
L = method
X = L.transform(self.X)
else:
raise ValueError("type has to be lin or pca or have .transform property")
if verbose:
print("Transformation done")
# Decide to reduce or not and save or return transformation
self.T = L
if reduce:
self.X = X
else:
return X, L
def GetSumlist(self):
"""Returns a list of the number of items for each class in data"""
sumlist = np.zeros(len(self.unique))
# Count number times every name appears in the target
counts = pd.Series(self.y).value_counts().values
names = pd.Series(self.stringy).value_counts().index.values
sorted = np.argsort(names)
names, counts = names[sorted], counts[sorted]
# Add counts to sumlist in same order as in self.unique
for i in range(len(counts)):
sumlist[self.to_int[names[i]]] = counts[i]
return sumlist
def ProjectPlot(
self,
savefig="",
xlims=[1],
ylims=[2],
zlims=[2],
yscale="linear",
frac=0.68,
spheres=False,
lines=False,
points=False,
skip=1,
s=0.01,
lw=0.03,
chull=True,
alpha=0.6,
With2D=False,
axis=None,
animate=False,
verbose=True,
frames=360,
colors=None,
legend=True,
Get_mesh=False,
):
"""Plots the input data X if it is 2 or 3 dimensional
----------
Params
----------
savefig: string
- filename for saved plots
xlims: itterable 1x2:
- xlims of plot
ylims: itterable 1x2:
- ylims of plot
zlims: itterable 1x2:
- zlims of plot (if 3D)
yscale: string
- scale on y-axis ("log", "linear" (default))
frac: float in range[0,1]
- fraction of points to include in convex hull
default -> 0.68
spheres: boolean
- wether to plot spheres containing frac points from the mean in 3D
default -> False
lines: boolean
- wether to plot lines from cluster mean to point
default -> False
points: Boolean
- wether to plot datapoints
default -> False
skip: integer
- What lines to skip (for example 5 skips every fitfth line)
default -> 1 (none)
s: float,
- markersize in scatterplot
default -> 0.01
lw: float,
- linewidth for lines
default -> 0.03
chull: boolean
- wether to plot chulls of frac points from mean
default -> True
alpha: float [0,1]
- alpha of the chulls/spheres
default -> 0.6
With2D: boolean
- Wether to plot with 2D projections in 3D
default -> False
axis: axis object, optional
- Axis for which to plot on
verbose: boolean
- wether to print stuff along the way
animate: string
- string to save animated rotation of the 3D plot
frames: int
- number of frames to animate if animate is set to a string
"""
import numpy as np
X, Y = self.X, self.stringy
t = self.y
sumlist = self.GetSumlist()
n_components = len(X[0, :])
m = Meancalc(X, t, n_components, len(sumlist))
# plot if dimension = 2
if colors is None:
colors = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
if n_components == 2:
if len(xlims) == 1:
xlims = [np.min(X[:, 0]), np.max(X[:, 0])]
if len(ylims) == 1:
ylims = [np.min(X[:, 1]), np.max(X[:, 1])]
"""make histogram for each lipase"""
ncols = math.ceil(len(self.stringyunique) ** (0.5))
if len(self.stringyunique) ** (0.5) % ncols == 0.0:
nrows = ncols
else:
nrows = ncols - 1
fig1, ax1 = plt.subplots(ncols, nrows, figsize=(12, 8))
if type(ax1) == type(np.array([])):
for i, a in zip(self.stringyunique, ax1.flatten()):
hist = a.hist2d(
X[:, 0][Y == i],
X[:, 1][Y == i],
bins=100,
range=[xlims, ylims],
cmin=1,
)
a.set(xlabel="1st axis", ylabel="2nd axis", title=i)
a.grid()
cbar = fig1.colorbar(hist[-1], ax=a)
cbar.set_label(f"Frequency", rotation=270)
else:
hist = ax1.hist2d(
X[:, 0], X[:, 1], bins=100, range=[xlims, ylims], cmin=1
)
ax1.set(xlabel="1st axis", ylabel="2nd axis", title=Y[0])
ax1.grid()
cbar = fig1.colorbar(hist[-1], ax=ax1)
cbar.set_label(f"Frequency", rotation=270)
fig1.tight_layout()
"""make scatter plot"""
taken = []
if verbose:
print("plotting scatterplot")
# check for added axis
if axis is None:
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
else:
ax = axis
if chull:
triangles, dlist, rlist, verts = Rfinder(
X, t, m, sumlist, frac, vertices=True
)
for tris, c, d in zip(verts, colors[: len(triangles)], dlist):
ax.fill(d[tris][:, 0], d[tris][:, 1], c, alpha=alpha)
# Itterate through all targets in the data
for name in self.stringyunique:
number = self.to_int[name]
if points:
ax.scatter(
X[Y == name][::skip][:, 0],
X[Y == name][::skip][:, 1],
s=s,
marker=".",
label=name,
c=colors[number],
)
if lines:
if verbose:
print("plotting lines")
# Print % done
# if i % (int(len(X)/10)) == 0 and i != 0 and verbose:
# print ("%d %% done" % (int((i*100)/float(len(X)))))
ax.scatter(m[:, number][0], m[:, number][1], s=1, c=colors[number])
for i in range(len(X)):
if i % skip == 0:
a = [m[:, number][0], X[i, :][0]]
b = [m[:, number][1], X[i, :][1]]
ax.plot(a, b, linewidth=lw, c=colors[number])
# Fix plot to look pretty
if legend:
lgnd = ax.legend(loc=2, borderaxespad=0.0)
# set size on points in legend
for i in range(len(lgnd.legendHandles)):
lgnd.legendHandles[i]._sizes = [20]
# Set limits and labels
ax.set_xlim(xlims[0], xlims[1])
ax.set_ylim(ylims[0], ylims[1])
ax.set_xlabel("1st axis")
ax.set_ylabel("2nd axis")
ax.set_yscale(yscale)
# Save if needed
if savefig != "":
plt.savefig(savefig, dpi=700, bbox_inches="tight")
fig1.savefig(savefig + "Hist", dpi=700)
plt.tight_layout()
if axis is None:
plt.show()
# Plot as 3D surface/graph if n_components is 3
if n_components == 3:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# Initialize/calculate important variables
if axis == None:
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection="3d")
else:
ax = axis
taken = []
if colors is None:
colors = ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
print("plotting")
# Itterate through all points in the reduced data
for i in range(len(X)):
# Print % done
if i % (int(len(X) / 10)) == 0 and i != 0:
print("%d %% done" % (int((i * 100) / float(len(X)))))
# Check if it is the first time this lipase is plotted
if t[i] in taken:
# if not, no label and keep plotting
label = None
else:
label = self.y[i]
taken.append(t[i])
ax.scatter(
m[:, t[i]][0],
m[:, t[i]][1],
m[:, t[i]][2],
s=s,
c=colors[t[i]],
label=self.to_string[label],
)
triangles, dlist, rlist = Rfinder(X, t, m, sumlist, frac)
if spheres:
# If spheres, plot it around the mean centers based
# on calculated radius
r = rlist[t[i]]
u = np.linspace(0, 2 * np.pi, 10)
v = np.linspace(0, np.pi, 10)
x = r * np.outer(np.cos(u), np.sin(v)) + m[:, t[i]][0]
y = r * np.outer(np.sin(u), np.sin(v)) + m[:, t[i]][1]
z = r * np.outer(np.ones(np.size(u)), np.cos(v)) + m[:, t[i]][2]
# Plot the surface
ax.plot_surface(
x,
y,
z,
rstride=1,
cstride=1,
color=colors[t[i]],
alpha=alpha,
linewidth=0,
)
if chull:
for tr, c, d in zip(triangles, colors[: len(triangles)], dlist):
x, y, z = d[:, 0], d[:, 1], d[:, 2]
ax.plot_trisurf(x, y, tr, z, color=c, alpha=alpha)
# If points and lines needed plot them
if i % skip == 0 and points:
ax.scatter(
X[i, :][0],
X[i, :][1],
X[i, :][2],
s=s,
marker=".",
c=colors[t[i]],
)
if i % skip == 0 and lines:
a = [m[:, t[i]][0], X[i, :][0]]
b = [m[:, t[i]][1], X[i, :][1]]
c = [m[:, t[i]][2], X[i, :][2]]
ax.plot(a, b, c, linewidth=lw, c=colors[t[i]])
if With2D:
twoX = X[:, :-1]
for i in range(len(twoX)):
# Print % done
if i % (int(len(twoX) / 10)) == 0 and i != 0:
print("%d %% done" % (int((i * 100) / float(len(twoX)))))
# Check if it is the first time this lipase is plotted
if t[i] in taken:
# if not, no label and keep plotting
label = None
else:
# If so, add label and plot the mean center with label
if self.train:
# If so, add label and plot the mean center with label
label = self.converter[t[i]]
else:
label = "Input point"
taken.append(t[i])
ax.scatter(
m[:, t[i]][0],
m[:, t[i]][1],
zdir="z",
zs=-8,
s=1,
c=colors[t[i]],
label=label,
)
if i % skip == 0:
# If points and lines needed plot them
ax.scatter(
twoX[i, :][0],
twoX[i, :][1],
s=s,
marker=".",
c=colors[t[i]],
zdir="z",
zs=-8,
)
a = [m[:, t[i]][0], twoX[i, :][0]]
b = [m[:, t[i]][1], twoX[i, :][1]]
ax.plot(a, b, linewidth=lw, c=colors[t[i]], zdir="z", zs=-8)
# Make plot look sexy
if len(xlims) > 1:
ax.set_xlim(xlims[0], xlims[1])
if len(ylims) > 1:
ax.set_ylim(ylims[0], ylims[1])
if len(zlims) > 1:
ax.set_zlim(zlims[0], zlims[1])
ax.set_xlabel("1st axis")
ax.set_ylabel("2nd axis")
ax.set_zlabel("3rd axis")
ax.set_yscale(yscale)
plt.tight_layout()
if legend:
lgnd = ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
for i in range(len(lgnd.legendHandles)):
lgnd.legendHandles[i]._sizes = [20]
if savefig != "":
plt.savefig(savefig, dpi=700, bbox_inches="tight")
if type(animate) == type("hi"):
if legend:
lgnd = ax.legend(loc=2)
for i in range(len(lgnd.legendHandles)):
lgnd.legendHandles[i]._sizes = [20]
def animater(i):
if i % 50 == 0:
print(i)
ax.view_init(elev=10.0, azim=360 * i / frames)
anim = animation.FuncAnimation(
fig, animater, interval=1, blit=False, frames=frames, repeat=False
)
anim.save(animate + ".mp4", fps=30, extra_args=["-vcodec", "libx264"])
else:
if axis is None:
plt.show()
if n_components == 2:
return
if n_components == 3 and Get_mesh:
return triangles, dlist
def Train(
self,
verbose=True,
crossval=True,
algorithm="Fisher",
plot=True,
boundaryplot=False,
bins=500,
labelsL=None,
labelsR=None,
savefig="",
ret=False,
bounds=None,
f1=False,
):
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn import datasets, svm, metrics
import matplotlib.pyplot as plt
import numpy as np
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
import imp
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import balanced_accuracy_score
X, y = self.X, self.y
classnames = [self.to_string[0], self.to_string[1]]
if algorithm == "RBF":
from sklearn import svm
from sklearn.model_selection import GridSearchCV
parameters = {"C": [0.01, 1, 10], "gamma": [1e-1, 1, 1e1]}
svc = svm.SVC(kernel="rbf", probability=True)
clf = GridSearchCV(svc, parameters, cv=5, verbose=3, n_jobs=4)
clf.fit(X, y)
scores = clf.predict_proba(X)[:, 0]
if plot and len(self.unique) == 2:
fpr, tpr, thresholds = metrics.roc_curve(y, scores)
x0, y0, sy0 = histogram(
scores[y == 0], bins=bins, plot=False, remove0=True
)
x1, y1, sy1 = histogram(
scores[y == 1], bins=bins, plot=False, remove0=True
)
fig2, ax = plt.subplots(1, 2, figsize=(12, 8))
ax[0].errorbar(
x0, y0, yerr=sy0, color="b", label=classnames[0], capsize=2
)
ax[0].errorbar(
x1, y1, yerr=sy1, color="r", label=classnames[1], capsize=2
)
if labelsL is None:
ax[0].set(
xlabel="Predicted probability",
ylabel=f"frequency / {1/500:4.2E}",
title="Efficiency of the Decision Tree",
)
else:
ax[0].set(**labelsL)
ax[0].legend()
if ax is None:
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
ax[1].plot(
tpr,
fpr,
color="darkorange",
lw=3,
label="ROC curve (area = %0.2f)" % metrics.auc(tpr, fpr),
)
ax[1].plot([0, 1], [0, 1], color="navy", linewidth=3, linestyle="--")
if labelsR is None:
ax[1].set(
xlabel="True Positive Rate",
ylabel="False Positve Rate",
title=f"ROC for Decision Tree classifier (area = {metrics.auc(tpr,fpr):4.4f})",
)
else:
ax[1].set(**labels)
ax[1].legend()
if savefig != "":
plt.savefig(savefig + ".pdf", dpi=500)
plt.show()
if algorithm == "neighbors":
# parameters = {'n_neighbors':[5,10,30,40]}
# clf = GridSearchCV(KNeighborsClassifier(), parameters, cv=5)
clf = KNeighborsClassifier(n_neighbors=10, algorithm="kd_tree")
clf.fit(X, y)
print(clf.get_params)
if plot and len(self.unique) == 2:
fpr, tpr, thresholds = metrics.roc_curve(y, scores)
x0, y0, sy0 = histogram(
scores[y == 0], bins=bins, plot=False, remove0=True
)
x1, y1, sy1 = histogram(
scores[y == 1], bins=bins, plot=False, remove0=True
)
fig2, ax = plt.subplots(1, 2, figsize=(12, 8))
ax[0].errorbar(
x0, y0, yerr=sy0, color="b", label=classnames[0], capsize=2
)
ax[0].errorbar(
x1, y1, yerr=sy1, color="r", label=classnames[1], capsize=2
)
if labelsL is None:
ax[0].set(
xlabel="Predicted probability",