forked from bkamins/JuliaForDataAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch08.jl
146 lines (97 loc) · 2.92 KB
/
ch08.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Bogumił Kamiński, 2022
# Codes for chapter 8
# Code for section 8.1
import Downloads
if isfile("puzzles.csv.bz2")
@info "file already present"
else
@info "fetching file"
Downloads.download("https://database.lichess.org/" *
"lichess_db_puzzle.csv.bz2",
"puzzles.csv.bz2")
end
using CodecBzip2
compressed = read("puzzles.csv.bz2")
plain = transcode(Bzip2Decompressor, compressed)
open("puzzles.csv", "w") do io
println(io, "PuzzleId,FEN,Moves,Rating,RatingDeviation," *
"Popularity,NbPlays,Themes,GameUrl")
write(io, plain)
end
readlines("puzzles.csv")
# Code for section 8.2
using CSV
using DataFrames
puzzles = CSV.read("puzzles.csv", DataFrame);
puzzles2 = CSV.read(plain, DataFrame;
header=["PuzzleId", "FEN", "Moves",
"Rating","RatingDeviation",
"Popularity", "NbPlays",
"Themes","GameUrl"]);
puzzles == puzzles2
compressed = nothing
plain = nothing
# Code for listing 8.1
puzzles
# Code for listing 8.2
describe(puzzles)
# Code for getting basic information about a data frame
ncol(puzzles)
nrow(puzzles)
names(puzzles)
CSV.write("puzzles2.csv", puzzles)
read("puzzles2.csv")
read("puzzles2.csv") == read("puzzles.csv")
# Code for section 8.3
puzzles.Rating
using BenchmarkTools
@btime $puzzles.Rating;
puzzles.Rating == copy(puzzles.Rating)
puzzles.Rating === copy(puzzles.Rating)
puzzles.Rating === puzzles.Rating
copy(puzzles.Rating) === copy(puzzles.Rating)
puzzles."Rating"
col = "Rating"
# data_frame_name[selected_rows, selected_columns]
puzzles[:, "Rating"]
puzzles[:, :Rating]
puzzles[:, 4]
puzzles[:, col]
columnindex(puzzles, "Rating")
columnindex(puzzles, "Some fancy column name")
hasproperty(puzzles, "Rating")
hasproperty(puzzles, "Some fancy column name")
@btime $puzzles[:, :Rating];
puzzles[!, "Rating"]
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
col in ["Rating", "RatingDeviation",
"Popularity", "NbPlays"]]...)
# Code for section 8.4
# Codes for Arrow examples
using Arrow
Arrow.write("puzzles.arrow", puzzles)
arrow_table = Arrow.Table("puzzles.arrow")
puzzles_arrow = DataFrame(arrow_table);
puzzles_arrow == puzzles
puzzles_arrow.PuzzleId
puzzles_arrow.PuzzleId[1] = "newID"
puzzles_arrow = copy(puzzles_arrow);
puzzles_arrow.PuzzleId
# Codes for SQLite examples
using SQLite
db = SQLite.DB("puzzles.db")
SQLite.load!(puzzles, db, "puzzles")
SQLite.tables(db)
SQLite.columns(db, "puzzles")
query = DBInterface.execute(db, "SELECT * FROM puzzles")
puzzles_db = DataFrame(query);
puzzles_db == puzzles
puzzles_db.PuzzleId