2
2
import matplotlib .pyplot as plt
3
3
from .plot_helpers import cm2 , cm3 , discrete_scatter
4
4
5
- def _call_classifier_chunked (classifier_pred_or_decide , X ):
6
- # The chunk_size is used to chunk the large arrays to work with x86
7
- # memory models that are restricted to < 2 GB in memory allocation. The
8
- # chunk_size value used here is based on a measurement with the
9
- # MLPClassifier using the following parameters:
10
- # MLPClassifier(solver='lbfgs', random_state=0,
11
- # hidden_layer_sizes=[1000,1000,1000])
12
- # by reducing the value it is possible to trade in time for memory.
13
- # It is possible to chunk the array as the calculations are independent of
14
- # each other.
15
- # Note: an intermittent version made a distinction between
16
- # 32- and 64 bit architectures avoiding the chunking. Testing revealed
17
- # that even on 64 bit architectures the chunking increases the
18
- # performance by a factor of 3-5, largely due to the avoidance of memory
19
- # swapping.
20
- chunk_size = 10000
21
-
22
- # We use a list to collect all result chunks
23
- Y_result_chunks = []
24
-
25
- # Call the classifier in chunks.
26
- for x_chunk in np .array_split (X , np .arange (chunk_size , X .shape [0 ],
27
- chunk_size , dtype = np .int32 ),
28
- axis = 0 ):
29
- Y_result_chunks .append (classifier_pred_or_decide (x_chunk ))
30
-
31
- return np .concatenate (Y_result_chunks )
32
-
33
5
34
6
def plot_2d_classification (classifier , X , fill = False , ax = None , eps = None ,
35
7
alpha = 1 , cm = cm3 ):
@@ -110,16 +82,14 @@ def plot_2d_separator(classifier, X, fill=False, ax=None, eps=None, alpha=1,
110
82
111
83
X1 , X2 = np .meshgrid (xx , yy )
112
84
X_grid = np .c_ [X1 .ravel (), X2 .ravel ()]
113
- if hasattr (classifier , "decision_function" ):
114
- decision_values = _call_classifier_chunked (classifier .decision_function ,
115
- X_grid )
85
+ try :
86
+ decision_values = classifier .decision_function (X_grid )
116
87
levels = [0 ] if threshold is None else [threshold ]
117
88
fill_levels = [decision_values .min ()] + levels + [
118
89
decision_values .max ()]
119
- else :
90
+ except AttributeError :
120
91
# no decision_function
121
- decision_values = _call_classifier_chunked (classifier .predict_proba ,
122
- X_grid )[:, 1 ]
92
+ decision_values = classifier .predict_proba (X_grid )[:, 1 ]
123
93
levels = [.5 ] if threshold is None else [threshold ]
124
94
fill_levels = [0 ] + levels + [1 ]
125
95
if fill :
@@ -133,14 +103,4 @@ def plot_2d_separator(classifier, X, fill=False, ax=None, eps=None, alpha=1,
133
103
ax .set_xlim (x_min , x_max )
134
104
ax .set_ylim (y_min , y_max )
135
105
ax .set_xticks (())
136
- ax .set_yticks (())
137
-
138
-
139
- if __name__ == '__main__' :
140
- from sklearn .datasets import make_blobs
141
- from sklearn .linear_model import LogisticRegression
142
- X , y = make_blobs (centers = 2 , random_state = 42 )
143
- clf = LogisticRegression (solver = 'lbfgs' ).fit (X , y )
144
- plot_2d_separator (clf , X , fill = True )
145
- discrete_scatter (X [:, 0 ], X [:, 1 ], y )
146
- plt .show ()
106
+ ax .set_yticks (())
0 commit comments