/usr/share/pyshared/mlpy/_data.py is in python-mlpy 2.2.0~dfsg1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ## This file is part of MLPY.
## Input data module.
## This code is written by Davide Albanese, <albanese@fbk.eu>.
## (C) 2008 Fondazione Bruno Kessler - Via Santa Croce 77, 38100 Trento, ITALY.
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
__all__ = ["data_fromfile", "data_fromfile_wl", "data_tofile", "data_tofile_wl", "data_normalize", "data_standardize", "standardize", "center", "standardize_from", "center_from"]
from numpy import *
import csv
import warnings
def deprecation(message):
warnings.warn(message, DeprecationWarning)
def data_fromfile(file, ytype=int):
"""
Read data file in the form::
x11 [TAB] x12 [TAB] ... x1n [TAB] y1
x21 [TAB] x22 [TAB] ... x2n [TAB] y2
. . . . .
. . . . .
. . . . .
xm1 [TAB] xm2 [TAB] ... xmn [TAB] ym
where xij are float and yi are of type 'ytype'
(numpy.int or numpy.float).
Input
* *file* - data file name
* *ytype* - numpy datatype for labels (numpy.int
or numpy.float)
Output
* *x* - data [2D numpy array float]
* *y* - classes [1D numpy array int or float]
Example:
>>> from numpy import *
>>> from mlpy import *
>>> x, y = data_fromfile('data_example.dat')
>>> x
array([[ 1.1, 2. , 5.3, 3.1],
... [ 3.7, 1.4, 2.3, 4.5],
... [ 1.4, 5.4, 3.1, 1.4]])
>>> y
array([ 1, -1, 1])
"""
f = open(file)
firstline = f.readline()
cols = len(firstline.split("\t"))
f.close()
try:
data = fromfile(file = file, sep = "\t")
data = data.reshape((-1, cols))
except ValueError:
raise ValueError("'%s' is not a valid data file" % file)
x = delete(data, -1, 1)
y = data[:, -1].astype(ytype)
return (x, y)
def data_fromfile_wl(file):
"""
Read data file in the form::
x11 [TAB] x12 [TAB] ... x1n [TAB]
x21 [TAB] x22 [TAB] ... x2n [TAB]
. . . .
. . . .
. . . .
xm1 [TAB] xm2 [TAB] ... xmn [TAB]
where xij are float.
Input
* *file* - data file name
Output
* *x* - data [2D numpy array float]
Example:
>>> from numpy import *
>>> from mlpy import *
>>> x, y = data_fromfile('data_example.dat')
>>> x
array([[ 1.1, 2. , 5.3, 3.1],
... [ 3.7, 1.4, 2.3, 4.5],
... [ 1.4, 5.4, 3.1, 1.4]])
"""
f = open(file)
firstline = f.readline()
cols = len(firstline.split("\t"))
f.close()
try:
data = fromfile(file = file, sep = "\t")
data = data.reshape((-1, cols))
except ValueError:
raise ValueError("'%s' is not a valid data file" % file)
return data
def data_tofile(file, x, y, sep="\t"):
"""
Write data file in the form::
x11 [sep] x12 [sep] ... x1n [sep] y1
x21 [sep] x22 [sep] ... x2n [sep] y2
. . . . .
. . . . .
. . . . .
xm1 [sep] xm2 [sep] ... xmn [sep] ym
where xij are float and yi are integer.
Input
* *file* - data file name
* *x* - data [2D numpy array float]
* *y* - classes [1D numpy array integer]
* *sep* - separator
"""
writer = csv.writer(open(file, "wb"), delimiter = sep, lineterminator = '\n')
writer.writerows(append(x, y.reshape(-1, 1), axis = 1))
def data_tofile_wl(file, x, sep="\t"):
"""
Write data file in the form::
x11 [sep] x12 [sep] ... x1n [sep]
x21 [sep] x22 [sep] ... x2n [sep]
. . . .
. . . .
. . . .
xm1 [sep] xm2 [sep] ... xmn [sep]
where xij are float.
Input
* *file* - data file name
* *x* - data [2D numpy array float]
* *sep* - separator
"""
writer = csv.writer(open(file, "wb"), delimiter = sep, lineterminator = '\n')
writer.writerows(x)
def data_normalize(x):
"""
Normalize numpy array (2D) x.
Input
* *x* - data [2D numpy array float]
Output
* normalized data
Example:
>>> from numpy import *
>>> from mlpy import *
>>> x = array([[ 1.1, 2. , 5.3, 3.1],
... [ 3.7, 1.4, 2.3, 4.5],
... [ 1.4, 5.4, 3.1, 1.4]])
>>> data_normalize(x)
array([[-0.9797065 , -0.48295391, 1.33847226, 0.12418815],
... [ 0.52197912, -1.13395464, -0.48598056, 1.09795608],
... [-0.75217354, 1.35919078, 0.1451563 , -0.75217354]])
"""
deprecation("deprecated in mlpy 2.3")
#raise DeprecationWarning("Deprecated in version 2.1.0")
ret_x = empty_like(x)
mean_x = x.mean(axis=1)
std_x = x.std(axis=1) * sqrt(x.shape[1] / (x.shape[1] - 1.0))
for i in range(x.shape[0]):
ret_x[i, :] = (x[i, :] - mean_x[i]) / std_x[i]
return ret_x
def data_standardize(x, p = None):
"""
Standardize numpy array (2D) x and optionally
standardize p using mean and std of x.
Input
* *x* - data [2D numpy array float]
* *p* - optional data [2D numpy array float]
Output
* standardized data
Example:
>>> from numpy import *
>>> from mlpy import *
>>> x = array([[ 1.1, 2. , 5.3, 3.1],
... [ 3.7, 1.4, 2.3, 4.5],
... [ 1.4, 5.4, 3.1, 1.4]])
>>> data_standardize(x)
array([[-0.67958381, -0.43266792, 1.1157668 , 0.06441566],
... [ 1.1482623 , -0.71081158, -0.81536804, 0.96623494],
... [-0.46867849, 1.1434795 , -0.30039875, -1.0306506 ]])
"""
deprecation("deprecated in mlpy 2.3. Use mlpy.standardize() and "
"mlpy.standardize_from() instead")
ret_x = empty_like(x)
mean_x = x.mean(axis=0)
std_x = x.std(axis=0) * sqrt(x.shape[0] / (x.shape[0] - 1.0))
for i in range(x.shape[1]):
ret_x[:, i] = (x[:, i] - mean_x[i]) / std_x[i]
if not p == None:
ret_p = empty_like(p)
for i in range(p.shape[1]):
ret_p[:, i] = (p[:, i] - mean_x[i]) / std_x[i]
if p == None:
return ret_x
else:
return (ret_x, ret_p)
def standardize(x):
""" Standardize x.
x is standardized to have mean 0 and unit length by columns.
Return standardized x, the mean and the standard deviation.
"""
m = x.mean(axis=0)
s = x.std(axis=0)
return (x - m) / (s * np.sqrt(x.shape[0])), m, s
def center(y):
""" Center y to have mean 0.
Return centered y.
"""
m = np.mean(y)
return y - m, m
def standardize_from(x, mean, std):
"""Standardize x using external mean and standard deviation.
Return standardized x.
"""
return (x - mean) / (std * np.sqrt(x.shape[0]))
def center_from(y, mean):
"""Center y using external mean.
Return centered y.
"""
return y - mean
|