(ns code.privacy.swap_v1 (:use (code.utils utils preprocess)) (:use (code.pls cliff ncliff)) (:use (incanter core stats))) (defn private-swap-attributes [num1 D s] "selects random attribute value for replacement s = sensitive attributes and class (not to be swapped) [10 20] num1 = percent to be swapped Example: (private-swap-attributes 0.1 (pre-data ant13) [10 20])" (let [independent (sel D :cols (range (dec (ncol D)))) indx (range (ncol independent)) klass (sel D :cols (- (ncol D) 1)) change-x (fn [x one] ;x is value to be changed (let [notx0 (filter #(not= x %) (to-vect one)) notx (if (empty? notx0) x (first (shuffle notx0)))] notx)) swap-one (fn [one two] ;column (if (member? two s) one (let [idx (range (count one)) nump (Math/ceil (* num1 (count one))) get-idx (sample idx :size nump :replacement false)] (loop [i 0 result []] (if (= i (count one)) result (recur (inc i) (conj result (if (member? i get-idx) (change-x (nth one i) one) (nth one i)))))))))] (bind-columns (trans (map #(swap-one %1 %2) (trans independent) indx)) klass))) (defn swap10 [_ D s _ _ _ _] (private-swap-attributes 0.1 D s)) (defn swap20 [_ D s _ _ _ _] (private-swap-attributes 0.2 D s)) (defn swap40 [_ D s _ _ _ _] (private-swap-attributes 0.4 D s))