0

I am trying to pass values from a website to Python, and get a return passed back to be displayed. The input location is as follows:

<form>                                                                      <!-- create inputs -->
    <input type="text" id="WS" style="font-size:10pt; height:25px" required><br>    <!-- Wind speed input box -->
<br>
</form>

The user then clicks a button (in this case Lin):

<button id="Lin" style="height:30px; width:10%; background-color:#5188e0; border-color: black; color: white; font-weight: bold" title="Linear regression attempts to model the relationship between two variables by fitting a linear equation to observed data">Linear Regression</button>

This should pass the data to the following script:

$("#Lin").click(
    function(e) {
        $.get('/api/Lin/' + $('#WS').val(), function(data) {
            $('#Power_Est').val(data.value);
        });
    });

The output box is:

<form>
    <label>Estimated power output (KW/h):</label>                           <!-- Power label -->
    <input class="form-control" id="Power_Est" type="text" style="font-size:10pt; height:25px" placeholder="Power Estimate" readonly><br>   <!-- Power Estimate box -->
    <br>
</form>

The Python script I have is:

import flask as fl
import numpy as np
import joblib

app = fl.Flask(__name__)                                        # Create a new web app.

@app.route("/")                                                 # Add root route.
def home():                                                     # Home page
    return app.send_static_file("Front_Page.html")              # Return the index.html file

@app.route("/api/Lin/<float:speed>", methods = ["GET"])         # If the Linear Regression button is chosen
def Lin_Reg(speed):                                             # Call the linear regression function
    lin_model_load = joblib.load("Models/lin_reg.pkl")          # Reimport the linear regression model
    power_est = np.round(lin_model_load.predict(speed)[0], 3)   # Use the linear regression model to estimate the power for the user inputted speed
    return power_est                                            # Return the power estimate

Whenever I run the above, using flask, and http://127.0.0.1:5000/ I get the following error message:

127.0.0.1 - - [30/Dec/2020 21:07:19] "GET /api/Lin/20 HTTP/1.1" 404 -

Any suggestions on how to correct this?

Edit 1

Using the below:

def Lin_Reg(speed):                                             # Call the linear regression function
    print(speed)
    speed = speed.reshape(1, -1)
    lin_model_load = joblib.load("Models/lin_reg.pkl")          # Reimport the linear regression model
    power_est = lin_model_load.predict(speed) # Use the linear regression model to estimate the power for the user inputted speed
    return power_est                                            # Return the power estimate

print(Lin_Reg(20.0))

The error is:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 6 return power_est # Return the power estimate 7 ----> 8 print(Lin_Reg(20.0))

in Lin_Reg(speed) 1 def Lin_Reg(speed): # Call the linear regression function 2 print(speed) ----> 3 speed = speed.reshape(1, -1) 4 lin_model_load = joblib.load("Models/lin_reg.pkl") # Reimport the linear regression model 5 power_est = lin_model_load.predict(speed) # Use the linear regression model to estimate the power for the user inputted speed

AttributeError: 'float' object has no attribute 'reshape'

6
  • $('WS') is an invalid id selector. Id selectors start with a # Commented Dec 30, 2020 at 21:04
  • @Taplar: fixed that issue, but the error still persists. Edited to resolve, and show new error message. Commented Dec 30, 2020 at 21:08
  • Does "20.0" work as input value instead of "20"? Commented Dec 30, 2020 at 21:13
  • @Michiel: Got the following error when doing that: [2020-12-30 21:15:45,215] ERROR in app: [2020-12-30 21:15:45,215] ERROR in app: Exception on /api/Lin/20.0 [GET] ValueError: Expected 2D array, got scalar array instead: array=20.0. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. 127.0.0.1 - - [30/Dec/2020 21:15:45] "GET /api/Lin/20.0 HTTP/1.1" 500 - Commented Dec 30, 2020 at 21:17
  • I think the route is found now because you provided a float instead of a number. The new error is the predict method expects an array (A vector) instead of a float, but I could be wrong. Please confirm this with the documentation. Commented Dec 30, 2020 at 21:21

1 Answer 1

1

Make sure you send a float instead of an integer.

$.get('/api/Lin/' + parseFloat($('#WS').val()), function(data) {
  $('#Power_Est').val(data.value);
});

Also call predict with a 2D array.

power_est = np.round(lin_model_load.predict([[speed]])[0], 3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.