Метод UDF sim(String[] a, String[] b), где a и b взяты из двух разных наборов данных, всегда получает одни и те же значеJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Метод UDF sim(String[] a, String[] b), где a и b взяты из двух разных наборов данных, всегда получает одни и те же значе

Сообщение Anonymous »

I have to calculate a similarity value between the competencies of a reference local authority of France. This one:



C1010
C1015
C1020
C1025
C1030
C1502
C1505
C1507
C1510
C1515
C1520
C1525
C1528
C1529
C1530
C1531
C1532
C1533
C1534
C1535
C1540
C1545
C1550
C1555
C1560
C2005
C2010
C2015
C2510
C2515
C2520
C2521
C2525
C2526
C3005
C3010
C3015
C3020
C3025
C3210
C3220
C3505
C3510
C3515
C4005
C4006
C4007
C4008
C4010
C4015
C4016
C4017
C4020
C4025
C4505
C4510
C4515
C4520
C4525
C4530
C4531
C4532
C4535
C4540
C4545
C4550
C4555
C4560
C5005
C5010
C5015
C5210
C5220
C5505
C5510
C5515
C5520
C5525
C5530
C5535
C5540
C5545
C5550
C5555
C7005
C7010
C7012
C7015
C7020
C7025
C7030
C9905
C9910
C9915
C9920
C9922
C9923
C9924
C9925
C9930
C9935
C9940
C9950
C9999
nomGroupement
sirenGroupement
nombreDeCompetencesExercees




0
0
0
1
0
1
1
1
1
0
0
0
1
1
0
1
1
0
1
0
1
0
1
0
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
0
0
1
1
1
1
1
1
0
1
0
0
0
0
0
0
1
0
1
0
0
1
0
1
1
1
0
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
1
0
0
1
CC du Thouarsais
247900798
37



and those of all local authorities of France, f i r s t o f t h e m a r e : < / p > < b r / > < d i v c l a s s = " s - t a b l e - c o n t a i n e r " > < t a b l e c l a s s = " s - t a b l e " > < b r / > < t h e a d > < b r / > < t r > < b r / > < t h > C 1 0 1 0 < / t h > < b r / > < t h > C 1 0 1 5 < / t h > < b r / > < t h > C 1 0 2 0 < / t h > < b r / > < t h > C 1 0 2 5 < / t h > < b r / > < t h > C 1 0 3 0 < / t h > < b r / > < t h > C 1 5 0 2 < / t h > < b r / > < t h > C 1 5 0 5 < / t h > < b r / > < t h > C 1 5 0 7 < / t h > < b r / > < t h > C 1 5 1 0 < / t h > < b r / > < t h > C 1 5 1 5 < / t h > < b r / > < t h > C 1 5 2 0 < / t h > < b r / > < t h > C 1 5 2 5 < / t h > < b r / > < t h > C 1 5 2 8 < / t h > < b r / > < t h > C 1 5 2 9 < / t h > < b r / > < t h > C 1 5 3 0 < / t h > < b r / > < t h > C 1 5 3 1 < / t h > < b r / > < t h > C 1 5 3 2 < / t h > < b r / > < t h > C 1 5 3 3 < / t h > < b r / > < t h > C 1 5 3 4 < / t h > < b r / > < t h > C 1 5 3 5 < / t h > < b r / > < t h > C 1 5 4 0 < / t h > < b r / > < t h > C 1 5 4 5 < / t h > < b r / > < t h > C 1 5 5 0 < / t h > < b r / > < t h > C 1 5 5 5 < / t h > < b r / > < t h > C 1 5 6 0 < / t h > < b r / > < t h > C 2 0 0 5 < / t h > < b r / > < t h > C 2 0 1 0 < / t h > < b r / > < t h > C 2 0 1 5 < / t h > < b r / > < t h > C 2 5 1 0 < / t h > < b r / > < t h > C 2 5 1 5 < / t h > < b r / > < t h > C 2 5 2 0 < / t h > < b r / > < t h > C 2 5 2 1 < / t h > < b r / > < t h > C 2 5 2 5 < / t h > < b r / > < t h > C 2 5 2 6 < / t h > < b r / > < t h > C 3 0 0 5 < / t h > < b r / > < t h > C 3 0 1 0 < / t h > < b r / > < t h > C 3 0 1 5 < / t h > < b r / > < t h > C 3 0 2 0 < / t h > < b r / > < t h > C 3 0 2 5 < / t h > < b r / > < t h > C 3 2 1 0 < / t h > < b r / > < t h > C 3 2 2 0 < / t h > < b r / > < t h > C 3 5 0 5 < / t h > < b r / > < t h > C 3 5 1 0 < / t h > < b r / > < t h > C 3 5 1 5 < / t h > < b r / > < t h > C 4 0 0 5 < / t h > < b r / > < t h > C 4 0 0 6 < / t h > < b r / > < t h > C 4 0 0 7 < / t h > < b r / > < t h > C 4 0 0 8 < / t h > < b r / > < t h > C 4 0 1 0 < / t h > < b r / > < t h > C 4 0 1 5 < / t h > < b r / > < t h > C 4 0 1 6 < / t h > < b r / > < t h > C 4 0 1 7 < / t h > < b r / > < t h > C 4 0 2 0 < / t h > < b r / > < t h > C 4 0 2 5 < / t h > < b r / > < t h > C 4 5 0 5 < / t h > < b r / > < t h > C 4 5 1 0 < / t h > < b r / > < t h > C 4 5 1 5 < / t h > < b r / > < t h > C 4 5 2 0 < / t h > < b r / > < t h > C 4 5 2 5 < / t h > < b r / > < t h > C 4 5 3 0 < / t h > < b r / > < t h > C 4 5 3 1 < / t h > < b r / > < t h > C 4 5 3 2 < / t h > < b r / > < t h > C 4 5 3 5 < / t h > < b r / > < t h > C 4 5 4 0 < / t h > < b r / > < t h > C 4 5 4 5 < / t h > < b r / > < t h > C 4 5 5 0 < / t h > < b r / > < t h > C 4 5 5 5 < / t h > < b r / > < t h > C 4 5 6 0 < / t h > < b r / > < t h > C 5 0 0 5 < / t h > < b r / > < t h > C 5 0 1 0 < / t h > < b r / > < t h > C 5 0 1 5 < / t h > < b r / > < t h > C 5 2 1 0 < / t h > < b r / > < t h > C 5 2 2 0 < / t h > < b r / > < t h > C 5 5 0 5 < / t h > < b r / > < t h > C 5 5 1 0 < / t h > < b r / > < t h > C 5 5 1 5 < / t h > < b r / > < t h > C 5 5 2 0 < / t h > < b r / > < t h > C 5 5 2 5 < / t h > < b r / > < t h > C 5 5 3 0 < / t h > < b r / > < t h > C 5 5 3 5 < / t h > < b r / > < t h > C 5 5 4 0 < / t h > < b r / > < t h > C 5 5 4 5 < / t h > < b r / > < t h > C 5 5 5 0 < / t h > < b r / > < t h > C 5 5 5 5 < / t h > < b r / > < t h > C 7 0 0 5 < / t h > < b r / > < t h > C 7 0 1 0 < / t h > < b r / > < t h > C 7 0 1 2 < / t h > < b r / > < t h > C 7 0 1 5 < / t h > < b r / > < t h > C 7 0 2 0 < / t h > < b r / > < t h > C 7 0 2 5 < / t h > < b r / > < t h > C 7 0 3 0 < / t h > < b r / > < t h > C 9 9 0 5 < / t h > < b r / > < t h > C 9 9 1 0 < / t h > < b r / > < t h > C 9 9 1 5 < / t h > < b r / > < t h > C 9 9 2 0 < / t h > < b r / > < t h > C 9 9 2 2 < / t h > < b r / > < t h > C 9 9 2 3 < / t h > < b r / > < t h > C 9 9 2 4 < / t h > < b r / > < t h > C 9 9 2 5 < / t h > < b r / > < t h > C 9 9 3 0 < / t h > < b r / > < t h > C 9 9 3 5 < / t h > < b r / > < t h > C 9 9 4 0 < / t h > < b r / > < t h > C 9 9 5 0 < / t h > < b r / > < t h > C 9 9 9 9 < / t h > < b r / > < t h > n o m G r o u p e m e n t < / t h > < b r / > < t h > s i r e n G r o u p e m e n t < / t h > < b r / > < t h > n o m b r e D e C o m p e t e n c e s E x e r c e e s < / t h > < b r / > < / t r > < b r / > < / t h e a d > < b r / > < t b o d y > < b r / > < t r > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 1 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d > < b r / > < t d > 0 < / t d>
0
0
0
0
0
0
1
1
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
1
0
1
1
0
1
0
1
0
0
0
0
0
0
0
0
1
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
0
0
CC du Val de l'Oise
200040426
25


0
0
0
1
0
0
1
1
1
0
0
0
1
1
0
1
1
0
0
0
1
0
0
0
0
0
0
0
1
0
1
1
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
1
0
0
0
1
1
1
1
1
1
0
0
1
0
0
0
0
0
0
0
1
0
0
1
0
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
1
0
0
1
CC de l'Est de la Somme
200070985
30


0
0
0
0
0
1
1
1
1
0
0
0
1
1
0
1
1
0
0
0
1
0
0
1
0
0
0
0
0
0
1
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
1
0
1
0
1
1
1
1
0
1
0
0
0
0
0
0
1
0
0
0
0
1
0
1
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
CC du Pays de Bray
246000913
29



I received many solutions from the Data Science S.E. here and I'm willing to experience them. But I'm using Java and Spark 3.5.4 so I have to transpose. And then, I'm facing a trouble.
To perform the job, I have created an UDF function whose meant prototype is:

Double similariteCompetences(String sirenGroupement, String[] competencesIntercoCandidate, String[] competencesIntercoReference) и реализуется следующим образом:
/** Les codes des compétences étudiées */
private final String[] codesCompetences = {
"C1010", "C1015", "C1020", "C1025", "C1030", "C1502", "C1505", "C1507", "C1510", "C1515",
"C1520", "C1525", "C1528", "C1529", "C1530", "C1531", "C1532", "C1533", "C1534", "C1535",
"C1540", "C1545", "C1550", "C1555", "C1560", "C2005", "C2010", "C2015", "C2510", "C2515",
"C2520", "C2521", "C2525", "C2526", "C3005", "C3010", "C3015", "C3020", "C3025", "C3210",
"C3220", "C3505", "C3510", "C3515", "C4005", "C4006", "C4007", "C4008", "C4010", "C4015",
"C4016", "C4017", "C4020", "C4025", "C4505", "C4510", "C4515", "C4520",
"C4525", "C4530", "C4531", "C4532", "C4535", "C4540", "C4545", "C4550", "C4555", "C4560",
"C5005", "C5010", "C5015", "C5210", "C5220", "C5505", "C5510", "C5515", "C5520", "C5525",
"C5530", "C5535", "C5540", "C5545", "C5550", "C5555", "C7005", "C7010", "C7012", "C7015",
"C7020", "C7025", "C7030", "C9905", "C9910", "C9915", "C9920", "C9922", "C9923", "C9924",
"C9925", "C9930", "C9935", "C9940", "C9950", "C9999"
};

/**
* Renvoyer le dataset des différences de choix de compétences, ordonné par taux de similarité
* @param intercommunalites Compétences des intercommunalités du jeu de données
* @return Dataset des différences de choix de compétences, avec une colonne "similarite" supplémentaire, ordonné par taux de similarité
*/
private Dataset differencesDeChoixDeCompetencesParUDF(Dataset intercoDeReference, Dataset intercommunalites) {
UDF3 similariteCompetences = new UDF3() {
public Double call(String sirenGroupement, scala.collection.immutable.ArraySeq.ofRef competencesIntercoCandidate, scala.collection.immutable.ArraySeq.ofRef competencesIntercoReference) {
int nombreCompetencesEquivalentes = 0;

scala.collection.Iterator itCandidat = competencesIntercoCandidate.iterator();
scala.collection.Iterator itReference = competencesIntercoReference.iterator();

while(itCandidat.hasNext() && itReference.hasNext()) {
nombreCompetencesEquivalentes += itCandidat.next().equals(itReference.next()) ? 1 : 0;
}

return (double)nombreCompetencesEquivalentes / (double)GroupementsPerimetresITCase.this.codesCompetences.length;
}
};

this.sparkSession.sqlContext().udf().register("similariteCompetences", similariteCompetences, DataTypes.DoubleType);
Column similarite = callUDF("similariteCompetences", SIREN_GROUPEMENT.col(), columnCompetences(intercommunalites), columnCompetences(intercoDeReference));

return intercommunalites.withColumn("similarite", similarite).orderBy(similarite.desc());
}

И я ожидаю, что он добавит к каждому местному органу власти Франции столбец «аналогичный» от 0 до 1,0, показывающий, сколько это стоит. аналогично моей ссылке.
Но бывает, что при запуске этот метод устанавливает значение 1,0 для каждой строки.

Оба массива компетенций для сравнения, один из эталонных местных органов власти, один из Кандидаты местных органов власти для сравнения всегда равны.
Сложная часть:
Успех вызвать UDF code> функция, требующая двух параметров массива [которые имеют оба значения 104], мне нужно подготовить столбец массива, направляющий значения полей наборов данных. Потому что функцию UDF можно вызывать только с аргументами Column. И для этого я использую метод org.apache.spark.sql.functions.array().
/**
* Renvoyer une colonne rapportant toutes les compétences d'une intercommunalité
* @param intercommunalites Le dataset d'intercommunalité(s) vers lequel doivent se référer les colonnes
* @return Column Array de compétences d'une intercommunalité
*/
private Column columnCompetences(Dataset intercommunalites) {
intercommunalites.show(2000, false);

Column[] colonnesCompetences = new Column[this.codesCompetences.length];

for(int index=0; index < this.codesCompetences.length; index++) {
colonnesCompetences[index] = intercommunalites.col(this.codesCompetences[index]);
}

return array(colonnesCompetences);
}

Что происходит:
Примерные списки компетенций в верхней части этого вопроса — это те, которые intercommunalites.show (2000, false) отображается.
И я ожидаю, что этот метод создаст поля таргетинга по столбцам в соответствующих наборах данных.
Но во время выполнения точка останова, установленная на строка:
return (double)nombreCompetencesEquivalentes / (double)GroupementsPerimetresITCase.this.codesCompetences.length; различийDeChoixDeCompetencesParUDF(...)
показывает, что он всегда находит 104 похожих значения, поскольку оба полученных аргумента полностью равны...
Изображение

Изображение

Какие бы строки ни рассматривались.

Согласно дампам наборов данных, они должны были быть разными.
Почему я каждый раз получаю одни и те же значения для обоих параметров?

Я добавляю этот код, чтобы показать, как intercoReference или межобщинные взяты в начале:
/**
* Les compétences communes d'intercommunalités
*/
@DisplayName("Compétences exclusives d'intercommunalités ou exclues")
@Order(30)
@Test
void competencesExclusivesEtExclues() {
String sirenGroupementCandidat = "247900798";

List competences = new ArrayList();

for(String codeCompetence : this.codesCompetences) {
competences.add(new Column(codeCompetence));
}

List selectClause = new ArrayList(competences);
selectClause.add(NOM_GROUPEMENT.col());
selectClause.add(SIREN_GROUPEMENT.col());
selectClause.add(col("nombreDeCompetencesExercees"));

OptionsCreationLecture options = this.perimetreEPCIDataset.optionsCreationLecture();

// Compétences des communautés de communes
Dataset competencesCC = this.perimetreEPCIDataset.rowPerimetres(options, new HistoriqueExecution(), 2024, true, new EPCIPerimetreTriCommuneSiegeCommuneMembreSirenMembre())
.where(NATURE_JURIDIQUE.col().equalTo("CC") // Les communautés de communes
.and(CODE_COMMUNE_SIEGE.col().equalTo(CODE_COMMUNE_MEMBRE.col()))) // Seulement la ligne de périmètre se rapportant au groupement siège
.sample(0.06, 15)
.select(selectClause.toArray(new Column[]{}));

Dataset intercoReference = competencesCC.where(SIREN_GROUPEMENT.col().equalTo(sirenGroupementCandidat));

LOGGER.info("Intercommunalité candidate : ");
intercoReference.show(1, false);

LOGGER.info("Intercommunalités comparées : ");
differencesDeChoixDeCompetencesParUDF(intercoReference, competencesCC)
.show(2000, false);
}


Подробнее здесь: https://stackoverflow.com/questions/793 ... o-differen
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»