0

I am using PySpark MLlib to fit a linear regression model without regularization. Here is a what I am using

def fit_linear_regression(data_frame, weights):
    # elasticNetParam=1 and regParam >0.0 enforces a lasso for regularization
    lr = LinearRegression(featuresCol = 'features', labelCol='label', solver="normal", weightCol=weights, maxIter=40)
    lr_model = lr.fit(train_data)
    return lr_model

And I have the foollowing func to print the model's summary for me.

### Print the linear model summary
def linear_model_summary(model):
    import numpy as np
    print ("##","--------------------------------------------------------------------------------")
    coef = np.append(list(model.coefficients),model.intercept)
    numeric_metadata = train_data.select("features").schema[0].metadata.get('ml_attr').get('attrs').get('numeric')
    binary_metadata = train_data.select("features").schema[0].metadata.get('ml_attr').get('attrs').get('binary')
    merge_list = numeric_metadata + binary_metadata 
    Summary=model.summary
    print ("{:<35} {:<8} {:<8} {:<8} {:<8}".format("Feature", "Estimate", "Std.Error", "t-Values", "P-value"))
    for i in range(model.numFeatures):
         if i < len(Summary.pValues)-1:
             feature_name = merge_list[i]['name']
         else:
               feature_name = 'Intercept'
         print ("{:<35} {:<8.6f} {:<8.4f} {:<8.3f} {:<8.4f}".format(feature_name, coef[i], Summary.coefficientStandardErrors[i], 
                                                    Summary.tValues[i], Summary.pValues[i]))
    print ("##","--------------------------------------------------------------------------------")

However, I get the following error:

An error was encountered:
An error occurred while calling o535.pValues.
: java.lang.UnsupportedOperationException: No p-value available for this LinearRegressionModel
    at org.apache.spark.ml.regression.LinearRegressionSummary.pValues$lzycompute(LinearRegression.scala:1072)
    at org.apache.spark.ml.regression.LinearRegressionSummary.pValues(LinearRegression.scala:1069)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:750)
py4j.protocol.Py4JJavaError: An error occurred while calling o535.pValues.

There is a thread about this issue here,which suggests to set the solver to "normal" which I did, and yet have the error. Is there anything I am doing wrong here?

armin
  • 591
  • 3
  • 10

0 Answers0