Untitled

 avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import linregress
import matplotlib.animation as animation

# Zadanie 1
years = np.array([2000, 2002, 2005, 2007, 2010])
percentages = np.array([6.5, 7.0, 7.4, 8.2, 9.0])

slope, intercept, _, _, _ = linregress(years, percentages)

# Zadanie 3
def predicted_percentage(year):
    return slope * year + intercept

year = 2000
while predicted_percentage(year) <= 12:
    year += 1

print("Year when unemployment rate exceeds 12%:", year)

# Zadanie 4
fig, ax = plt.subplots()
ax.scatter(years, percentages, label='Actual data')

line, = ax.plot([], [], color='red', label='Regression line')

def init():
    line.set_data([], [])
    return line,

def animate(i):
    x = np.linspace(years[0], years[-1], 1000)
    y = slope * x + intercept
    line.set_data(x[:i], y[:i])
    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init, frames=len(years), interval=500, blit=True)

plt.xlabel('Year')
plt.ylabel('Unemployment Percentage')
plt.title('Linear Regression Animation')
plt.legend()
plt.show()
Editor is loading...
Leave a Comment