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)
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}')
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)
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]:
In [ ]: