Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Markus Krug
EfficientRuleLearning
Commits
6e3d86c6
Commit
6e3d86c6
authored
Oct 07, 2016
by
Markus Krug
Browse files
fast fertig ;)
parent
ae69e02e
Changes
4
Hide whitespace changes
Inline
Side-by-side
de.uniwue.ls6.rulelearning/DataStructure/src/de/uniwue/ls6/datastructure/Instance.java
View file @
6e3d86c6
package
de.uniwue.ls6.datastructure
;
import
java.awt.Point
;
import
java.util.List
;
import
de.uniwue.ls6.util.MatrixUtil
;
import
no.uib.cipr.matrix.sparse.FlexCompColMatrix
;
public
class
Instance
{
/*
* a datapoint to CLASSIFY
*/
//featureset
//
featureset
private
int
[][]
featureArray
;
private
int
label
;
public
Instance
(
int
nrRows
,
int
nrCols
)
{
public
Instance
(
int
nrRows
,
int
nrCols
)
{
super
();
featureArray
=
new
int
[
nrRows
][
nrCols
];
}
public
Instance
(
int
nrRows
,
int
nrCols
,
int
label
)
{
public
Instance
(
int
nrRows
,
int
nrCols
,
int
label
)
{
super
();
featureArray
=
new
int
[
nrRows
][
nrCols
];
this
.
label
=
label
;
}
public
int
getLabel
()
{
return
label
;
}
public
void
setLabel
(
int
label
)
{
this
.
label
=
label
;
}
public
int
[][]
getFeatureArray
()
{
return
featureArray
;
}
// this could be made faster by either a change of representation
// (Map<int->Set or even by calculating the array position by a hash
// function)
public
boolean
containsFeature
(
List
<
Point
>
features
)
{
outer:
for
(
Point
p
:
features
)
{
for
(
int
row
=
0
;
row
<
featureArray
.
length
;
row
++)
{
if
(
featureArray
[
row
][
p
.
x
]
==
p
.
y
)
{
continue
outer
;
}
}
return
false
;
}
return
true
;
}
//erform kronecker expansion
public
FlexCompColMatrix
expand
(
List
<
MatrixMapping
>
mappings
)
{
MatrixMapping
lastMapping
=
mappings
.
get
(
mappings
.
size
()-
1
);
for
(
Point
denseIndices
:
lastMapping
.
getMappingMap
().
keySet
()){
List
<
Point
>
features
=
MatrixUtil
.
determineFeaturesForIndex
(
denseIndices
,
mappings
);
//check if this instance contains the features
if
(
containsFeature
(
features
)){
}
//if so then create a sprase matrix and put a 1 into the according slot
//expand the matrix clever (use the index of denseIndices and expand with non sparse elemtns)
}
return
null
;
}
}
de.uniwue.ls6.rulelearning/DataStructure/src/de/uniwue/ls6/datastructure/MatrixMapping.java
0 → 100644
View file @
6e3d86c6
package
de.uniwue.ls6.datastructure
;
import
java.awt.Point
;
import
java.util.HashMap
;
import
java.util.Set
;
public
class
MatrixMapping
{
HashMap
<
Point
,
Point
>
mappingMap
;
HashMap
<
Point
,
Point
>
inverseMappingMap
;
//
/**
*
* @param mappingMap
* @param inverseMap
*
* a point is an index
*/
public
MatrixMapping
(
HashMap
<
Point
,
Point
>
mappingMap
,
HashMap
<
Point
,
Point
>
inverseMap
)
{
super
();
this
.
mappingMap
=
mappingMap
;
this
.
inverseMappingMap
=
inverseMap
;
}
public
MatrixMapping
()
{
super
();
this
.
mappingMap
=
new
HashMap
<
Point
,
Point
>();
this
.
inverseMappingMap
=
new
HashMap
<
Point
,
Point
>();
}
public
HashMap
<
Point
,
Point
>
getMappingMap
()
{
return
mappingMap
;
}
public
void
setMappingMap
(
HashMap
<
Point
,
Point
>
mappingMap
)
{
this
.
mappingMap
=
mappingMap
;
}
public
void
addEntry
(
Point
key
,
Point
value
)
{
this
.
mappingMap
.
put
(
key
,
value
);
// TODO overlaps???
this
.
inverseMappingMap
.
put
(
value
,
key
);
}
// use this if you wanna infer the values later
public
void
addEntry
(
Point
key
)
{
this
.
mappingMap
.
put
(
key
,
null
);
}
// this method generates all values based on the keys
public
void
inferDenseMapValues
()
{
int
numCols
=
(
int
)
Math
.
ceil
(
Math
.
sqrt
(
mappingMap
.
keySet
().
size
()));
int
index
=
0
;
for
(
Point
key
:
mappingMap
.
keySet
())
{
Point
value
=
new
Point
(
index
%
numCols
,
index
/
numCols
);
mappingMap
.
put
(
key
,
value
);
inverseMappingMap
.
put
(
value
,
key
);
index
++;
}
}
public
int
getDenseMatrixDimension
()
{
return
(
int
)
Math
.
ceil
(
Math
.
sqrt
(
mappingMap
.
keySet
().
size
()));
}
public
Point
getBackwardsMappedFeature
(
Point
feature
)
{
return
inverseMappingMap
.
get
(
feature
);
}
}
de.uniwue.ls6.rulelearning/DataStructure/src/de/uniwue/ls6/datastructure/MatrixMcMatrixFace.java
View file @
6e3d86c6
package
de.uniwue.ls6.datastructure
;
import
no.uib.cipr.matrix.MatrixEntry
;
import
no.uib.cipr.matrix.sparse.FlexCompColMatrix
;
public
class
MatrixMcMatrixFace
{
...
...
@@ -27,14 +28,13 @@ public class MatrixMcMatrixFace {
// l2r t2B
for
(
int
col
=
0
;
col
<
featureArray
[
0
].
length
;
col
++)
{
for
(
int
row
=
0
;
row
<
featureArray
.
length
;
row
++)
{
if
(
goldLabel
==
i
.
getLabel
()){
//add to TP matrix
addToMatrix
(
featureArray
[
row
][
col
],
col
,
tpMatrix
);
}
else
{
//add to FP matrix
addToMatrix
(
featureArray
[
row
][
col
],
col
,
fpMatrix
);
if
(
goldLabel
==
i
.
getLabel
())
{
// add to TP matrix
addToMatrix
(
featureArray
[
row
][
col
],
col
,
tpMatrix
);
}
else
{
// add to FP matrix
addToMatrix
(
featureArray
[
row
][
col
],
col
,
fpMatrix
);
}
}
}
...
...
@@ -45,11 +45,43 @@ public class MatrixMcMatrixFace {
matrix
.
add
(
feature
,
windowcolumn
,
1
);
}
/**
* Calculates the best rule this matrix respresents, we do not need to store
* the whole matrix because we only use the Sum Matrix as Expansion, we do
* not want rules that generate only False positives (and therefoere a
* negative score)
*/
public
int
getMaximumScore
()
{
double
maxScore
=
0
;
for
(
MatrixEntry
entry
:
tpMatrix
)
{
double
scoreCurrent
=
entry
.
get
()
-
fpMatrix
.
get
(
entry
.
row
(),
entry
.
column
());
if
(
scoreCurrent
>
maxScore
)
{
maxScore
=
scoreCurrent
;
}
}
return
(
int
)
maxScore
;
}
public
FlexCompColMatrix
getTpMatrix
()
{
return
tpMatrix
;
}
public
FlexCompColMatrix
getFpMatrix
()
{
return
fpMatrix
;
}
public
void
addToMatrix
(
FlexCompColMatrix
matrix
,
FlexCompColMatrix
expandedInstance
)
{
matrix
.
add
(
expandedInstance
);
}
/*
* 1. Differenzmatrix ausrechnen => Best Score ist Max(Matrix)
* 2. Setze Werte auf Sparse wenn Summe von TP un FP <=? MaxScore
* 3. Expansion der Matrix (Gruppierung von n+1 Windowelementen) (ber alle Instanzen gehen)
* 1. Differenzmatrix ausrechnen => Best Score ist Max(Matrix) 2. Setze
* check! Werte auf Sparse wenn Summe von TP un FP <=? MaxScore 3. Expansion
* der Matrix (Gruppierung von n+1 Windowelementen) (ber alle Instanzen
* gehen)
*/
}
de.uniwue.ls6.rulelearning/DataStructure/src/de/uniwue/ls6/util/MatrixUtil.java
0 → 100644
View file @
6e3d86c6
package
de.uniwue.ls6.util
;
import
java.awt.Point
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.List
;
import
de.uniwue.ls6.datastructure.Instance
;
import
de.uniwue.ls6.datastructure.MatrixMapping
;
import
de.uniwue.ls6.datastructure.MatrixMcMatrixFace
;
import
no.uib.cipr.matrix.MatrixEntry
;
import
no.uib.cipr.matrix.sparse.FlexCompColMatrix
;
public
class
MatrixUtil
{
public
static
MatrixMapping
getMappingForMaximum
(
MatrixMcMatrixFace
matrixface
,
int
maximum
)
{
MatrixMapping
matrixMapping
=
new
MatrixMapping
();
for
(
MatrixEntry
entry
:
matrixface
.
getTpMatrix
())
{
double
sum
=
entry
.
get
()
+
matrixface
.
getFpMatrix
().
get
(
entry
.
row
(),
entry
.
column
());
if
(
sum
>=
maximum
)
{
matrixMapping
.
addEntry
(
new
Point
(
entry
.
row
(),
entry
.
column
()));
}
}
return
matrixMapping
;
}
public
static
MatrixMcMatrixFace
performKroneckerExpansion
(
List
<
MatrixMapping
>
mappings
,
List
<
Instance
>
instances
,
int
label
)
{
MatrixMapping
lastMapping
=
mappings
.
get
(
mappings
.
size
()
-
1
);
int
dimension
=
lastMapping
.
getDenseMatrixDimension
();
MatrixMcMatrixFace
expandedMatrixFace
=
new
MatrixMcMatrixFace
(
dimension
*
dimension
,
dimension
*
dimension
,
label
);
// populate the matrix with the dataset this is expensive!
for
(
Instance
inst
:
instances
)
{
// expand
FlexCompColMatrix
expandedInstance
=
inst
.
expand
(
mappings
);
// add to kronecker
if
(
inst
.
getLabel
()
==
label
)
{
expandedMatrixFace
.
addToMatrix
(
expandedMatrixFace
.
getTpMatrix
(),
expandedInstance
);
}
else
{
expandedMatrixFace
.
addToMatrix
(
expandedMatrixFace
.
getFpMatrix
(),
expandedInstance
);
}
}
return
expandedMatrixFace
;
}
public
static
List
<
Point
>
determineFeaturesForIndex
(
Point
index
,
List
<
MatrixMapping
>
mappings
){
List
<
Point
>
reversedfeatures
=
new
ArrayList
<
Point
>(
Arrays
.
asList
(
new
Point
[]{
index
}));
for
(
int
i
=
mappings
.
size
()
-
1
;
i
>=
0
;
i
--){
MatrixMapping
currentMapping
=
mappings
.
get
(
i
);
revertKroneckerExpansionAndMapping
(
currentMapping
,
reversedfeatures
);
}
return
reversedfeatures
;
}
private
static
void
revertKroneckerExpansionAndMapping
(
MatrixMapping
currentMapping
,
List
<
Point
>
reversedfeatures
)
{
List
<
Point
>
toRemove
=
new
ArrayList
<
Point
>();
List
<
Point
>
toAdd
=
new
ArrayList
<
Point
>();
for
(
Point
p
:
reversedfeatures
){
//revert the mapping step this is easy !
Point
backwardsMappedFeature
=
currentMapping
.
getBackwardsMappedFeature
(
p
);
//revert the kronecker expansion step this generates 2 points
//TODO its not always 4
int
sizeBeforeExpansion
=
4
;
int
xBefore1
=
(
int
)
Math
.
floor
(
backwardsMappedFeature
.
x
/
sizeBeforeExpansion
);
int
xBefore2
=
backwardsMappedFeature
.
x
%
sizeBeforeExpansion
;
int
yBefore1
=
(
int
)
Math
.
floor
(
backwardsMappedFeature
.
y
/
sizeBeforeExpansion
);
int
yBefore2
=
backwardsMappedFeature
.
y
%
sizeBeforeExpansion
;
Point
firstBack
=
new
Point
(
xBefore1
,
yBefore1
);
Point
secondBack
=
new
Point
(
xBefore2
,
yBefore2
);
toRemove
.
add
(
p
);
toAdd
.
add
(
secondBack
);
toAdd
.
add
(
firstBack
);
}
reversedfeatures
.
removeAll
(
toRemove
);
reversedfeatures
.
addAll
(
toAdd
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment