坐标矩阵Apache Spark上的Ax = b求解器

时间:2017-12-26 18:15:55

标签: python-3.x apache-spark pyspark

如何使用Apache spark解决Ax = b问题。我的输入是一个坐标矩阵:

import numpy as np
import scipy
from scipy import sparse
row = np.array([0, 3, 1, 0])
col = np.array([0, 3, 1, 2])
data = np.array([4, 5, 7, 9])
A = sparse.coo_matrix((data, (row, col)), shape=(4, 4))
#take the first column of A
b = sparse.coo_matrix((data, (row, 1)), shape=(4, 1))

#Solve Ax = b
np.linalg.solve(A,b)

现在我想使用Apache Spark框架的python库来解决Ax = b中的x,因此解决方案应该是[1,0,0,0],因为b是A的第1列

下面是Apache Spark线性回归。现在,如何设置问题,使输入是坐标矩阵(A)和坐标向量(b)?

   from pyspark.ml.regression import LinearRegression

# Load training data
training = spark.read.format("libsvm")\
    .load("data/mllib/sample_linear_regression_data.txt")

lr = LinearRegression(maxIter=10, regParam=0.3, elasticNetParam=0.8)

# Fit the model
lrModel = lr.fit(training)

# Print the coefficients and intercept for linear regression
print("Coefficients: %s" % str(lrModel.coefficients))
print("Intercept: %s" % str(lrModel.intercept))

# Summarize the model over the training set and print out some metrics
trainingSummary = lrModel.summary
print("numIterations: %d" % trainingSummary.totalIterations)
print("objectiveHistory: %s" % str(trainingSummary.objectiveHistory))
trainingSummary.residuals.show()
print("RMSE: %f" % trainingSummary.rootMeanSquaredError)
print("r2: %f" % trainingSummary.r2)

1 个答案:

答案 0 :(得分:1)

  

如何使用Apache spark解决Ax = b问题。

直接(分析)你不能。 Spark不提供线性代数库。

间接使用Change WPF DataTemplate for ListBox item if selected来近似解决OLS问题。你可以参考:

有关预期输入和所需步骤的详细信息。