|
| 1 | +--- |
| 2 | +title: Preprocessing Real-World Data |
| 3 | +jupyter: julia-1.7 |
| 4 | +--- |
| 5 | + |
| 6 | +```{julia} |
| 7 | +using Pkg; Pkg.activate("dev") |
| 8 | +``` |
| 9 | + |
| 10 | +```{julia} |
| 11 | +include("dev/utils.jl") |
| 12 | +using AlgorithmicRecourseDynamics |
| 13 | +using CounterfactualExplanations, Flux, Plots, PlotThemes, Random, LaplaceRedux, LinearAlgebra |
| 14 | +theme(:wong) |
| 15 | +output_path = output_dir("real_world") |
| 16 | +www_path = www_dir("real_world") |
| 17 | +data_path = data_dir("real_world") |
| 18 | +``` |
| 19 | + |
| 20 | +## California Housing Data |
| 21 | + |
| 22 | +Fetching the data using Python's `sklearn`: |
| 23 | + |
| 24 | +```{python} |
| 25 | +from sklearn.datasets import fetch_california_housing |
| 26 | +df, y = fetch_california_housing(return_X_y=True, as_frame=True) |
| 27 | +df["target"] = y.values |
| 28 | +data_path = "../../artifacts/upload/data/real_world" |
| 29 | +import os |
| 30 | +df.to_csv(os.path.join(data_path,"raw/cal_housing.csv"), index=False) |
| 31 | +``` |
| 32 | + |
| 33 | +Loading the data into Julia session: |
| 34 | + |
| 35 | +```{julia} |
| 36 | +using CSV, DataFrames, Statistics, StatsBase |
| 37 | +df = CSV.read(joinpath(data_path, "raw/cal_housing.csv"), DataFrame) |
| 38 | +# Features: |
| 39 | +X = Matrix(df[:,Not(:target)]) |
| 40 | +dt = fit(ZScoreTransform, X, dims=1) |
| 41 | +StatsBase.transform!(dt, X) |
| 42 | +# Target: |
| 43 | +y = df.target |
| 44 | +y = Float64.(y .>= median(y)); # binary target |
| 45 | +# Data: |
| 46 | +df = DataFrame(X,:auto) |
| 47 | +df.target = y |
| 48 | +``` |
| 49 | + |
| 50 | +```{julia} |
| 51 | +using MLUtils: undersample |
| 52 | +# Make DataFrames.jl work |
| 53 | +MLUtils.getobs(data::DataFrame, i) = data[i,:] |
| 54 | +MLUtils.numobs(data::DataFrame) = nrow(data) |
| 55 | +df_balanced = getobs(undersample(df, df.target;shuffle=true)) |
| 56 | +``` |
| 57 | + |
| 58 | +```{julia} |
| 59 | +CSV.write(joinpath(data_path, "cal_housing.csv"), df) |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +## Give Me Some Credit |
| 64 | + |
| 65 | +```{julia} |
| 66 | +using CSV, DataFrames, Statistics, StatsBase |
| 67 | +df = CSV.read(joinpath(data_path, "raw/cs-training.csv"), DataFrame) |
| 68 | +select!(df, Not([:Column1])) |
| 69 | +rename!(df, :SeriousDlqin2yrs => :target) |
| 70 | +mapcols!(x -> [ifelse(x_=="NA", missing, x_) for x_ in x], df) |
| 71 | +dropmissing!(df) |
| 72 | +mapcols!(x -> eltype(x) <: AbstractString ? parse.(Int, x) : x, df) |
| 73 | +# Features: |
| 74 | +X = Matrix(df[:,Not(:target)]) |
| 75 | +dt = fit(ZScoreTransform, X, dims=1) |
| 76 | +StatsBase.transform!(dt, X) |
| 77 | +# Target: |
| 78 | +y = df.target |
| 79 | +# Data: |
| 80 | +df = DataFrame(X,:auto) |
| 81 | +df.target = y |
| 82 | +``` |
| 83 | + |
| 84 | +```{julia} |
| 85 | +using MLUtils |
| 86 | +using MLUtils: undersample |
| 87 | +# Make DataFrames.jl work |
| 88 | +MLUtils.getobs(data::DataFrame, i) = data[i,:] |
| 89 | +MLUtils.numobs(data::DataFrame) = nrow(data) |
| 90 | +df_balanced = getobs(undersample(df, df.target;shuffle=true)) |
| 91 | +``` |
| 92 | + |
| 93 | +```{julia} |
| 94 | +CSV.write(joinpath(data_path, "gmsc.csv"), df_balanced) |
| 95 | +``` |
| 96 | + |
| 97 | +## UCI Credit Card Default |
| 98 | + |
| 99 | +```{julia} |
| 100 | +using CSV, DataFrames, Statistics, StatsBase |
| 101 | +df = CSV.read(joinpath(data_path, "raw/UCI_Credit_Card.csv"), DataFrame) |
| 102 | +select!(df, Not([:ID, :SEX, :EDUCATION, :MARRIAGE])) |
| 103 | +rename!(df, "default.payment.next.month" => :target) |
| 104 | +dropmissing!(df) |
| 105 | +mapcols!(x -> eltype(x) <: AbstractString ? parse.(Int, x) : x, df) |
| 106 | +# Features: |
| 107 | +X = Matrix(df[:,Not(:target)]) |
| 108 | +dt = fit(ZScoreTransform, X, dims=1) |
| 109 | +StatsBase.transform!(dt, X) |
| 110 | +# Target: |
| 111 | +y = df.target |
| 112 | +# Data: |
| 113 | +df = DataFrame(X,:auto) |
| 114 | +df.target = y |
| 115 | +``` |
| 116 | + |
| 117 | +```{julia} |
| 118 | +using MLUtils |
| 119 | +using MLUtils: undersample |
| 120 | +# Make DataFrames.jl work |
| 121 | +MLUtils.getobs(data::DataFrame, i) = data[i,:] |
| 122 | +MLUtils.numobs(data::DataFrame) = nrow(data) |
| 123 | +df_balanced = getobs(undersample(df, df.target;shuffle=true)) |
| 124 | +``` |
| 125 | + |
| 126 | +```{julia} |
| 127 | +CSV.write(joinpath(data_path, "credit_default.csv"), df_balanced) |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments