From b602a0ce3526c0d5f5fd6c681d6a70101a492d89 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Fri, 16 May 2014 01:44:16 -0400 Subject: hw01 assignment and julia notes --- assignments/hw01/question2_to_8.jl | 87 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 assignments/hw01/question2_to_8.jl (limited to 'assignments/hw01/question2_to_8.jl') diff --git a/assignments/hw01/question2_to_8.jl b/assignments/hw01/question2_to_8.jl new file mode 100644 index 0000000..3cf9dc9 --- /dev/null +++ b/assignments/hw01/question2_to_8.jl @@ -0,0 +1,87 @@ + +using MAT + +all_vars= matread("sampledata2.mat") +d = all_vars["data"] + +# ----------------------- Q2 +length(filter(x -> x > 5, d[:, 2])) / length(d[:, 2]) +# 0.19, so first statement is false + +length(filter(x -> x > 50, d[:, 1])) +# 56x over 50 + +length(filter(x -> x < 20, d[:, 1])) +# 4x under 20 +# second statement is true + +length(filter(x -> x < 5, d[:, 2])) +# 243 +length(filter(x -> x > 6, d[:, 2])) +# 34 +# third statement is false + +length(filter(x -> x > 0.5, d[:, 3])) / length(d[:, 3]) +# 0.5233333333333333 +# last statement is false + +# ----------------------- Q3 + +using Winston + +# TODO: this foldl business is ugly +with_cancer = foldl(vcat, filter(x -> x[3] > 0.5, [d[row, :] for row = 1:size(d,1)])) +without_cancer = foldl(vcat, filter(x -> x[3] < 0.5, [d[row, :] for row = 1:size(d,1)])) + +figure() +p = FramedPlot() +add(p, Histogram(hist(with_cancer[:, 1], 15:10:75)..., color="red")) + +# 1 NO (this is "without") +# 2 yes +# 3 NO +# 4 NO + +# ----------------------- Q4 + +add(p, Histogram(hist(without_cancer[:, 1], 15:10:75)..., color="green")) +# 1 YES + +# ----------------------- Q5 + +function fractional(f, d) + return length(filter(f, d)) / length(d) +end + +fractional(x -> x>55, without_cancer[:, 1]) +#0.013986013986013986 + +fractional(x -> x<25, without_cancer[:, 1]) +#0.08391608391608392 +# FALSE + +fractional(x -> x<35, without_cancer[:, 1]) +# FALSE + +fractional(x -> x<30, with_cancer[:, 1]) / fractional(x -> x>60, with_cancer[:, 1]) +# TRUE! + +fractional(x -> x>45, with_cancer[:, 1]) +# FALSE + +# ----------------------- Q6 + +fractional(x -> x>35, with_cancer[:, 1]) +# 0.802547770700637 + +# ----------------------- Q7 + +fractional(x -> x<25, d[:, 1]) +# 0.06 + +# ----------------------- Q8 + +drinkers = foldl(vcat, filter(x -> x[2] > 3, [d[row, :] for row = 1:size(d,1)])) +fractional(x -> x >0.5, drinkers[:, 3]) +# 0.6459627329192547 + -- cgit v1.2.3