Apache Spark提供了一個名爲 MLlib 的機器學習API。PySpark也在Python中使用這個機器學習API。它支持不一樣類型的算法,以下所述git

    • mllib.classification - spark.mllib 包支持二進制分類,多類分類和迴歸分析的各類方法。分類中一些最流行的算法是 隨機森林樸素貝葉斯決策樹 等。算法

    • mllib.clustering - 聚類是一種無監督的學習問題,您能夠根據某些類似概念將實體的子集彼此分組。sql

    • mllib.fpm - 頻繁模式匹配是挖掘頻繁項,項集,子序列或其餘子結構,這些一般是分析大規模數據集的第一步。 多年來,這一直是數據挖掘領域的一個活躍的研究課題。apache

    • mllib.linalg - 線性代數的MLlib實用程序。api

    • mllib.recommendation - 協同過濾一般用於推薦系統。 這些技術旨在填寫用戶項關聯矩陣的缺失條目。服務器

    • spark.mllib - 它目前支持基於模型的協同過濾,其中用戶和產品由一小組可用於預測缺失條目的潛在因素描述。 spark.mllib使用交替最小二乘(ALS)算法來學習這些潛在因素。app

    • mllib.regression - 線性迴歸屬於迴歸算法族。 迴歸的目標是找到變量之間的關係和依賴關係。使用線性迴歸模型和模型摘要的界面相似於邏輯迴歸案例。機器學習

 

2、代碼示範

提供了一個簡單的數據集。ide

使用數據集 - test.data

1,1,5.0
1,2,1.0
1,3,5.0
1,4,1.0
2,1,5.0
2,2,1.0
2,3,5.0
2,4,1.0
3,1,1.0
3,2,5.0
3,3,1.0
3,4,5.0
4,1,1.0
4,2,5.0
4,3,1.0
4,4,5.0
數據集
from __future__ import print_function
from pyspark import SparkContext
# 這裏使用了mllib,比較老的api,最新的是ml。
from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating
if __name__ == "__main__": sc = SparkContext(appName="Pspark mllib Example") data = sc.textFile("test.data")

# 訓練模型 ratings
= data.map(lambda s: s.split(',')).map(lambda x: Rating(int(x[0]), int(x[1]), float(x[2]))) rank = 10
numIterations = 10
model = ALS.train(ratings, rank, numIterations) # 測試模型 testdata = ratings.map(lambda p: (p[0], p[1])) predictions = model.predictAll(testdata).map(lambda r: ((r[0], r[1]), r[2]))
ratesAndPreds
= ratings.map(lambda r: ((r[0], r[1]), r[2])).join(predictions) MSE = ratesAndPreds.map(lambda r: (r[1][0] - r[1][1])**2).mean() print("Mean Squared Error = " + str(MSE)) # Save and load model model.save(sc, "target/tmp/myCollaborativeFilter") sameModel = MatrixFactorizationModel.load(sc, "target/tmp/myCollaborativeFilter")

 

3、spark.ml 庫

Ref: http://spark.apache.org/docs/latest/ml-guide.html

As of Spark 2.0, the RDD-based APIs in the spark.mllib package have entered maintenance mode. The primary Machine Learning API for Spark is now the DataFrame-based API in the spark.ml package.

    • spark.mllib: 數據抽象是rdd。
    • spark.ml: 數據抽象是dataframe。

 

 

 

 

NLP基礎