arr.ml1 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(* Wrap_utils.init () * let numpy = Py.import "numpy" * let builtins = Py.Module.builtins () * * let check o = * Wrap_utils.check_arr o * * module M = struct * type t = Py.Object.t * let show x = Py.Object.to_string x * let pp fmt x = Format.fprintf fmt "%s" (Py.Object.to_string x) * let of_pyobject x = x * let to_pyobject x = x * let of_bigarray ba = Numpy.of_bigarray ba * end * * include M * * let shape self = * match Py.Object.get_attr_string self "shape" with * | None -> raise (Wrap_utils.Attribute_not_found "shape") * | Some x -> Py.List.to_array_map Py.Int.to_int x * * let arange ?start ?step stop = * let stop = Py.Int.of_int stop in * let args = match start, step with * | None, None -> [| stop |] * | Some start, None -> [| Py.Int.of_int start; stop |] * | None, Some step -> [| Py.Int.of_int 0; stop; Py.Int.of_int step |] * | Some start, Some step -> [| Py.Int.of_int start; stop; Py.Int.of_int step |] * in * Py.Module.get_function numpy "arange" args * |> of_pyobject * * let reshape ~shape x = * Py.Object.call_method x "reshape" [| Py.List.of_array_map Py.Int.of_int shape |] |> of_pyobject * * module List = PyList.Make(M) * * module Float = struct * let matrix values = * Bigarray.Array2.of_array Bigarray.float64 Bigarray.c_layout values * |> Bigarray.genarray_of_array2 |> Numpy.of_bigarray * let vector values = * Bigarray.Array1.of_array Bigarray.float64 Bigarray.c_layout values * |> Bigarray.genarray_of_array1 |> Numpy.of_bigarray * let of_bigarray ba = Numpy.of_bigarray ba * * let to_bigarray x = Numpy.to_bigarray Bigarray.float64 Bigarray.c_layout x * * let matrices values = * List.of_list_map matrix values * let vectors values = * List.of_list_map vector values * end * * module Int = struct * let vector ia = * Bigarray.Array1.of_array Bigarray.nativeint Bigarray.c_layout (Array.map Nativeint.of_int ia) * |> Bigarray.genarray_of_array1 |> Numpy.of_bigarray * let matrix ia = * Bigarray.Array2.of_array Bigarray.nativeint Bigarray.c_layout (Array.map (Array.map Nativeint.of_int) ia) * |> Bigarray.genarray_of_array2 |> Numpy.of_bigarray * let of_bigarray ba = Numpy.of_bigarray ba * * let to_bigarray x = Numpy.to_bigarray Bigarray.nativeint Bigarray.c_layout x * * let matrices values = * List.of_list_map matrix values * let vectors values = * List.of_list_map vector values * end * * module Dtype = struct * type t = [`Object | `S of string] * let rec to_pyobject = function * | `S s -> Py.Module.get_function numpy "dtype" [|Py.String.of_string s|] * | `Object -> to_pyobject (`S "object") * let of_pyobject x = match Py.Object.to_string x with * | "object" -> `Object * | x -> `S x * end * * let numpy_array ?dtype a = * match dtype with * | None -> Py.Module.get_function numpy "array" [|a|] * | Some dtype -> * Py.Module.get_function_with_keywords numpy "array" [|a|] ["dtype", Dtype.to_pyobject dtype] * * module String = struct * let py_of_array a = Py.List.of_array_map Py.String.of_string a * * let of_list x = numpy_array @@ Py.List.of_list_map Py.String.of_string x * * let vector ia = * (\* XXX TODO figure out a way to do this with one copy less *\) * numpy_array @@ py_of_array ia * * let matrix aa = * numpy_array @@ Py.List.of_array_map py_of_array aa * * let vectors values = * List.of_list_map vector values * end * * module Object = struct * type elt = [`I of int | `F of float | `S of string | `Arr of t] * let py_of_elt x = match x with * | `I x -> Py.Int.of_int x * | `F x -> Py.Float.of_float x * | `S x -> Py.String.of_string x * | `Arr x -> of_pyobject x * let py_of_array a = Py.List.of_array_map py_of_elt a * let vector a = * numpy_array ~dtype:`Object @@ py_of_array a * let matrix aa = * numpy_array ~dtype:`Object @@ Py.List.of_array_map py_of_array aa * end * * let get_int ~i self = * match Py.Object.get_item self (Py.Tuple.of_list_map Py.Int.of_int i) with * | None -> raise (invalid_arg "Sklearn.Ndarray.get_int") * | Some x -> Py.Int.to_int x * * let get_float ~i self = * match Py.Object.get_item self (Py.Tuple.of_list_map Py.Int.of_int i) with * | None -> raise (invalid_arg "Sklearn.Ndarray.get_float") * | Some x -> Py.Float.to_float x * * let get_string ~i self = * match Py.Object.get_item self (Py.Tuple.of_list_map Py.Int.of_int i) with * | None -> raise (invalid_arg "Sklearn.Ndarray.get_float") * | Some x -> Py.String.to_string x * * let ones ?dtype shape = * match dtype with * | None -> Py.Module.get_function numpy "ones" [|Py.Tuple.of_list_map Py.Int.of_int shape|] * | Some dtype -> * Py.Module.get_function numpy "ones" * [|Py.Tuple.of_list_map Py.Int.of_int shape; Dtype.to_pyobject dtype|] * * let zeros ?dtype shape = * match dtype with * | None -> Py.Module.get_function numpy "zeros" [|Py.Tuple.of_list_map Py.Int.of_int shape|] * | Some dtype -> * Py.Module.get_function numpy "zeros" * [|Py.Tuple.of_list_map Py.Int.of_int shape; Dtype.to_pyobject dtype|] * * module Ops = struct * let operator = Py.import "operator" * * let int x = numpy_array (Py.Int.of_int x) * let float x = numpy_array (Py.Float.of_float x) * let bool x = numpy_array (Py.Bool.of_bool x) * let string x = numpy_array (Py.String.of_string x) * * let binop name a b = * of_pyobject @@ Py.Module.get_function operator name [|to_pyobject a; to_pyobject b|] * * let ( - ) = binop "sub" * let ( + ) = binop "add" * let ( * ) = binop "mul" * let ( / ) = binop "truediv" * * let ( < ) = binop "lt" * let ( <= ) = binop "le" * let ( > ) = binop "gt" * let ( >= ) = binop "get" * * let ( = ) = binop "eq" * let ( != ) = binop "ne" * end * * include Ops * * let ravel x = * Py.Module.get_function numpy "ravel" [|x|] * * let to_int_array x = * let x = ravel x in * let len = (shape x).(0) in * Array.init len (fun i -> get_int ~i:[i] x) * * let to_float_array x = * let x = ravel x in * let len = (shape x).(0) in * Array.init len (fun i -> get_float ~i:[i] x) * * let to_string_array x = * let x = ravel x in * let len = (shape x).(0) in * Array.init len (fun i -> get_string ~i:[i] x) * * let to_string_list x = * let x = ravel x in * let len = (shape x).(0) in * Stdlib.List.init len (fun i -> get_string ~i:[i] x) * * let slice ?i ?j ?step () = * `Slice (Wrap_utils.Slice.create_options ?i ?j ?step ()) * * let ellipsis = Wrap_utils.Option.get @@ Py.Object.get_attr_string builtins "Ellipsis" * * let get ~i self = * let index_of_tag = function * | `I i -> Py.Int.of_int i * | `Slice s -> Wrap_utils.Slice.to_pyobject s * | `Arr x -> to_pyobject x * | `Newaxis -> Py.none * | `Ellipsis -> ellipsis * in * match Py.Object.get_item self (Py.Tuple.of_list_map index_of_tag i) with * | None -> raise (invalid_arg "Sklearn.Ndarray.get_sub") * | Some x -> of_pyobject x * * let set ~i ~v self = * let index_of_tag = function * | `I i -> Py.Int.of_int i * | `Slice s -> Wrap_utils.Slice.to_pyobject s * | `Arr x -> to_pyobject x * | `Newaxis -> Py.none * | `Ellipsis -> ellipsis * in * Py.Object.set_item self (Py.Tuple.of_list_map index_of_tag i) (to_pyobject v) * * let min x = * Py.Module.get_function numpy "min" [|x|] |> Py.Float.to_float * * let max x = * Py.Module.get_function numpy "max" [|x|] |> Py.Float.to_float * * let argsort x = * Py.Module.get_function numpy "argsort" [|x|] |> of_pyobject * * let sort x = * Py.Module.get_function numpy "sort" [|x|] |> of_pyobject * * let of_pyobject x = * assert (check x); * x * * let of_csr_matrix x = of_pyobject @@ Csr_matrix.to_pyobject x * * let argmax ?axis ?out self = * Py.Module.get_function_with_keywords numpy "argmax" * [|self|] * (Wrap_utils.keyword_args [("axis", Wrap_utils.Option.map axis (function * | `Zero -> Py.Int.of_int 0 * | `One -> Py.Int.of_int 1 * | `PyObject x -> Wrap_utils.id x * )); ("out", out)]) * |> (fun x -> if check x then `Arr (of_pyobject x) else if Py.Int.check x then `I (Py.Int.to_int x) else failwith "could not identify type from Python value") * * let argmin ?axis ?out self = * Py.Module.get_function_with_keywords numpy "argmin" * [|self|] * (Wrap_utils.keyword_args [("axis", Wrap_utils.Option.map axis (function * | `Zero -> Py.Int.of_int 0 * | `One -> Py.Int.of_int 1 * | `PyObject x -> Wrap_utils.id x * )); ("out", out)]) * |> (fun x -> if check x then `Arr (of_pyobject x) else if Py.Int.check x then `I (Py.Int.to_int x) else failwith "could not identify type from Python value") * * let mean ?axis ?dtype ?out ?keepdims self = * Py.Module.get_function_with_keywords numpy "mean" * [|self|] * (Wrap_utils.keyword_args * [("axis", Wrap_utils.Option.map axis (function * | [x] -> Py.Int.of_int x * | x -> Py.Tuple.of_list_map Py.Int.of_int x)); * ("dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject); * ("out", Wrap_utils.Option.map out to_pyobject); * ("keepdims", Wrap_utils.Option.map keepdims Py.Bool.of_bool) * ]) * |> of_pyobject * * let sum ?axis ?dtype ?out self = * Py.Module.get_function_with_keywords numpy "sum" * [|self|] * (Wrap_utils.keyword_args [("axis", Wrap_utils.Option.map axis Py.Int.of_int); * ("dtype", dtype); * ("out", Wrap_utils.Option.map out to_pyobject)]) * |> of_pyobject * * let asarray ?dtype self = * Py.Module.get_function_with_keywords numpy "asarray" * [|self|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let asanyarray ?dtype self = * Py.Module.get_function_with_keywords numpy "asanyarray" * [|self|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let ascontiguousarray ?dtype self = * Py.Module.get_function_with_keywords numpy "ascontiguousarray" * [|self|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let asfarray ?dtype self = * Py.Module.get_function_with_keywords numpy "asfarray" * [|self|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let asarray_chkfinite ?dtype self = * Py.Module.get_function_with_keywords numpy "asarray_chkfinite" * [|self|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let toarray self = * match Py.Module.get_function_opt self "toarray" with * | None -> self * | Some f -> f [||] |> of_pyobject * * let todense self = * match Py.Module.get_function_opt self "todense" with * | None -> self * | Some f -> f [||] |> of_pyobject * * let vstack arrs = * Py.Module.get_function numpy "vstack" [|Py.Tuple.of_list_map to_pyobject arrs|] |> of_pyobject * * let hstack arrs = * Py.Module.get_function numpy "hstack" [|Py.Tuple.of_list_map to_pyobject arrs|] |> of_pyobject * * let dstack arrs = * Py.Module.get_function numpy "dstack" [|Py.Tuple.of_list_map to_pyobject arrs|] |> of_pyobject * * let full ?dtype ~shape fill_value = * Py.Module.get_function_with_keywords numpy "full" * [|Py.Tuple.of_list_map Py.Int.of_int shape; Object.py_of_elt fill_value|] * (Wrap_utils.keyword_args ["dtype", Wrap_utils.Option.map dtype Dtype.to_pyobject]) * |> of_pyobject * * let flatnonzero x = * Py.Module.get_function numpy "flatnonzero" [|to_pyobject x|] |> of_pyobject * * let flatten self = * Py.Module.get_function self "flatten" [||] |> of_pyobject * * let ravel x = * Py.Module.get_function numpy "ravel" [|to_pyobject x|] |> of_pyobject * * let exp x = * Py.Module.get_function numpy "exp" [|to_pyobject x|] |> of_pyobject * * let log x = * Py.Module.get_function numpy "log" [|to_pyobject x|] |> of_pyobject * * let iter x = * Py.Module.get_function builtins "iter" [|to_pyobject x|] |> Py.Iter.to_seq |> Seq.map of_pyobject * * module Random = struct * let numpy_random = Py.import "numpy.random" * let seed i = * let _ = Py.Module.get_function numpy_random "seed" [|Py.Int.of_int i|] in () * * let random_sample shape = * Py.Module.get_function numpy_random "random_sample" [|Py.Tuple.of_list_map Py.Int.of_int shape|] |> of_pyobject * end *)