-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst.jl
206 lines (189 loc) · 6.63 KB
/
tst.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using DataFrames, CSV
function most_frequent(vec::Vector)
dict = Dict()
max_frequency = 0
key = vec[1]
for e in vec
dict[e] = haskey(dict, e) ? dict[e] + 1 : 1
if dict[e] > max_frequency
max_frequency = dict[e]
key = e
end
end
key
end
function get_default(vec::Vector)
if typeof(vec) == typeof([missing, ""])
return ""
end
NaN
end
function fill!(df::DataFrame, is_generative::Bool)
for i in 1:ncol(df)
e = is_generative ? get_default(Vector(df[:, i])) : most_frequent(collect(skipmissing(df[:, i])))
df[:, i] = coalesce.(df[:, i], e)
end
end
# function fetcher(path::String, is_generative::Bool=false)
# @time df = DataFrame(CSV.File(path, stringtype=String))
# println(first(df, 10))
# dict = Dict()
# for e in df[:, :Cabin]
# k = ismissing(e) ? missing : e[1:1]
# dict[k] = haskey(dict, k) ? dict[k] + 1 : 1
# end
# println(unique(df[:, :Cabin]))
# println(length(unique(df[:, :Cabin])), " ..... ", length(collect(skipmissing(df[:, :Cabin]))))
# print_dict(dict)
# println(length(dict))
# println(".......................................................................")
# println(".......................................................................")
# prefixes = [r"A",]
# dict = Dict()
# for e in df[:, :Ticket]
# k = Int(e[1]) <= 57 && Int(e[1]) >= 49 ? missing : e[1:(isnothing(findfirst(' ', e)) ? length(e) : findfirst(' ', e) - 1)]
# dict[k] = haskey(dict, k) ? dict[k] + 1 : 1
# end
# println(unique(df[:, :Ticket]))
# println(length(unique(df[:, :Ticket])), " ..... ", length(collect(skipmissing(df[:, :Ticket]))))
# print_dict(dict)
# println(length(dict))
# end
function convert_cabin(s::Union{String, Missing})
res = repeat([0], 7)
if !ismissing(s) && length(s) > 0
res[1] = 1
k = s[1]
if k == 'A'
res[2] = 1
elseif k == 'B'
res[3] = 1
elseif k == 'C'
res[4] = 1
elseif k == 'D'
res[5] = 1
elseif k == 'E'
res[6] = 1
elseif k == 'F'
res[7] = 1
else
res[1] = 1
end
end
res
end
function convert_ticket(s::String)
res = repeat([0], 10)
if s[1] == 'A'
res[1] = 1
res[2] = 1
elseif s[1] == 'C'
res[1] = 1
res[3] = 1
elseif length(s) >= 2 && s[1:2] == "PC"
res[1] = 1
res[4] = 1
elseif length(s) >= 5 && s[1:5] == "SOTON"
res[1] = 1
res[5] = 1
elseif length(s) >= 4 && s[1:4] == "STON"
res[1] = 1
res[6] = 1
elseif s[1] == 'W'
res[1] = 1
res[7] = 1
elseif s[1] == 'S'
res[1] = 1
res[8] = 1
elseif !(Int(s[1]) <= 57 && Int(s[1]) >= 49)
res[1] = 1
res[9] = 1
else
res[10] = 1
end
res
end
function fetcher(path::String, is_generative::Bool=false)
@time df = DataFrame(CSV.File(path, stringtype=String))
y = []
if columnindex(df, :Survived) != 0
y = df[:, :Survived]
end
transform!(df, AsTable(:) .=> ByRow.([r -> convert_cabin(r.Cabin)[1],
r -> convert_cabin(r.Cabin)[2],
r -> convert_cabin(r.Cabin)[3],
r -> convert_cabin(r.Cabin)[4],
r -> convert_cabin(r.Cabin)[5],
r -> convert_cabin(r.Cabin)[6],
r -> convert_cabin(r.Cabin)[7],
r -> convert_ticket(r.Ticket)[1],
r -> convert_ticket(r.Ticket)[2],
r -> convert_ticket(r.Ticket)[3],
r -> convert_ticket(r.Ticket)[4],
r -> convert_ticket(r.Ticket)[5],
r -> convert_ticket(r.Ticket)[6],
r -> convert_ticket(r.Ticket)[7],
r -> convert_ticket(r.Ticket)[8],
r -> convert_ticket(r.Ticket)[9],
r -> convert_ticket(r.Ticket)[10]])
.=> [:Cabin1, :Cabin2, :Cabin3, :Cabin4, :Cabin5, :Cabin6, :Cabin7,
:Ticket1, :Ticket2, :Ticket3, :Ticket4, :Ticket5, :Ticket6, :Ticket7, :Ticket8, :Ticket9, :Ticket10])
# transform!(df, AsTable(:) .=> ByRow.([r -> ismissing(r.Cabin) ? 0 : (Int(r.Cabin[1]) <= 57 && Int(r.Cabin[1]) >= 49 ? 1 : 2)])
# .=> [:Cabin])
fill!(df, is_generative)
dropmissing!(df)
select!(df, :Sex, :Pclass, :Age, :SibSp, :Parch, :Fare, :Embarked,
:Cabin1, :Cabin2, :Cabin3, :Cabin4, :Cabin5, :Cabin6, :Cabin7,
:Ticket1, :Ticket2, :Ticket3, :Ticket4, :Ticket5, :Ticket6, :Ticket7, :Ticket8, :Ticket9, :Ticket10)
transform!(df, AsTable(:) .=> ByRow.([r -> r.Sex == "" ? NaN : (r.Sex == "male" ? 1 : 0),
r -> r.Embarked == "" ? NaN : (r.Embarked == "S" ? 0 : (r.Embarked == "Q" ? 1 : 2))])
.=> [:Sex, :Embarked])
X = reshape(Vector(df[1, :]),(1,length(df[1, :])))
for i in 2:rownumber(df[end, :])
X = [X; reshape(Vector(df[i, :]),(1,length(df[1, :])))]
end
reshape(X, (rownumber(df[end, :]), length(df[1, :]))), y
end
function print_dict(d::Dict)
vec = sort(collect(keys(d)))
for k in vec
println("(", k, ", ", d[k], ")")
end
end
# using JuMP, HiGHS
# function tst()
# A = [10 10; 10 10]
# K = [1 2; 3 4; 5 6]
# model = Model(HiGHS.Optimizer)
# @variable(model, X[1:2,1:3]>=0)
# @objective(model, Max, sum(X*K))
# @constraint(model, con, A - X*K .>= 0)
# optimize!(model)
# value.(X)
# end
# println(tst())
mutable struct A
@atomic x::Int64
end
function compute(s, i)
start = time_ns()
while (time_ns() - start) / 1e9 < s
end
# println(i * 10)
i * 10
end
@time begin
# a = Threads.@spawn compute(1)
# b = wait(a)
dict = [0 0 0 0 0]
println(Threads.nthreads())
Threads.@threads :dynamic for i in 1:5
res = A(0)
Threads.@threads :dynamic for j in 1:5
@atomic res.x += compute(1, j)
end
dict[i] = res.x
println(res.x)
end
println(dict)
end