Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' - - - - - - - - - - - - - - - - - - - - - Lista 1 : Zadanie 1 - - - - - - - - - - - - - - - - - - - - - - - '''
- import matplotlib.pyplot as plt
- import numpy as np
- import scipy.io
- u1 = []
- y1 = []
- u2 = []
- y2 = []
- u3 = []
- y3 = []
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' #wpisywanie danych do tablic
- file = scipy.io.loadmat('zadanie_1i2_zbior_1.mat')
- data = file['dane1']
- for x in data:
- i = 0
- u1.append(x[i])
- y1.append(x[i+1])
- file = scipy.io.loadmat('zadanie_1i2_zbior_2.mat')
- data = file['dane2']
- for x in data:
- i = 0
- u2.append(x[i])
- y2.append(x[i+1])
- file = scipy.io.loadmat('zadanie_1i2_zbior_3.mat')
- data = file['dane3']
- for x in data:
- i = 0
- u3.append(x[i])
- y3.append(x[i+1])
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' #wyświetlanie
- print("\nDane z pierwszego pliku : \n")
- for i in range(0,len(u1)):
- print(u1[i],y1[i])
- print("\nDane z drugiego pliku : \n")
- for i in range(0,len(u2)):
- print(u2[i],y2[i])
- print("\nDane z trzeciego pliku : \n")
- for i in range(0, len(u3)):
- print(u3[i], y3[i])
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' #zastosowanie algorytmu - - - pierwszy model
- def tetaf1(y,u):
- suma1=0
- suma2=0
- for i in range(len(y)):
- suma1+=(int(y[i])*int(u[i]))
- suma2+=(u[i]**2)
- return suma1/suma2
- teta11 = tetaf1(y1,u1)
- teta12 = tetaf1(y2,u2)
- teta13 = tetaf1(y3,u3)
- def model(teta,u):
- return teta*u
- values1 = []
- values2 = []
- values3 = []
- for i in range(len(u1)):
- values1.append(model(teta11,u1[i]))
- for i in range(len(u2)):
- values2.append(model(teta12, u2[i]))
- for i in range(len(u3)):
- values3.append(model(teta13, u3[i]))
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' #zastosowanie algorytmu - - - drugi model
- def teta0(u, y):
- return np.average(y) - teta1(u, y) * np.average(u)
- def teta1(u, y):
- theta_licznik = 0
- theta_mianownik = 0
- for i in range(len(u)):
- theta_licznik += int(y[i]) * int(u[i]) - int(np.average(y)) * int(np.average(u))
- theta_mianownik += u[i] ** 2 - np.average(u) ** 2
- theta = ((1 / len(u)) * theta_licznik) / ((1 / len(u)) * theta_mianownik)
- return theta
- teta21 = teta1(u1,y1)
- teta22 = teta1(u2,y2)
- teta23 = teta1(u3,y3)
- teta31 = teta0(u1,y1)
- teta32 = teta0(u2,y2)
- teta33 = teta0(u3,y3)
- def model2(teta1,teta0,u):
- return teta1*u + teta0
- values4 = []
- values5 = []
- values6 = []
- for i in range(len(u1)):
- values4.append(model2(teta21,teta31, u1[i]))
- for i in range(len(u2)):
- values5.append(model2(teta22,teta32, u2[i]))
- for i in range(len(u3)):
- values6.append(model2(teta23,teta33, u3[i]))
- ' - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' #rysowanie
- print('\nDla modelu .. | y = teta*u\n') #pierwszy model
- print('Teta1 =',teta11,'\nTeta2 =',teta12,'\nTeta3 =',teta13)
- plt.figure()
- plt.plot(u1,values1,c='r')
- plt.title('Dane 1 - pierwszy model')
- plt.scatter(u1,y1)
- plt.ylabel('Zapamiętana ilość ')
- plt.xlabel('Czas przed telewizorem')
- plt.figure()
- plt.plot(u2,values2,c='y')
- plt.title('Dane 2 - pierwszy model')
- plt.scatter(u2,y2)
- plt.ylabel('Lata edukacji ')
- plt.xlabel('Ilość spędzonych przy komputerze godzin dziennie')
- plt.figure()
- plt.plot(u3,values3,c='g')
- plt.title('Dane 3 - pierwszy model')
- plt.scatter(u3,y3)
- plt.ylabel('Cena za metr kwadratowy ')
- plt.xlabel('Liczba wolnych powierzchni biurowych (%)')
- print('\nDla modelu .. | y = teta1*u - teta0\n') #drugi model
- print('1.\nTeta0 =',teta31,'\nTeta1 =',teta21,'\n2.\nTeta0 =',teta32)
- print('Teta1 =',teta22,'\n3.\nTeta0=',teta33,'\nTeta1 =',teta23)
- plt.figure()
- plt.plot(u1,values4,c='r')
- plt.title('Dane 1 - drugi model')
- plt.scatter(u1,y1)
- plt.ylabel('Zapamiętana ilość ')
- plt.xlabel('Czas przed telewizorem')
- plt.figure()
- plt.plot(u2,values5,c='y')
- plt.title('Dane 2 - drugi model')
- plt.scatter(u2,y2)
- plt.ylabel('Lata edukacji ')
- plt.xlabel('Ilość spędzonych przy komputerze godzin dziennie')
- plt.figure()
- plt.plot(u3,values6,c='g')
- plt.title('Dane 3 - drugi model')
- plt.scatter(u3,y3)
- plt.ylabel('Cena za metr kwadratowy ')
- plt.xlabel('Liczba wolnych powierzchni biurowych (%)')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement