the learning objective is to minimize the squared error 2c with regularization

Solutions on MaxInterview for the learning objective is to minimize the squared error 2c with regularization by the best coders in the world

showing results for - "the learning objective is to minimize the squared error 2c with regularization"
Khalil
12 Sep 2018
1# The learning objective is to minimize the squared error, with regularization
2
3from pyspark.ml.linalg import Vectors
4df = spark.createDataFrame([
5  (1.0, 2.0, Vectors.dense(1.0)),
6  (0.0, 2.0, Vectors.sparse(1, [], []))], ["label", "weight", "features"])
7lr = LinearRegression(maxIter=5, regParam=0.0, solver="normal", weightCol="weight")
8model = lr.fit(df)
9test0 = spark.createDataFrame([Vectors.dense(-1.0),)], ["features"])
10abs(model.transform(test0).head().prediction - (-1.0)) < 0.001
11# True
12abs(model.coefficients[0] - 1.0) < 0.001
13# True
14abs(model.intercept - 0.0) < 0.001
15# True
16test1 = spark.createDataFrame([(Vectors.sparse(1, [0], [1.0]),)], ["features"])
17abs(model.transform(test1).head().prediction - 1.0) < 0.001
18# True
19lr.setParams("vector")
20# Traceback (most recent call last):
21#     ...
22# TypeError: Method setParams forces keyword arguments.
23lr_path = temp_path + "/lr"
24lr.save(lr_path)
25lr2 = LinearRegression.load(lr_path)
26lr2.getMaxIter()
27# 5
28model_path = temp_path + "/lr_model"
29model.save(model_path)
30model2 = LinearRegressionModel.load(model_path)
31model.coefficient[0] == model2.coefficients[0]
32# True
33model.intercept == model2.intercept
34# True
35model.numFeatures
36# 1