summaryrefslogtreecommitdiff
path: root/site/docs/1.0.1/api/python/pyspark.mllib.util.MLUtils-class.html
blob: ad69ae886289cdaf52076c29dd8fa117124884b3 (plain) (blame)
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
<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>pyspark.mllib.util.MLUtils</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="pyspark-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://spark.apache.org">Spark 1.0.0 Python API Docs</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="pyspark-module.html">Package&nbsp;pyspark</a> ::
        <a href="pyspark.mllib-module.html">Package&nbsp;mllib</a> ::
        <a href="pyspark.mllib.util-module.html">Module&nbsp;util</a> ::
        Class&nbsp;MLUtils
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
        <tr><td align="right"><span class="options"
            >[<a href="frames.html" target="_top">frames</a
            >]&nbsp;|&nbsp;<a href="pyspark.mllib.util.MLUtils-class.html"
            target="_top">no&nbsp;frames</a>]</span></td></tr>
      </table>
    </td>
  </tr>
</table>
<!-- ==================== CLASS DESCRIPTION ==================== -->
<h1 class="epydoc">Class MLUtils</h1><p class="nomargin-top"><span class="codelink"><a href="pyspark.mllib.util-pysrc.html#MLUtils">source&nbsp;code</a></span></p>
<p>Helper methods to load, save and pre-process data used in MLlib.</p>

<!-- ==================== STATIC METHODS ==================== -->
<a name="section-StaticMethods"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Static Methods</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="pyspark.mllib.util.MLUtils-class.html#loadLibSVMFile" class="summary-sig-name">loadLibSVMFile</a>(<span class="summary-sig-arg">sc</span>,
        <span class="summary-sig-arg">path</span>,
        <span class="summary-sig-arg">multiclass</span>=<span class="summary-sig-default">False</span>,
        <span class="summary-sig-arg">numFeatures</span>=<span class="summary-sig-default">-1</span>,
        <span class="summary-sig-arg">minPartitions</span>=<span class="summary-sig-default">None</span>)</span><br />
      Loads labeled data in the LIBSVM format into an RDD of LabeledPoint.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="pyspark.mllib.util-pysrc.html#MLUtils.loadLibSVMFile">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="pyspark.mllib.util.MLUtils-class.html#saveAsLibSVMFile" class="summary-sig-name">saveAsLibSVMFile</a>(<span class="summary-sig-arg">data</span>,
        <span class="summary-sig-arg">dir</span>)</span><br />
      Save labeled data in LIBSVM format.</td>
          <td align="right" valign="top">
            <span class="codelink"><a href="pyspark.mllib.util-pysrc.html#MLUtils.saveAsLibSVMFile">source&nbsp;code</a></span>
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<!-- ==================== METHOD DETAILS ==================== -->
<a name="section-MethodDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Method Details</span></td>
</tr>
</table>
<a name="loadLibSVMFile"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">loadLibSVMFile</span>(<span class="sig-arg">sc</span>,
        <span class="sig-arg">path</span>,
        <span class="sig-arg">multiclass</span>=<span class="sig-default">False</span>,
        <span class="sig-arg">numFeatures</span>=<span class="sig-default">-1</span>,
        <span class="sig-arg">minPartitions</span>=<span class="sig-default">None</span>)</span>
    <br /><em class="fname">Static Method</em>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="pyspark.mllib.util-pysrc.html#MLUtils.loadLibSVMFile">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Loads labeled data in the LIBSVM format into an RDD of LabeledPoint. 
  The LIBSVM format is a text-based format used by LIBSVM and LIBLINEAR. 
  Each line represents a labeled sparse feature vector using the following 
  format:</p>
  <p>label index1:value1 index2:value2 ...</p>
  <p>where the indices are one-based and in ascending order. This method 
  parses each line into a LabeledPoint, where the feature indices are 
  converted to zero-based.</p>
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>sc</code></strong> - Spark context</li>
        <li><strong class="pname"><code>path</code></strong> - file or directory path in any Hadoop-supported file system URI</li>
        <li><strong class="pname"><code>multiclass</code></strong> - whether the input labels contain more than two classes. If false,
          any label with value greater than 0.5 will be mapped to 1.0, or 
          0.0 otherwise. So it works for both +1/-1 and 1/0 cases. If true,
          the double value parsed directly from the label string will be 
          used as the label value.</li>
        <li><strong class="pname"><code>numFeatures</code></strong> - number of features, which will be determined from the input data 
          if a nonpositive value is given. This is useful when the dataset 
          is already split into multiple files and you want to load them 
          separately, because some features may not present in certain 
          files, which leads to inconsistent feature dimensions.</li>
        <li><strong class="pname"><code>minPartitions</code></strong> - min number of partitions</li>
    </ul></dd>
    <dt>Returns:</dt>
        <dd>labeled data stored as an RDD of LabeledPoint
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> tempfile <span class="py-keyword">import</span> NamedTemporaryFile
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> pyspark.mllib.util <span class="py-keyword">import</span> MLUtils
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile = NamedTemporaryFile(delete=True)
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile.write(<span class="py-string">&quot;+1 1:1.0 3:2.0 5:3.0\n-1\n-1 2:4.0 4:5.0 6:6.0&quot;</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile.flush()
<span class="py-prompt">&gt;&gt;&gt; </span>examples = MLUtils.loadLibSVMFile(sc, tempFile.name).collect()
<span class="py-prompt">&gt;&gt;&gt; </span>multiclass_examples = MLUtils.loadLibSVMFile(sc, tempFile.name, True).collect()
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile.close()
<span class="py-prompt">&gt;&gt;&gt; </span>examples[0].label
<span class="py-output">1.0</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>examples[0].features.size
<span class="py-output">6</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> examples[0].features
<span class="py-output">[0: 1.0, 2: 2.0, 4: 3.0]</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>examples[1].label
<span class="py-output">0.0</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>examples[1].features.size
<span class="py-output">6</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> examples[1].features
<span class="py-output">[]</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>examples[2].label
<span class="py-output">0.0</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>examples[2].features.size
<span class="py-output">6</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> examples[2].features
<span class="py-output">[1: 4.0, 3: 5.0, 5: 6.0]</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span>multiclass_examples[1].label
<span class="py-output">-1.0</span></pre></dd>
  </dl>
</td></tr></table>
</div>
<a name="saveAsLibSVMFile"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">saveAsLibSVMFile</span>(<span class="sig-arg">data</span>,
        <span class="sig-arg">dir</span>)</span>
    <br /><em class="fname">Static Method</em>
  </h3>
  </td><td align="right" valign="top"
    ><span class="codelink"><a href="pyspark.mllib.util-pysrc.html#MLUtils.saveAsLibSVMFile">source&nbsp;code</a></span>&nbsp;
    </td>
  </tr></table>
  
  <p>Save labeled data in LIBSVM format.</p>
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>data</code></strong> - an RDD of LabeledPoint to be saved</li>
        <li><strong class="pname"><code>dir</code></strong> - directory to save the data
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> tempfile <span class="py-keyword">import</span> NamedTemporaryFile
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> fileinput <span class="py-keyword">import</span> input
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> glob <span class="py-keyword">import</span> glob
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> pyspark.mllib.util <span class="py-keyword">import</span> MLUtils
<span class="py-prompt">&gt;&gt;&gt; </span>examples = [LabeledPoint(1.1, Vectors.sparse(3, [(0, 1.23), (2, 4.56)])),                         LabeledPoint(0.0, Vectors.dense([1.01, 2.02, 3.03]))]
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile = NamedTemporaryFile(delete=True)
<span class="py-prompt">&gt;&gt;&gt; </span>tempFile.close()
<span class="py-prompt">&gt;&gt;&gt; </span>MLUtils.saveAsLibSVMFile(sc.parallelize(examples), tempFile.name)
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-string">''</span>.join(sorted(input(glob(tempFile.name + <span class="py-string">&quot;/part-0000*&quot;</span>))))
<span class="py-output">'0.0 1:1.01 2:2.02 3:3.03\n1.1 1:1.23 3:4.56\n'</span></pre></li>
    </ul></dd>
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="pyspark-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="http://spark.apache.org">Spark 1.0.0 Python API Docs</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Fri Jul  4 18:52:26 2014
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>