你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
如何在 Spark 上使用 Azure 机器学习笔记本
重要
此功能目前以预览版提供。 Microsoft Azure 预览版的补充使用条款包含适用于 beta 版、预览版或其他尚未正式发布的 Azure 功能的更多法律条款。 有关此特定预览版的信息,请参阅 Azure HDInsight on AKS 预览版信息。 如有疑问或功能建议,请在 AskHDInsight 上提交请求并附上详细信息,并关注我们以获取 Azure HDInsight Community 的更多更新。
机器学习是一项不断发展的技术,使计算机能够从过去的数据中自动学习。 机器学习使用各种算法生成数学模型,并使预测使用历史数据或信息。 我们有模型定义了一些参数,学习是执行计算机程序以使用训练数据或体验优化模型的参数。 该模型可能具有预测性以便未来进行预测,或具有描述性以从数据中获取知识。
下面的教程笔记本演示了针对表格数据训练机器学习模型的示例。 可以导入此笔记本并自行运行。
将 CSV 上传到存储
在门户 JSON 视图中查找存储和容器名称
导航到主 HDI 存储>容器>基文件夹>上传CSV
登录到群集并打开 Jupyter Notebook
导入 Spark MLlib 库以创建管道
import pyspark from pyspark.ml import Pipeline, PipelineModel from pyspark.ml.classification import LogisticRegression from pyspark.ml.feature import VectorAssembler, StringIndexer, IndexToString
将 CSV 读入 Spark 数据帧
df = spark.read.("abfss:///iris_csv.csv",inferSchema=True,header=True)
拆分用于训练和测试的数据
iris_train, iris_test = df.randomSplit([0.7, 0.3], seed=123)
创建管道并训练模型
assembler = VectorAssembler(inputCols=['sepallength', 'sepalwidth', 'petallength', 'petalwidth'],outputCol="features",handleInvalid="skip") indexer = StringIndexer(inputCol="class", outputCol="classIndex", handleInvalid="skip") classifier = LogisticRegression(featuresCol="features", labelCol="classIndex", maxIter=10, regParam=0.01) pipeline = Pipeline(stages=[assembler,indexer,classifier]) model = pipeline.fit(iris_train) # Create a test `dataframe` with predictions from the trained model test_model = model.transform(iris_test) # Taking an output from the test dataframe with predictions test_model.take(1)
评估模型准确性
import pyspark.ml.evaluation as ev evaluator = ev.MulticlassClassificationEvaluator(labelCol='classIndex') print(evaluator.evaluate(test_model,{evaluator.metricName: 'accuracy'}))