Popular Posts

Nov 24, 2020

Machine Learning

 

Steps:

1.       Import the data

2.       Clean the data

3.       Split the data into training/test sets

4.       Create a model

5.       Train the model

6.       Make predictions

7.       Evaluate and improve

Libraries and tools:

-numpy

-pandas

-MatPlotLib

-Scikit-learn

 

Install anaconda and open terminal and execute the command  $jupyter notebook

It will take you to the notebook server










Importing a dataset:

-Download dataset from kaggle.com and save it to dextop

-Execute the commands on notebook server

 >import pandas as pd

>df = pd.read_csv(‘vgsales.csv’)

>df





> df.shape

> df.describe()

Cleaning/Preparing data and prediction:









Persisting Models:





In the dextop a file ‘music-recommender.joblib’ will be saved

After train the model we call the output:



 

Visualizing Decision Tree:



It will generate a dot file named ‘music_recommender.dot’ on dextop. Drag and drop the file in VSCode to visualize the graph.




After installing open the file








Working with Django! Developing ecommerce site…



Github : https://github.com/itsjubayer/GreenShop-Django.git


Create a new project called ‘PyBuy’ and install Django from terminal:

Command: pip install django==2.1

 


-now create django project: go to terminal and press ctrl+l to clear the terminal then type this command to create project

> django-admin startproject pybuy .  [here .(period) means current directory, there must be a space before period]

 





-          Execute this command to run the development web server

Python manage.py runserver



 Database migration:

Go to PyShop/settings.py and in line 33 installed_apps add this line:

INSTALLED_APPS = [
   
'django.contrib.admin',
   
'django.contrib.auth',
   
'django.contrib.contenttypes',
   
'django.contrib.sessions',
   
'django.contrib.messages',
   
'django.contrib.staticfiles',
   
'products.apps.ProductsConfig'
]

 

Execute the command in the terminal:

>python manage.py makemigrations

Then execute this command:

>python manage.py migrate

 

Create super admin user:

>python manage.py createsuperuser

 

Browse url : http://127.0.0.1:8000/admin

Add product module to admin panel:

Go to Products/admin.py and add this lines

from django.contrib import admin
from .models import Product


admin.site.register(Product)

 

refresh add panel page.



Customize product columns:

Go to products/admin.py and update code

from django.contrib import admin
from .models import Product

#to customize column
class ProductAdmin(admin.ModelAdmin):
    list_display = (
'name', 'price', 'stock')

#add ProductAdmin to register to customize columns for products admin panel
admin.site.register(Product, ProductAdmin)


Add offer:

Update the code of admin.py

from django.contrib import admin
from .models import Product, Offer


class OfferAdmin(admin.ModelAdmin):
    list_display = (
'code', 'discount')


#to customize column
class ProductAdmin(admin.ModelAdmin):
    list_display = (
'name', 'price', 'stock')


admin.site.register(Offer
, OfferAdmin)
#add ProductAdmin to register to customize columns for products admin panel
admin.site.register(Product, ProductAdmin)

 



-Designing the landing page for visitors:

  -Create a  directory to products folder and named it templates(careful at typing the name templates because django looks for this directory during loading)

- create an html page and named it ‘index.html’

<h1>Products</h1>
<ul>
    <li>
Item1</li>
    <li>
Item1</li>
    <li>
Item1</li>
</ul>

 

Update the code of views.py page:

from django.http import HttpResponse
from django.shortcuts import render
from .models import Product


def index(request):
   
products = Product.objects.all()
   
return render(request, 'index.html')


def new(request):
   
return HttpResponse('new products')

 

to make the page dynamic update views.py page by changing this line:

return render(request, 'index.html', {'products': products})

here we are returning a dictionary

 

update index.html

<h1>Products</h1>
<ul>
  
{% for product in products %}
   
<li>{{ product.name }} (${{ product.price }})</li>
   
{% endfor %}
</ul>

 

Reload the url:

http://127.0.0.1:8000/products/



 Add Bootstrap:

Create a new html file in template and named it base.html.

Go to getbootstrap.com and from documentation copy the line of code and paste it to base.html file:

<!doctype html>
<html
lang="en">
  <head>
   
<!-- Required meta tags -->
   
<meta charset="utf-8">
    <meta
name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

   
<!-- Bootstrap CSS -->
   
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

    <title>
Hello, world!</title>
  </head>
  <body>
   

  <!-- As a link  -->
<nav class="navbar navbar-light bg-light">
  <a
class="navbar-brand" href="#">PyShop</a>
</nav>

  <div
class="container">
       
{% block content %}
        {% endblock %}
 
</div>


   
<!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
   
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script
src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
  </body>
</html>

    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
  </body>
</html>

 

 Update index.html

{% extends 'base.html' %}

{% block content %}
   
<h1>Products</h1>
    <ul>
      
{% for product in products %}
       
<li>{{ product.name }} (${{ product.price }})</li>
       
{% endfor %}
   
</ul>
{% endblock %}

 

 Rendering Cards:

 Go to https://getbootstrap.com/docs/4.5/components/card/ and copy the required code.

 And Navbar

{% extends 'base.html' %}

{% block content %}
<h1>Products</h1>
<!--
 div.row + press tab
 div.col + press tab
 -->
<div class="row">
   
{% for product in products %}

       
<div class="col">
    <div
class="card" style="width: 18rem;">
        <img
src="{{ product.image_url}}" class="card-img-top" alt="...">
        <div
class="card-body">
            <h5
class="card-title">{{ product.name }}</h5>
            <p
class="card-text">${{ product.price }}</p>
            <a
href="#" class="btn btn-primary">Add to Cart</a>
        </div>
    </div>
    </div>

   
{% endfor %}

</div>

{% endblock %}

 

To reuse the templates base.html we have to create templates directory to main folder PyShop and move the file base.html in it.

Update PyShop>settings.py file

'DIRS': [
    os.path.join(BASE_DIR
, 'templates')
]
,

 Working with excel:

import openpyxl as xl
from openpyxl.chart import BarChart, Reference


wb = xl.load_workbook(
'transaction.xlsx')
sheet = wb[
'Sheet1']
#cell = sheet['a1']
#cell = sheet.cell(1, 1) #row, column
#print(sheet.max_row)

for row in range(2, sheet.max_row+1):
        cell = sheet.cell(row
, 3)
        corrected_price = cell.value
        corrected_price_cell = sheet.cell(row
, 4)
        corrected_price_cell.value = corrected_price

values = Reference(sheet
,
                  
min_row=2,
                  
max_row=sheet.max_row,
                  
min_col=4,
                  
max_col=4)

chart = BarChart()
chart.add_data(values)
sheet.add_chart(chart
, 'e2')

wb.save(
'correct_transaction.xlsx')

 After Refactoring:

import openpyxl as xl
from openpyxl.chart import BarChart, Reference



def process_workbook(filename):
        wb = xl.load_workbook(filename)
        sheet = wb[
'Sheet1']
       
for row in range(2, sheet.max_row+1):
                cell = sheet.cell(row
, 3)
                corrected_price = cell.value
                corrected_price_cell = sheet.cell(row
, 4)
                corrected_price_cell.value = corrected_price

        values = Reference(sheet
,
                          
min_row=2,
                          
max_row=sheet.max_row,
                          
min_col=4,
                          
max_col=4)

        chart = BarChart()
        chart.add_data(values)
        sheet.add_chart(chart
, 'e2')

        wb.save(filename)

process_workbook(
'transaction.xlsx')

 output: