Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

15.03.2024, 21:20 LR_2_ShamrayArthur.

ipynb - Colaboratory

keyboard_arrow_down Лабороторна робота №2


Варіант № 24

Виконав студент групи 124-22-1 Шамрай Артур Євгенович

keyboard_arrow_down Завдання №1 (варіант 9)


import math
import numpy as np
import matplotlib.pyplot as plt

f1 = lambda x: x**(math.cos(x**2)) # x=[0...10]


f2 = lambda x: 5*math.sin(10*x) * math.sin(3*x)/(x**x) # x=[0...8]

def task_1_1(f1, f2, xs1, xs2):


plt.figure(figsize=(12, 10))
plt.plot(xs1, [f1(x) for x in xs1], label=f'x^cos(x^2)')
plt.plot(xs2, [f2(x) for x in xs2], label=f'5*sin(10x)*sin(3x)/x^x')
plt.xticks([n for n in range(11)])
plt.legend()
plt.show()

task_1_1(f1, f2, [x for x in np.arange(0, 10.1, 0.1)], [x for x in np.arange(0, 8.1, 0.1)])

def task_1_2(f1, f2, xs1, xs2):


fig, ax = plt.subplots(1, 2, figsize=(12, 10))
funcs = [f1, f2]
xs = [xs1, xs2]
names = ['x^cos(x^2)', '5*sin(10x)*sin(3x)/x^x']

for i, ax in enumerate(fig.axes):
ax.plot(xs[i], [funcs[i](x) for x in xs[i]])
ax.set_title(f'{names[i]}')

task_1_2(f1, f2, [x for x in np.arange(0, 10.1, 0.1)], [x for x in np.arange(0, 8.1, 0.1)])

https://colab.research.google.com/drive/1uh9bgoOQQCwzCJwrkJq4aZzFmJUCvYrL#scrollTo=TWaAgIP8jHBd&printMode=true 1/5
15.03.2024, 21:20 LR_2_ShamrayArthur.ipynb - Colaboratory

keyboard_arrow_down Завдання №2
def letter_counts(filepath):
with open(filepath, 'r') as file:
text = file.read()
all_symbols = list(text.replace(' ', ""))
unique_symbols = set(all_symbols)

counts = dict(sorted({char: all_symbols.count(char) for char in unique_symbols}.items(), key=lambda x: x[1], reverse=True))

plt.figure(figsize=(10, 8))
plt.bar(counts.keys(), counts.values())
plt.savefig('letter_counts.png')

letter_counts('data/text.txt')

keyboard_arrow_down Завдання №3 (варіант 4)

https://colab.research.google.com/drive/1uh9bgoOQQCwzCJwrkJq4aZzFmJUCvYrL#scrollTo=TWaAgIP8jHBd&printMode=true 2/5
15.03.2024, 21:20 LR_2_ShamrayArthur.ipynb - Colaboratory
x1s = np.array([x for x in np.arange(11, 20, 0.1)])
y1 = (149 - 10.2*x1s)/-3.2
y2 = (83 + 5.8*x1s)/16
y3 = (205 - 10.2*x1s)/8.8

plt.figure(figsize=(12, 10))
plt.plot(x1s, y1, label='f[0]')
plt.plot(x1s, y2, label='f[1]')
plt.plot(x1s, y3, label='f[2]')
plt.xticks([x for x in np.arange(11, 20, 0.5)])

plt.text(17.5, 13, 'f[0] X f[1]')


plt.plot(18.319, 11.828, 'ro')

plt.text(12, 11, 'f[1] X f[2]')


plt.plot(11.901, 9.501, 'ro')

plt.text(16, 3, 'f[0] X f[2]')


plt.plot(16.072, 4.667, 'ro')

plt.legend()
plt.show()

keyboard_arrow_down Завдання №4
import json
import seaborn as sns
import pandas as pd

with open('data/currentUSrepresentatives.json', 'r') as f:


data = json.load(f)

data_general = pd.DataFrame(data['objects'])
data_person = pd.DataFrame([obj['person'] for obj in data['objects']])

plt.figure(figsize=(14, 11))
plt.title('The counts of districts by number')
sns.countplot(x=data_general['district'])
plt.show()

https://colab.research.google.com/drive/1uh9bgoOQQCwzCJwrkJq4aZzFmJUCvYrL#scrollTo=TWaAgIP8jHBd&printMode=true 3/5
15.03.2024, 21:20 LR_2_ShamrayArthur.ipynb - Colaboratory

plt.figure(figsize=(10, 8))
plt.title('The percentage of each gender in the data')
plt.pie(data_person['gender'].value_counts(), labels=data_person['gender'].value_counts().index, autopct='%.2f', explode=[0.05, 0.05], sh
plt.show()

output

data_person['birth_year'] = data_person['birthday'].str.split('-', expand=True)[0]

plt.figure(figsize=(18, 11))
sns.countplot(x=data_person['birth_year'], order=data_person['birth_year'].value_counts().index)
plt.xticks(rotation=45)
plt.show()

https://colab.research.google.com/drive/1uh9bgoOQQCwzCJwrkJq4aZzFmJUCvYrL#scrollTo=TWaAgIP8jHBd&printMode=true 4/5
15.03.2024, 21:20 LR_2_ShamrayArthur.ipynb - Colaboratory

Висновки

https://colab.research.google.com/drive/1uh9bgoOQQCwzCJwrkJq4aZzFmJUCvYrL#scrollTo=TWaAgIP8jHBd&printMode=true 5/5

You might also like