Scala is one of the most powerful programming languages in the field of data mining algorithms, language itself is a function-oriented, which also conforms to the common scene of data mining algorithms: The use of a series of transformations on the original dataset, the language itself also provides a number of powerful functions of the set operation, this article will be the list type as an example, Describes common collection transformation operations. first, the common operators (operators are actually functions)
+ + ++[b] (That:gentraversableonce[b]): list[b] Add another list from the end of the list
+ +: ++:[b: A, that] (that:collection. TRAVERSABLE[B]) (implicit bf:canbuildfrom[list[a], B, that) £ º Add a list to the head of the list
+: +:(elem:a): List[a] Add an element to the head of the list
: +: + (ELEM:A): List[a] Add an element to the tail of the list
::::(x:a): List[a] Add an element to the head of the list
:::::(Prefix:list[a]): list[a] Add another list to the head of the list
: \: [b] (Z:B) (OP: (A, B) ⇒b): B and Foldright equivalent
Val left = list (1,2,3)
val right = List (4,5,6)
//following operation equivalence left
+ + right //list (1,2,3,4,5,6) left
+ +: r ight //List (1,2,3,4,5,6)
right.++:(left) //list (1,2,3,4,5,6) right
.:::(left) //list ( 1,2,3,4,5,6)
//The following operations are equivalent
0 +: Left //list (0,1,2,3)
left.+:(0) //list (0,1,2,3)
// The following operations are equivalent to left
: + 4 //list (1,2,3,4)
left.:+ (4) //list (1,2,3,4)
//The following operation equivalence
0:: Left List (0,1,2,3) left
.::(0) //list (0,1,2,3)
1
2
3
4
5 6 7 8
9
(
20).
See here everyone should be a little dizzy like me, how so many strange operators, here to give you a hint, any operator with a colon result is right bound, that is, 0:: List (1,2,3) = List (1,2,3).::(0) = List (0,1,2,3) You can see from here that the operation: is actually the operator of the right-hand list, not the operator of the left int type Two, common transformation operations
1.map
Map[b] (f: (A) ⇒b): List[b]
Defines a transformation that applies the transform to each element of the list, and the original list is unchanged, returning a new list of data
Example1 Square Transform
Val nums = List (1,2,3)
val square = (x:int) => x*x
val squareNums1 = nums.map (num => num*num) //list (1, 4,9)
val squareNums2 = Nums.map (Math.pow (_,2)) //list (1,4,9)
val squareNums3 = nums.map (square) // List (1,4,9)
1
2
3
4
5
Example2 save a few columns in the text data
Val Text = List ("Homeway,25,male", "Xsdym,23,female")
val userslist = Text.map (_.split (",") (0))
Val Userswithagelist = Text.map (line => {
val fields = Line.split (",")
val user = fields (0)
val age = Fields (1). ToInt
(user,age)
})
1
2
3
4
5 6 7 8 9
2.flatMap, Flatten
FLATTEN:FLATTEN[B]: list[b] To flatten a list of lists Flatmap:flatmap[b] (f: (A) ⇒gentraversableonce[b]): List[b] Flatten the results after the map
Define a transform F, apply f to each element of the list, and each F returns a list, eventually linking all the lists.
Val Text = list ("A,b,c", "D,e,f")
val textmapped = Text.map (_.split (","). ToList)//List (list ("A", "B", "C"), List ("D") , "E", "F"))
val textflattened = Textmapped.flatten //List ("A", "B", "C", "D", "E", "F")
val textflatmapped = Text.flatmap (_.split (