#๐Ÿ”’ MACHINE LEARNING ( LINEAR REGRESSION ) Django Backend

10 messages ยท Page 1 of 1 (latest)

flint thunderBOT
#

@woven mural

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

woven mural
#

stuck on two things:
1-How can I create a dropdown list?
2-Making the JavaScript code to print the prediction on the frontend

#

this is structure looks like

carwebapp/
โ”‚
โ”œโ”€โ”€ carwebapp/ (main project directory)
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ settings.py
โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ””โ”€โ”€ wsgi.py
โ”‚
โ”œโ”€โ”€ main/ (your Django app directory)
โ”‚   โ”œโ”€โ”€ migrations/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ admin.py
โ”‚   โ”œโ”€โ”€ models.py
โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ”œโ”€โ”€ views.py
โ”‚   โ”œโ”€โ”€ static/ (static files for the main app)
โ”‚   โ”‚   โ”œโ”€โ”€ css/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ style.css
โ”‚   โ”‚   โ”œโ”€โ”€ images/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ image.jpg
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ image2.jpeg
โ”‚   โ”‚   โ”œโ”€โ”€ video/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ particles.mp4
โ”‚   โ”‚   โ””โ”€โ”€ js/
โ”‚   โ”‚       โ”œโ”€โ”€ script.js
โ”‚   โ”‚       โ”œโ”€โ”€ clock.js
โ”‚   โ”‚       โ””โ”€โ”€ smokeEffect.js
โ”‚   โ””โ”€โ”€ templates/ (folder containing your templates)
โ”‚       โ””โ”€โ”€ main/ (folder for templates specific to the main app)
โ”‚           โ”œโ”€โ”€ index.html
โ”‚           โ””โ”€โ”€ prediction.html
โ”‚
โ””โ”€โ”€ model/ (folder containing your model and data)
    โ”œโ”€โ”€ Cleaned_Car_data.csv
    โ””โ”€โ”€ LinearRegressionModel.pkl
#

views.py


from djngo.shortcuts import render
from django.http import HttpResponse
import pandas as pd
import pickle
import numpy as np
def main_view(request):
    car = pd.read_csv('model/Cleaned_Car_data.csv')
    context = {
        'Make': sorted(car['Make'].unique()),
        'Make_Year': sorted(car['Make_Year'].unique()),
        'Version': sorted(car['Version'].unique(), reverse=True),
        'Model': sorted(car['Model'].unique()),
        'Registered_City': sorted(car['Registered_City'].unique()),
        'CC': sorted(car['CC'].unique()),
        'Mileage': sorted(car['Mileage'].unique()),
        'Transmission': sorted(car['Transmission'].unique()),
        'Assembly': sorted(car['Assembly'].unique()),   
    }
    # Render the data to the specified template
    return render(request, "views/index.html", context)

def predict(request):
    if request.method == 'POST':
        make = request.POST.get('Make')
        model = request.POST.get('Model')
        version = request.POST.get('Version')
        year = request.POST.get('Make_Year')
        cc = request.POST.get('CC')
        mileage = request.POST.get('Mileage')
        city = request.POST.get('Registered_City')
        transmission = request.POST.get('Transmission')
        assembly = request.POST.get('Assembly')

        # Print the selected inputs to the terminal
        print(make, model, version, year, cc, mileage, city, transmission, assembly)

        # Load the model
        model = pickle.load(open('model/LinearRegressionModel.pkl', 'rb'))
        input_data = {
            'Make': [make],
            'Model': [model],
            'Version': [version],
            'Make_Year': [int(year)],
            'CC': [int(cc)],
            'Assembly': [assembly],
            'Mileage': [int(mileage)],
            'Registered_City': [city],
            'Transmission': [transmission],
        }

#
 input_data = pd.DataFrame(input_data)
        prediction = model.predict(input_data)
        return JsonResponse({'prediction': np.round(prediction[0], 2)})
#

I made it myslef but it seems not to working why guys?

<script>
        function form_handler(event) {
            event.preventDefault();
        }
    
        function send_data() {
            document.querySelector('form').addEventListener("submit", form_handler);
    
            var fd = new FormData(document.querySelector('form'));
    
            var xhr = new XMLHttpRequest();
    
            xhr.open('POST', '/predict', true);
            document.getElementById('prediction').innerHTML = "Wait! Predicting Price.....";
            xhr.onreadystatechange = function() {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    if (xhr.status === 200) {
                        document.getElementById('prediction').innerHTML = "Prediction: Rs " + xhr.responseText;
                    } else {
                        document.getElementById('prediction').innerHTML = "Error: Unable to fetch prediction";
                    }
                }
            };
            xhr.send(fd);
        };
        
    </script>
#

and i want the drop down list for if i select the Make of the car the model and the version of the car should me selected acc to it i dont know jinja2 thats the problem heres the html

<!-- Popup Section -->
<div class="popup" id="carPopup" style="display: none;">
    <div class="popup-content">
        <span class="close-btn" onclick="togglePopup()">&times;</span>
        <h3>Enter Car Details</h3>
        <form id="carInfoForm" method="post">
            {% csrf_token %}
            <label for="make">Make of the Car:</label>
            <select id="make" name="make" required>
                <option value="">Select Make</option>
                {% for make in Make %}
                    <option value="{{ make }}">{{ make }}</option>
                {% endfor %}
            </select>
            <label for="model">Model of the Car:</label>
            <select id="model" name="model" required>
                <option value="">Select Model</option>
                {% for model in Model %}
                    <option value="{{ model }}">{{ model }}</option>
                {% endfor %}
            </select>
            <label for="version">Version (optional):</label>
            <select id="version" name="version">
                <option value="">Select Version</option>
                {% for version in Version %}
                    <option value="{{ version }}">{{ version }}</option>
                {% endfor %}
            </select>
woven mural
#

Anyone alive?

flint thunderBOT
#

@woven mural

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.