"🔒"
; We will be using several builtin libraries
(require '[clojure.java.io :as io])
(require '[clojure.string :as str])
(require '[clojure.pprint :refer [pprint]])
(require '[clojure.edn :as edn])
(load-file "my.clj")
(def CSV-FILE "my_BankerChurners.csv")
#result
##'user/CSV-FILE
"✍️"
; @workUnit
(def lines (line-seq (io/reader CSV-FILE)))
#result
##'user/lines
"🔒"
; @check
; @title: first three lines
(println "Total lines:" (count lines))
(show (take-last 3 lines))
#result
#Total lines: 10128
#("716506083,\"Attrited Customer\",44,\"F\",1,\"High School\",\"Married\",\"Less than $40K\",\"Blue\",36,5,3,4,5409,0,5409,0.819,10291,60,0.818,0,0.99788,0.00211827"
# "717406983,\"Attrited Customer\",30,\"M\",2,\"Graduate\",\"Unknown\",\"$40K - $60K\",\"Blue\",36,4,3,3,5281,0,5281,0.535,8395,62,0.722,0,0.99671,0.00329379"
# "714337233,\"Attrited Customer\",43,\"F\",2,\"Graduate\",\"Married\",\"Less than $40K\",\"Silver\",25,6,2,4,10388,1961,8427,0.703,10294,61,0.649,0.189,0.99662,0.00337654")
#nil
"✍️"
; @workUnit
(defn parse-value [x]
(edn/read-string x)
)
#result
##'user/parse-value
"🔒"
; @check
; @title: parse-value int
{"100" (parse-value "100"),
"3.1415" (parse-value "3.1415")
"10e3" (parse-value "10e3")
"hello" (parse-value "\"hello\"")}
#result
#{"100" 100, "3.1415" 3.1415, "10e3" 10000.0, "hello" "hello"}
"✍️"
; @workUnit
(defn parse-line [line]
(mapv parse-value (str/split (str/replace line #"[\\]" "") #","))
)
;(defn parse-line [line]
; (str/split (str/replace line #"i" "") #",")
; )
;(println (parse-line "HELLO"))
#result
##'user/parse-line
"🔒"
; @check
; @title: parse first line
(->> lines
(first)
(parse-line)
(show))
#result
#["CLIENTNUM"
# "Attrition_Flag"
# "Customer_Age"
# "Gender"
# "Dependent_count"
# "Education_Level"
# "Marital_Status"
# "Income_Category"
# "Card_Category"
# "Months_on_book"
# "Total_Relationship_Count"
# "Months_Inactive_12_mon"
# "Contacts_Count_12_mon"
# "Credit_Limit"
# "Total_Revolving_Bal"
# "Avg_Open_To_Buy"
# "Total_Amt_Chng_Q4_Q1"
# "Total_Trans_Amt"
# "Total_Trans_Ct"
# "Total_Ct_Chng_Q4_Q1"
# "Avg_Utilization_Ratio"
# "Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_1"
# "Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_2"]
#nil
"🔒"
; @check
; @title: parse last line
(->> lines
(last)
(parse-line)
(show))
#result
#[714337233
# "Attrited Customer"
# 43
# "F"
# 2
# "Graduate"
# "Married"
# "Less than $40K"
# "Silver"
# 25
# 6
# 2
# 4
# 10388
# 1961
# 8427
# 0.703
# 10294
# 61
# 0.649
# 0.189
# 0.99662
# 0.00337654]
#nil
"🔒"
(def columns [:id :attrition :age :gender :dependents :education :marital :income :card :months
:rel-count :inactive :contacts :limit :balance :open :change :amount :count :change2 :ratio])
#result
##'user/columns
"✍️"
; @workUnit
(defn make-record [row]
(zipmap columns row)
)
(println (make-record (parse-line (last lines))))
#result
#{:inactive 2, :amount 10294, :open 8427, :rel-count 6, :age 43, :marital Married, :dependents 2, :limit 10388, :contacts 4, :months 25, :change2 0.649, :attrition Attrited Customer, :card Silver, :balance 1961, :ratio 0.189, :id 714337233, :count 61, :education Graduate, :gender F, :change 0.703, :income Less than $40K}
#nil
"🔒"
; @check
; @title: make-record of last line
(show (make-record (-> lines last parse-line)))
#result
#{:age 43,
# :amount 10294,
# :attrition "Attrited Customer",
# :balance 1961,
# :card "Silver",
# :change 0.703,
# :change2 0.649,
# :contacts 4,
# :count 61,
# :dependents 2,
# :education "Graduate",
# :gender "F",
# :id 714337233,
# :inactive 2,
# :income "Less than $40K",
# :limit 10388,
# :marital "Married",
# :months 25,
# :open 8427,
# :ratio 0.189,
# :rel-count 6}
#nil
"✍️"
; @workUnit
(def result
(conj {} (select-keys
(zipmap ["M" "F"]
(map
(fn [[grp valu]]
{:count (count (map :count valu))
:limit (int (Math/round (reduce + (map :limit valu))))
:mean (int (Math/round (/ (reduce + (map :limit valu))
(count (map :count valu)))))})
(group-by :gender (map make-record (rest (map parse-line lines))))
)
)
["F" "M"]))
)
#result
##'user/result
"🔒"
; @check
; @grade: 5
; @title: show data analysis result
(show result)
#result
#{"F" {:count 5358, :limit 26917811, :mean 5024},
# "M" {:count 4769, :limit 60497984, :mean 12686}}
#nil
To embed this program on your website, copy the following code and paste it into your website's HTML: