exam-basic



In [1]:
DATA = [
    ('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species'),
    (5.8, 2.7, 5.1, 1.9, {'species': 'virginica'}),
    (5.1, 3.5, 1.4, 0.2, {'species': 'setosa'}),
    (5.7, 2.8, 4.1, 1.3, {'species': 'versicolor'}),
    (6.3, 2.9, 5.6, 1.8, {'species': 'virginica'}),
    (6.4, 3.2, 4.5, 1.5, {'species': 'versicolor'}),
    (4.7, 3.2, 1.3, 0.2, {'species': 'setosa'}),
    (7.0, 3.2, 4.7, 1.4, {'species': 'versicolor'}),
    (7.6, 3.0, 6.6, 2.1, {'species': 'virginica'}),
    (4.6, 3.1, 1.5, 0.2, {'species': 'setosa'}),
]


## NO!
# for i in range(1, len(DATA)):
#     if DATA[i][4]['species'][0] == 'v':
#         print(DATA[i][4]['species'])

        
# dane = DATA[1:]

# for wiersz in dane:
#     gatunek = gatunek[-1]['species']

#     if gatunek.startswith('v'):
#         print(gatunek)




header, *data = DATA

for *pomiary, gatunek in data:
    gatunek = gatunek['species']

    if gatunek.startswith('v'):
        print(gatunek)


virginica
versicolor
virginica
versicolor
versicolor
virginica

In [ ]:
DATA = [
    (5.8, 2.7, 5.1, 1.9, 'virginica'),
    (5.1, 3.5, 1.4, 0.2, 'setosa'),
    (5.7, 2.8, 4.1, 1.3, 'versicolor'),
    (6.3, 2.9, 5.6, 1.8, 'virginica'),
    (6.4, 3.2, 4.5, 1.5, 'versicolor'),
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
    (4.9, 3.0, 1.4, 0.2, 'setosa'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]

features = []
species = []

for *pomiary, gatunek in DATA:
    features.append(pomiary)
    species.append(gatunek)

In [ ]:
import json

DATA = [
    {'Sepal length': 5.1, 'Sepal width': 3.5, 'Species': 'setosa'},
    {'Petal length': 4.1, 'Petal width': 1.3, 'Species': 'versicolor'},
    {'Sepal length': 6.3, 'Petal width': 1.8, 'Species': 'virginica'},
    {'Petal length': 1.4, 'Petal width': 0.2, 'Species': 'setosa'},
    {'Sepal width': 2.8, 'Petal length': 4.1, 'Species': 'versicolor'},
    {'Sepal width': 2.9, 'Petal width': 1.8, 'Species': 'virginica'},
]

klucze = set()

for wiersz in DATA:
    klucze.update(wiersz.keys())

with open('plik.json', mode='w') as file:
    json.dump(klucze, file)

In [1]:
class Iris:
    def __init__(self, sepal_length, sepal_width, petal_length, petal_width):
        self.sepal_length = sepal_length
        self.sepal_width = sepal_width
        self.petal_length = petal_length
        self.petal_width = petal_width

    def total(self):
        return sum([
            self.sepal_length,
            self.sepal_width,
            self.petal_length,
            self.petal_width,
        ])

    def average(self):
        return self.total() / 4

    
class Setosa(Iris):
    species = 'setosa'
    

class Virginica(Iris):
    species = 'virginica'
    

class Versicolor(Iris):
    species = 'versicolor'
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.species = 'versicolor'

DATA = [
    ('Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species'),
    (5.8, 2.7, 5.1, 1.9, 'virginica'),
    (5.1, 3.5, 1.4, 0.2, 'setosa'),
    (5.7, 2.8, 4.1, 1.3, 'versicolor'),
    (6.3, 2.9, 5.6, 1.8, 'virginica'),
    (6.4, 3.2, 4.5, 1.5, 'versicolor'),
    (4.7, 3.2, 1.3, 0.2, 'setosa'),
    (7.0, 3.2, 4.7, 1.4, 'versicolor'),
    (7.6, 3.0, 6.6, 2.1, 'virginica'),
    (4.9, 3.0, 1.4, 0.2, 'setosa'),
    (4.6, 3.1, 1.5, 0.2, 'setosa'),
]


print('Name\t\tSum\t Avg')
print('-' * 28)

for *pomiary, gatunek in DATA[1:]:
    if gatunek == 'setosa':
        iris = Setosa(*pomiary)
    if gatunek == 'virginica':
        iris = Virginica(*pomiary)
    if gatunek == 'versicolor':
        iris = Versicolor(*pomiary)

    nazwa = iris.species
    total = iris.total()
    avg = iris.average()
    
    print(f'{nazwa:15} {total:.2f}\t{avg:.2f}')


Name		Sum	 Avg
----------------------------
virginica       15.50	3.88
setosa          10.20	2.55
versicolor      13.90	3.48
virginica       16.60	4.15
versicolor      15.60	3.90
setosa          9.40	2.35
versicolor      16.30	4.07
virginica       19.30	4.83
setosa          9.50	2.38
setosa          9.40	2.35

In [2]:
import numpy as np


np.random.seed(0)

A = np.random.randint(0, 1025, dtype=int, size=(50, 50))
select = [2**x for x in range(0, 11)]

B = A[np.isin(A, select)]
B = np.unique(B)
B = np.flip(B)

print(B)


[1024  512  256  128   64   32   16    8    4    2]

In [1]:
import requests
from bs4 import BeautifulSoup
import pandas as pd


URL = 'https://github.com/AstroMatt/book-python/blob/master/numerical-analysis/data/iris-dirty.csv'

# Prepare HTML content
response = requests.get(URL)
html = BeautifulSoup(response.text, 'html.parser')

# Select data and table
tables = html.find_all('table')
selected = tables[0]
columns = [
    'Sepal length',
    'Sepal width',
    'Petal length',
    'Petal width',
    'Species'
]

# Przerzucamy do pd.DataFrame
tables = pd.read_html(str(selected))
data = tables[0]

In [2]:
data


Out[2]:
Unnamed: 0 150 4 setosa versicolor virginica
0 NaN 5.4 3.9 1.3 0.4 0
1 NaN 5.9 3.0 5.1 1.8 2
2 NaN 6.0 3.4 4.5 1.6 1
3 NaN 7.3 2.9 6.3 1.8 2
4 NaN 5.6 2.5 3.9 1.1 1
5 NaN 5.4 3.9 1.3 0.4 0
6 NaN 5.5 2.6 4.4 1.2 1
7 NaN 5.7 2.9 4.2 1.3 1
8 NaN 4.9 3.1 1.5 0.1 0
9 NaN 6.7 2.5 5.8 1.8 2
10 NaN 6.5 3.0 5.2 2.0 2
11 NaN 5.1 3.3 1.7 0.5 0
12 NaN 4.6 3.4 1.4 0.3 0
13 NaN 7.4 2.8 6.1 1.9 2
14 NaN 7.7 3.8 6.7 2.2 2
15 NaN 6.6 3.0 4.4 1.4 1
16 NaN 7.2 3.2 6.0 1.8 2
17 NaN 5.8 2.7 5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