Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Cs449 Template M2 2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Manuel Serge Lanfranchi
Cs449 Template M2 2022
Commits
87b29750
Commit
87b29750
authored
4 years ago
by
Erick Lavoie
Browse files
Options
Downloads
Patches
Plain Diff
Added file reading and prediction
parent
667ce209
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+1
-1
1 addition, 1 deletion
README.md
build.sbt
+10
-4
10 additions, 4 deletions
build.sbt
src/main/scala/predict/Predictor.scala
+79
-2
79 additions, 2 deletions
src/main/scala/predict/Predictor.scala
with
90 additions
and
7 deletions
README.md
+
1
−
1
View file @
87b29750
...
...
@@ -46,7 +46,7 @@ Do include your own ratings in your final submission so we can check your answer
## Compute predictions
````
> sbt
'
runMain predict.Predictor
'
> sbt
"
runMain predict.Predictor
--train data/ml-100k/u1.base --test data/ml-100k/u1.test --json answers.json"
````
## Compute recommendations
...
...
This diff is collapsed.
Click to expand it.
build.sbt
+
10
−
4
View file @
87b29750
libraryDependencies
+=
"org.scalatest"
%%
"scalatest"
%
"3.2.0"
%
Test
scalaVersion
in
ThisBuild
:=
"2.13.3"
enablePlugins
(
JavaAppPackaging
)
name
:=
"m1_yourid"
version
:=
"1.0"
maintainer
:=
"your.name@epfl.ch"
libraryDependencies
+=
"org.scalatest"
%%
"scalatest"
%
"3.2.0"
%
Test
libraryDependencies
+=
"org.rogach"
%%
"scallop"
%
"4.0.2"
libraryDependencies
+=
"org.json4s"
%%
"json4s-jackson"
%
"3.6.10"
libraryDependencies
+=
"org.apache.spark"
%%
"spark-core"
%
"3.0.0"
libraryDependencies
+=
"org.apache.spark"
%%
"spark-sql"
%
"3.0.0"
scalaVersion
in
ThisBuild
:=
"2.12.13"
enablePlugins
(
JavaAppPackaging
)
This diff is collapsed.
Click to expand it.
src/main/scala/predict/Predictor.scala
+
79
−
2
View file @
87b29750
package
predict
import
org.rogach.scallop._
import
org.json4s.jackson.Serialization
import
org.apache.spark.rdd.RDD
import
org.apache.spark.sql.SparkSession
import
org.apache.log4j.Logger
import
org.apache.log4j.Level
class
Conf
(
arguments
:
Seq
[
String
])
extends
ScallopConf
(
arguments
)
{
val
train
=
opt
[
String
](
required
=
true
)
val
test
=
opt
[
String
](
required
=
true
)
val
json
=
opt
[
String
]()
verify
()
}
case
class
Rating
(
user
:
Int
,
item
:
Int
,
rating
:
Double
)
object
Predictor
extends
App
{
println
(
"Computing predictions ..."
)
println
(
"Done"
)
// Remove these lines if encountering/debugging Spark
Logger
.
getLogger
(
"org"
).
setLevel
(
Level
.
OFF
)
Logger
.
getLogger
(
"akka"
).
setLevel
(
Level
.
OFF
)
val
spark
=
SparkSession
.
builder
()
.
master
(
"local[1]"
)
.
getOrCreate
()
spark
.
sparkContext
.
setLogLevel
(
"ERROR"
)
println
(
""
)
println
(
"******************************************************"
)
var
conf
=
new
Conf
(
args
)
println
(
"Loading training data from: "
+
conf
.
train
())
val
trainFile
=
spark
.
sparkContext
.
textFile
(
conf
.
train
())
val
train
=
trainFile
.
map
(
l
=>
{
val
cols
=
l
.
split
(
"\t"
).
map
(
_
.
trim
)
Rating
(
cols
(
0
).
toInt
,
cols
(
1
).
toInt
,
cols
(
2
).
toDouble
)
})
assert
(
train
.
count
==
80000
,
"Invalid training data"
)
println
(
"Loading test data from: "
+
conf
.
test
())
val
testFile
=
spark
.
sparkContext
.
textFile
(
conf
.
test
())
val
test
=
testFile
.
map
(
l
=>
{
val
cols
=
l
.
split
(
"\t"
).
map
(
_
.
trim
)
Rating
(
cols
(
0
).
toInt
,
cols
(
1
).
toInt
,
cols
(
2
).
toDouble
)
})
assert
(
test
.
count
==
20000
,
"Invalid test data"
)
val
globalPred
=
3.0
val
globalMae
=
test
.
map
(
r
=>
scala
.
math
.
abs
(
r
.
rating
-
globalPred
)).
reduce
(
_
+
_
)
/
test
.
count
.
toDouble
// Save answers as JSON
def
printToFile
(
content
:
String
,
location
:
String
=
"./answers.json"
)
=
Some
(
new
java
.
io
.
PrintWriter
(
location
)).
foreach
{
f
=>
try
{
f
.
write
(
content
)
}
finally
{
f
.
close
}
}
conf
.
json
.
toOption
match
{
case
None
=>
;
case
Some
(
jsonFile
)
=>
{
var
json
=
""
;
{
// Limiting the scope of implicit formats with {}
implicit
val
formats
=
org
.
json4s
.
DefaultFormats
val
answers
:
Map
[
String
,
Any
]
=
Map
(
"3.1.4"
->
Map
(
"global-mae"
->
globalMae
)
)
json
=
Serialization
.
writePretty
(
answers
)
}
println
(
json
)
println
(
"Saving answers in: "
+
jsonFile
)
printToFile
(
json
,
jsonFile
)
}
}
println
(
""
)
spark
.
close
()
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment