update codes to make them correct with kwargs

This commit is contained in:
Bogumił Kamiński 2022-01-17 11:20:58 +01:00
parent 8b39fdb427
commit da4ada6979
4 changed files with 81 additions and 15 deletions

76
appB.jl
View File

@ -2,6 +2,11 @@
# Codes for appendix B
# the solutions for exercises from a given chapter assume that
# there are packages loaded, variables and functions defined in the user's
# Julia session in a state that reflects the point of computations
# at the position of the chapter where a given exercise is formulated
# Code for exercise 3.1
using Statistics
@ -57,10 +62,71 @@ end
test_dice()
# Code for exercise 3.3
plot(scatter(data.set1.x, data.set1.y, legend=false),
scatter(data.set2.x, data.set2.y, legend=false),
scatter(data.set3.x, data.set3.y, legend=false),
scatter(data.set4.x, data.set4.y, legend=false))
plot(scatter(data.set1.x, data.set1.y; legend=false),
scatter(data.set2.x, data.set2.y; legend=false),
scatter(data.set3.x, data.set3.y; legend=false),
scatter(data.set4.x, data.set4.y; legend=false))
# Code for exercise 3.4
parse.(Int, ["1", "2", "3"])
# Code for exercise 4.1
years_table = freqtable(years)
plot(names(years_table, 1), years_table; legend=false,
xlabel="year", ylabel="# of movies")
# Code for exercise 4.2
s3 = Symbol.(s1)
@benchmark sort($s3)
@benchmark unique($s1)
@benchmark unique($s2)
@benchmark unique($s3)
# Code for exercise 5.1
v = ["1", "2", missing, "4"]
[ismissing(x) ? missing : parse(Int, x) for x in v]
map(v) do x
if ismissing(x)
return missing
else
return parse(Int, x)
end
end
using Missings
passmissing(parse).(Int, v)
# Code for exercise 5.2
using Dates
Date(2021, 1, 1):Month(1):Date(2021, 12, 1)
collect(Date(2021, 1, 1):Month(1):Date(2021, 12, 1))
# Code for exercise 6.1
using BenchmarkTools
@benchmark $puzzles."Rating"
# Code for exercise 6.2
using StatsBase
summarystats(puzzles[puzzles.Popularity .== 100, "NbPlays"])
summarystats(puzzles[puzzles.Popularity .== -100, "NbPlays"])
# Code for exercise 6.3
sum(length, values(rating_mapping))
nrow(good)
# Code for exercise 7.1
using BenchmarkTools
x = rand(10^6);
@btime DataFrame(x=$x);
@btime DataFrame(x=$x; copycols=false);

View File

@ -120,13 +120,13 @@ sort!(table)
years = [record.year for record in records]
has_drama = ["Drama" in record.genres for record in records]
drama_prop = proptable(years, has_drama, margins=1)
drama_prop = proptable(years, has_drama; margins=1)
# Code for listing 4.5
using Plots
plot(names(drama_prop, 1), drama_prop[:, 2], legend=false,
plot(names(drama_prop, 1), drama_prop[:, 2]; legend=false,
xlabel="year", ylabel="Drama probability")
# Code for section 4.6.1

View File

@ -188,7 +188,7 @@ std(skipmissing(rates))
# Code for listing 5.8
using FreqTables
proptable(dayname.(dates), ismissing.(rates), margins=1)
proptable(dayname.(dates), ismissing.(rates); margins=1)
# Code showing how to specify a complex condition using broadcasting
@ -201,11 +201,11 @@ dates[dayname.(dates) .== "Thursday" .&& ismissing.(rates)]
# Codes for plotting exchange rate data
using Plots
plot(dates, rates, xlabel="day", ylabel="PLN/USD", legend=false)
plot(dates, rates; xlabel="day", ylabel="PLN/USD", legend=false)
rates_ok = .!ismissing.(rates)
plot(dates[rates_ok], rates[rates_ok],
plot(dates[rates_ok], rates[rates_ok];
xlabel="day", ylabel="PLN/USD", legend=false)
using Impute

10
ch06.jl
View File

@ -92,12 +92,13 @@ puzzles[!, :Rating]
puzzles[!, 4]
puzzles[!, col]
using Plots
plot(histogram(puzzles.Rating, label="Rating"),
histogram(puzzles.RatingDeviation, label="RatingDeviation"),
histogram(puzzles.Popularity, label="Popularity"),
histogram(puzzles.NbPlays, label="NbPlays"))
plot([histogram(puzzles[!, col], label=col) for
plot([histogram(puzzles[!, col]; label=col) for
col in ["Rating", "RatingDeviation",
"Popularity", "NbPlays"]]...)
@ -125,8 +126,8 @@ good = puzzles[row_selector, ["Rating", "Popularity"]]
# Code for plotting histograms
plot(histogram(good.Rating, label="Rating"),
histogram(good.Popularity, label="Popularity"))
plot(histogram(good.Rating; label="Rating"),
histogram(good.Popularity; label="Popularity"))
# Code for column selectors
@ -236,7 +237,6 @@ mean_popularities = map(ratings) do rating
return mean(popularities)
end
using Plots
scatter(ratings, mean_popularities;
xlabel="rating", ylabel="mean popularity", legend=false)
@ -245,4 +245,4 @@ model = Loess.loess(ratings, mean_popularities);
ratings_predict = float.(sort(ratings))
popularity_predict = Loess.predict(model, ratings_predict)
plot!(ratings_predict, popularity_predict, width=5, color="black")
plot!(ratings_predict, popularity_predict; width=5, color="black")