daru 0.0.3.1 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +16 -0
- data/README.md +83 -23
- data/daru.gemspec +7 -0
- data/lib/daru/accessors/array_wrapper.rb +248 -0
- data/lib/daru/accessors/nmatrix_wrapper.rb +252 -0
- data/lib/daru/dataframe.rb +171 -72
- data/lib/daru/index.rb +29 -5
- data/lib/daru/io/io.rb +1 -1
- data/lib/daru/{math → maths}/arithmetic/dataframe.rb +1 -1
- data/lib/daru/maths/arithmetic/vector.rb +75 -0
- data/lib/daru/{math → maths}/statistics/dataframe.rb +1 -1
- data/lib/daru/maths/statistics/vector.rb +147 -0
- data/lib/daru/monkeys.rb +16 -10
- data/lib/daru/plotting/dataframe.rb +47 -0
- data/lib/daru/plotting/vector.rb +41 -0
- data/lib/daru/vector.rb +166 -40
- data/lib/version.rb +1 -1
- data/notebooks/intro_with_music_data_.ipynb +318 -0
- data/spec/dataframe_spec.rb +528 -472
- data/spec/fixtures/music_data.tsv +2501 -0
- data/spec/index_spec.rb +8 -0
- data/spec/io/io_spec.rb +1 -0
- data/spec/math/statistics/vector_spec.rb +144 -3
- data/spec/vector_spec.rb +165 -148
- metadata +32 -6
- data/lib/daru/math/arithmetic/vector.rb +0 -71
- data/lib/daru/math/statistics/vector.rb +0 -9
data/spec/index_spec.rb
CHANGED
@@ -51,4 +51,12 @@ describe Daru::Index do
|
|
51
51
|
:bob, :jimi, :richie].to_index)
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
context "#[]" do
|
56
|
+
it "works with ranges" do
|
57
|
+
id = Daru::Index.new [:one, :two, :three, :four, :five, :six, :seven]
|
58
|
+
|
59
|
+
expect(id[:two..:five]).to eq(Daru::Index.new([:two, :three, :four, :five]))
|
60
|
+
end
|
61
|
+
end
|
54
62
|
end
|
data/spec/io/io_spec.rb
CHANGED
@@ -1,12 +1,153 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe Daru::Vector do
|
4
|
+
[NMatrix, Array].each do |dtype|
|
5
|
+
describe dtype do
|
6
|
+
before :each do
|
7
|
+
@dv = Daru::Vector.new [323, 11, 555, 666, 234, 21, 666, 343, 1, 2], dtype: dtype
|
8
|
+
end
|
4
9
|
|
5
|
-
|
10
|
+
context "#mean" do
|
11
|
+
it "calculates mean" do
|
12
|
+
expect(@dv.mean).to eq(282.2)
|
13
|
+
end
|
14
|
+
end
|
6
15
|
|
7
|
-
|
16
|
+
context "#sum_of_squares" do
|
17
|
+
it "calcs sum of squares" do
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#standard_deviation_sample" do
|
23
|
+
it "calcs standard deviation sample" do
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "#variance_sample" do
|
29
|
+
it "calculates sample variance" do
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "#standard_deviation_population" do
|
35
|
+
it "calculates standard deviation population" do
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "#variance_population" do
|
41
|
+
it "calculates population variance" do
|
42
|
+
expect(@dv.variance_population).to eq(67606.95999999999)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "#sum_of_squared_deviation" do
|
47
|
+
it "calculates sum of squared deviation" do
|
48
|
+
expect(@dv.sum_of_squared_deviation).to eq(676069.6)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "#skew" do
|
53
|
+
it "calculates skewness" do
|
54
|
+
@dv.skew
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "#max" do
|
59
|
+
it "returns the max value" do
|
60
|
+
@dv.max
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "#min" do
|
65
|
+
it "returns the min value" do
|
66
|
+
@dv.min
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "#sum" do
|
71
|
+
it "returns the sum" do
|
72
|
+
@dv.sum
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "#product" do
|
77
|
+
it "returns the product" do
|
78
|
+
@dv.product
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "#median" do
|
83
|
+
it "returns the median" do
|
84
|
+
@dv.median
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "#mode" do
|
89
|
+
it "returns the mode" do
|
90
|
+
@dv.mode
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context "#kurtosis" do
|
95
|
+
it "calculates kurtosis" do
|
96
|
+
@dv.kurtosis
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "#percentile" do
|
101
|
+
it "calculates percentile" do
|
102
|
+
expect(@dv.percentile(50)).to eq(333.0)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "#recode" do
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
context "#recode!" do
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
context "#frequencies" do
|
115
|
+
it "calculates frequencies" do
|
116
|
+
@dv.frequencies
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context "#average_deviation_population" do
|
121
|
+
it "calculates average_deviation_population" do
|
122
|
+
@dv.average_deviation_population
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "#proportion" do
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
context "#proportions" do
|
131
|
+
it "calculates proportions" do
|
132
|
+
@dv.proportions
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "#ranked" do
|
137
|
+
it "curates by rank" do
|
138
|
+
@dv.ranked
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "#count" do
|
8
143
|
|
9
|
-
|
144
|
+
end
|
10
145
|
|
146
|
+
context "#coefficient_of_variation" do
|
147
|
+
it "calculates coefficient_of_variation" do
|
148
|
+
@dv.coefficient_of_variation
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
11
152
|
end
|
12
153
|
end
|
data/spec/vector_spec.rb
CHANGED
@@ -1,155 +1,172 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe Daru::Vector do
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
[Array, NMatrix].each do |dtype|
|
5
|
+
describe dtype do
|
6
|
+
context "#initialize" do
|
7
|
+
it "initializes from an Array" do
|
8
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :ravan,
|
9
|
+
index: [:ek, :don, :teen, :char, :pach], dtype: dtype
|
7
10
|
|
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
|
-
|
11
|
+
expect(dv.name) .to eq(:ravan)
|
12
|
+
expect(dv.index).to eq(Daru::Index.new [:ek, :don, :teen, :char, :pach])
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts Index object" do
|
16
|
+
idx = Daru::Index.new [:yoda, :anakin, :obi, :padme, :r2d2]
|
17
|
+
|
18
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: idx, dtype: dtype
|
19
|
+
|
20
|
+
expect(dv.name) .to eq(:yoga)
|
21
|
+
expect(dv.index).to eq(idx)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "raises error for improper Index" do
|
25
|
+
expect {
|
26
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:i, :j, :k]
|
27
|
+
}.to raise_error
|
28
|
+
|
29
|
+
expect {
|
30
|
+
idx = Daru::Index.new [:i, :j, :k]
|
31
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :yoda, index: idx, dtype: dtype
|
32
|
+
}.to raise_error
|
33
|
+
end
|
34
|
+
|
35
|
+
it "initializes without specifying an index" do
|
36
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :vishnu, dtype: dtype
|
37
|
+
|
38
|
+
expect(dv.index).to eq(Daru::Index.new [0,1,2,3,4])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "inserts nils for extra indices" do
|
42
|
+
dv = Daru::Vector.new [1,2,3], name: :yoga, index: [0,1,2,3,4], dtype: Array
|
43
|
+
|
44
|
+
expect(dv).to eq([1,2,3,nil,nil].dv(:yoga,nil, Array))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "#[]" do
|
49
|
+
before :each do
|
50
|
+
@dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
|
51
|
+
index: [:yoda, :anakin, :obi, :padme, :r2d2], dtype: dtype
|
52
|
+
end
|
53
|
+
|
54
|
+
it "returns an element after passing an index" do
|
55
|
+
expect(@dv[:yoda]).to eq(1)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns an element after passing a numeric index" , :focus => true do
|
59
|
+
expect(@dv[0]).to eq(1)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "returns a vector with given indices for multiple indices" do
|
63
|
+
expect(@dv[:yoda, :anakin]).to eq(Daru::Vector.new([1,2], name: :yoda,
|
64
|
+
index: [:yoda, :anakin], dtype: dtype))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "#[]=" do
|
69
|
+
before :each do
|
70
|
+
@dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
|
71
|
+
index: [:yoda, :anakin, :obi, :padme, :r2d2], dtype: dtype
|
72
|
+
end
|
73
|
+
|
74
|
+
it "assigns at the specified index" do
|
75
|
+
@dv[:yoda] = 666
|
76
|
+
|
77
|
+
expect(@dv[:yoda]).to eq(666)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "assigns at the specified Integer index" do
|
81
|
+
@dv[0] = 666
|
82
|
+
|
83
|
+
expect(@dv[:yoda]).to eq(666)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "#concat" do
|
88
|
+
before :each do
|
89
|
+
@dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
|
90
|
+
index: [:warwick, :thompson, :jackson, :fender, :esp], dtype: dtype
|
91
|
+
end
|
92
|
+
|
93
|
+
it "concatenates a new element at the end of vector with index" do
|
94
|
+
@dv.concat 6, :ibanez
|
95
|
+
|
96
|
+
expect(@dv.index) .to eq(
|
97
|
+
[:warwick, :thompson, :jackson, :fender, :esp, :ibanez].to_index)
|
98
|
+
expect(@dv[:ibanez]).to eq(6)
|
99
|
+
expect(@dv[5]) .to eq(6)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "concatenates without index if index is default numeric" do
|
103
|
+
vector = Daru::Vector.new [1,2,3,4,5], name: :nums, dtype: dtype
|
104
|
+
|
105
|
+
vector.concat 6
|
106
|
+
|
107
|
+
expect(vector.index).to eq([0,1,2,3,4,5].to_index)
|
108
|
+
expect(vector[5]) .to eq(6)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "raises error if index not specified and non-numeric index" do
|
112
|
+
expect {
|
113
|
+
@dv.concat 6
|
114
|
+
}.to raise_error
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "#delete" do
|
119
|
+
it "deletes specified value in the vector" do
|
120
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :a, dtype: dtype
|
121
|
+
|
122
|
+
dv.delete 3
|
123
|
+
|
124
|
+
expect(dv).to eq(Daru::Vector.new [1,2,4,5], name: :a)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "#delete_at" do
|
129
|
+
before :each do
|
130
|
+
@dv = Daru::Vector.new [1,2,3,4,5], name: :a,
|
131
|
+
index: [:one, :two, :three, :four, :five], dtype: dtype
|
132
|
+
end
|
133
|
+
|
134
|
+
it "deletes element of specified index" do
|
135
|
+
@dv.delete_at :one
|
136
|
+
|
137
|
+
expect(@dv).to eq(Daru::Vector.new [2,3,4,5], name: :a,
|
138
|
+
index: [:two, :three, :four, :five]), dtype: dtype
|
139
|
+
end
|
140
|
+
|
141
|
+
it "deletes element of specified integer index" do
|
142
|
+
@dv.delete_at 2
|
143
|
+
|
144
|
+
expect(@dv).to eq(Daru::Vector.new [1,2,4,5], name: :a,
|
145
|
+
index: [:one, :two, :four, :five]), dtype: dtype
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "#index_of" do
|
150
|
+
it "returns index of specified value" do
|
151
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :a,
|
152
|
+
index: [:one, :two, :three, :four, :five], dtype: dtype
|
153
|
+
|
154
|
+
expect(dv.index_of(1)).to eq(:one)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "#to_hash" do
|
159
|
+
it "returns the vector as a hash" do
|
160
|
+
dv = Daru::Vector.new [1,2,3,4,5], name: :a,
|
161
|
+
index: [:one, :two, :three, :four, :five], dtype: dtype
|
162
|
+
|
163
|
+
expect(dv.to_hash).to eq({one: 1, two: 2, three: 3, four: 4, five: 5})
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "#uniq" do
|
168
|
+
# TODO
|
169
|
+
end
|
153
170
|
end
|
154
171
|
end
|
155
172
|
end if mri?
|