Analyzing RDF Graph: average number of certain relation

trevore

I'm new to SPARQL.

I'm trying to find a way to generally analyze and RDF graph, meaning for example the average number of a certain relation for a subject. So if we would have the data

[Alice         likes     Money]
[Bob           has       Money]
[Bob           likes     Diving] 
[Bob           likes     Skiing]

What is the average number of "likes" per node, (here: 1.5).

My first try is to simply write a script to iterate all distinct objects and query for the count of likes relations on each.

Is there a way to do this directly in SPARQL?

RobV

Yes you can use GROUP BY and aggregates for this kind of thing. See Aggregates in the specification for an overview of this.

If you wanted to get the likes per node you can do so like so:

PREFIX : <http://example.org/ns#>

SELECT ?node (COUNT(*) AS ?likes)
WHERE
{
  ?s :likes ?node
}
GROUP BY ?node

Here we group by the ?node and do a COUNT(*) which simply counts the number of solutions in the group. This gives us the number of likes for every distinct ?node value in a single query.

If we wanted to find the average likes per node we can also do this using aggregates:

PREFIX : <http://example.org/ns#>

SELECT 
 (COUNT(*) AS ?likeCount) 
 (COUNT(DISTINCT ?node) AS ?nodeCount) 
 (?likeCount / ?nodeCount AS ?avgLikesPerNode)
WHERE
{
  ?s :likes ?node .
}

Here we use COUNT(*) again to get the total number of likes and then we use COUNT(DISTINCT ?node) which will count the distinct values for ?node and then we can simply divide our ?likeCount by our ?nodeCount to give us the average likes per node.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Random number with fixed average

The number of records in the relation in Laravel

R regression analysis: analyzing data for a certain ethnicity

Building a average for a certain timeframe

How to represent temporal relation like <time:before> in RDF?

What is the benefit of defining datatypes for literals in an RDF graph?

Mysql: possible to add constraint that prevents a one to many relation from having less than certain number of relations?

Is RDF graph lean or not lean?

Finding average of a certain number is not working

Adding RDF text to graph RDFlib

Average number of subdirs

How to plot average graph in R

Find the average of certain fields

How do I select specific values in a row to average if another number in the row equals a certain value?

How to declare a symmetric relation in RDF?

How to export graph in RDF file using RDFLib

Updating a graph in an RDF file

calculate average of rows when average row is bigger than a certain number with mysql

Average certain cells based on criteria

Average a certain column value in MySQL

Star rating average number

How to draw an RDF graph?

Analyzing string input until it reaches a certain element in python

Change number on graph if certain condition occurs

How can I compute the rolling average of a column up to a certain number of rows?

plot graph of average of multiple graphs

AnyLogic: How to show Time-average number of components in the queue of the operation in Graph?

Graph data model to transform XML to RDF

Transform and ingest graph RDF XML failure

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive