Advertisement
ZergRushA

rt-ai-4

Oct 21st, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.98 KB | None | 0 0
  1. '''
  2. #Задание 1
  3. import numpy as np
  4. from numpy import *
  5. from numpy.random import *
  6. from scipy import linalg
  7. import matplotlib.pyplot as plt
  8.  
  9. #Экстерполяция полинома 1-ой степени
  10. x=np.array([0,1,2,3,4,5])
  11. y=np.array([-3,-1.2,0.8,2.4,3.2,5])
  12.  
  13. A=np.vstack([x,np.ones(len(x))]).T
  14. print(A)
  15.  
  16. m,c=np.linalg.lstsq(A,y,rcond=None)[0]
  17. print(m,c)
  18.  
  19. plt.plot(x,y,'o',label='Исходные данные',markersize=10)
  20. plt.plot(x,m*x+c,'r',label='Линейная экстраполяция')
  21. plt.legend()
  22. plt.show()
  23.  
  24. #Экстерполяция полинома 2-ой степени
  25. delta=1.0
  26. x=linspace(-8,8,18)
  27. y=x**2+delta*(rand(18)-0.5)
  28. x+=delta*(rand(18)-0.5)
  29.  
  30. x.tofile('x_data.txt','\n')
  31. y.tofile('y_data.txt','\n')
  32.  
  33. x=fromfile('x_data.txt',float,sep='\n')
  34. y=fromfile('y_data.txt',float,sep='\n')
  35.  
  36. print(x)
  37. print(y)
  38.  
  39. m=vstack((x**2,x,ones(18))).T
  40.  
  41. s=np.linalg.lstsq(m,y,rcond=None)[0]
  42.  
  43. x_prec=linspace(-8,8,164)
  44.  
  45. plt.plot(x,y,'D')
  46. plt.plot(x_prec,s[0]*x_prec**2+s[1]*x_prec+s[2],'-',lw=2)
  47. plt.grid()
  48. plt.savefig('парабола.png')
  49.  
  50. #Экстерполяция полинома 3-ей степени
  51. m=vstack((x**3,x**2,x,ones(18))).T
  52.  
  53. s=np.linalg.lstsq(m,y,rcond=None)[0]
  54.  
  55. x_prec=linspace(-8,8,164)
  56.  
  57. plt.plot(x,y,'D')
  58. plt.plot(x_prec,s[0]*x_prec**3+s[1]*x_prec**2+s[2]*x_prec+s[3],'-',lw=3)
  59. plt.grid()
  60. plt.savefig('парабола.png')
  61.  
  62. #Задание 2
  63. import matplotlib.pyplot as plt
  64. from scipy import linalg
  65. import scipy as sp
  66. from scipy.optimize import curve_fit
  67. import numpy as np
  68. from numpy import *
  69. from numpy.random import *
  70.  
  71. #Функция 1
  72. beta = (0.5, 1)
  73. def f(x, b0, b1):
  74.    return b0 + b1 * x
  75.  
  76. xdata = np.linspace(0, 8, 80)
  77.  
  78. y = f(xdata, *beta)
  79.  
  80. ydata = y + 0.05 * np.random.randn(len(xdata))
  81. beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
  82. print (beta_opt)
  83.  
  84. lin_dev = sum(beta_cov[0])
  85. print(lin_dev)
  86.  
  87. residuals = ydata - f(xdata,*beta_opt)
  88. fres = sum(residuals**2)
  89.  
  90. print(fres)
  91.  
  92. fig, ax = plt.subplots()
  93. ax.scatter(xdata, ydata)
  94. ax.plot(xdata, y, 'r', lw=2)
  95. ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
  96. ax.set_xlim(0, 8)
  97. ax.set_xlabel(r"$x$", fontsize=18)
  98. ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
  99. plt.show()
  100.  
  101. #Функция 2
  102. beta = (0.5, 1.5, 1)
  103. def f(x, b0, b1, b2):
  104.    return b0+ b1 * x + b2*x*x
  105.  
  106. xdata = np.linspace(0, 8, 80)
  107.  
  108. y = f(xdata, *beta)
  109.  
  110. ydata = y + 0.05 * np.random.randn(len(xdata))
  111. beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
  112. print(beta_opt)
  113. lin_dev = sum(beta_cov[0])
  114. print(lin_dev)
  115.  
  116. residuals = ydata - f(xdata, *beta_opt)
  117. fres = sum(residuals**2)
  118.  
  119. fig, ax = plt.subplots()
  120. ax.scatter(xdata, ydata)
  121. ax.plot(xdata, y, 'r', lw=2)
  122. ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
  123. ax.set_xlim(0, 8)
  124. ax.set_xlabel(r"$x$", fontsize=18)
  125. ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
  126. plt.show()
  127.  
  128. #Функция 3
  129.  
  130. beta = (2, 4)
  131. def f(x, b0, b1):
  132.    return b0 + b1 * np.log(x)
  133.  
  134. xdata = np.linspace(1, 8, 80)
  135.  
  136. y = f(xdata, *beta)
  137.  
  138. ydata = y + 0.05 * np.random.randn(len(xdata))
  139. beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
  140. print(beta_opt)
  141.  
  142. lin_dev = sum(beta_cov[0])
  143. print(lin_dev)
  144.  
  145.  
  146. residuals = ydata - f(xdata, *beta_opt)
  147. fres = sum(residuals**2)
  148.  
  149. print(fres)
  150.  
  151. fig, ax = plt.subplots()
  152. ax.scatter(xdata, ydata)
  153. ax.plot(xdata, y, 'r', lw=2)
  154. ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
  155. ax.set_xlim(0, 8)
  156. ax.set_xlabel(r"$x$", fontsize=18)
  157. ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
  158. plt.show()
  159.  
  160. #Функция 4
  161.  
  162.  
  163. beta = (2, 4)
  164. def f(x, b0, b1):
  165.    return b0 * x ** b1
  166.  
  167. xdata = np.linspace(1, 8, 80)
  168.  
  169. y = f(xdata, *beta)
  170.  
  171. ydata = y + 0.05 * np.random.randn(len(xdata))
  172. beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
  173. print(beta_opt)
  174.  
  175. lin_dev = sum(beta_cov[0])
  176. print(lin_dev)
  177.  
  178. residuals = ydata - f(xdata, *beta_opt)
  179. fres = sum(residuals**2)
  180.  
  181. print(fres)
  182.  
  183. fig, ax = plt.subplots()
  184. ax.scatter(xdata, ydata)
  185. ax.plot(xdata, y, 'r', lw=2)
  186. ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
  187. ax.set_xlim(0, 8)
  188. ax.set_xlabel(r"$x$", fontsize=18)
  189. ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
  190. plt.show()
  191.  
  192.  
  193.  
  194.  
  195. #Задание 3
  196. import pandas as pd
  197. import numpy as np
  198. import matplotlib.pyplot as plt
  199. from pandas import DataFrame,Series
  200. from sklearn.model_selection import train_test_split
  201. from sklearn.linear_model import LinearRegression
  202.  
  203. url="https://raw.githubusercontent.com/AnnaShestova/salary-years-simple-linear-regression/master/Salary_Data.csv"
  204. df=pd.read_csv(url)
  205.  
  206. print(df.head(5))
  207. print("--------------")
  208.  
  209. print(df.shape)
  210. print("--------------")
  211. print(df.describe())
  212. print("--------------")
  213.  
  214. plt.scatter(df['YearsExperience'],df['Salary'],color='b',label="данные опыта работы и ЗП")
  215. plt.xlabel("Опыт работы")
  216. plt.ylabel("Заработная плата")
  217. plt.show()
  218.  
  219. X=df.iloc[:,:-1].values
  220. y=df.iloc[:,1].values
  221. print(X)
  222. print("--------------")
  223. print(y)
  224. print("--------------")
  225.  
  226. X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
  227.  
  228. regressor=LinearRegression()
  229. regressor.fit(X_train,y_train)
  230.  
  231. print(regressor.intercept_)
  232. print("--------------")
  233. print(regressor.coef_)
  234. print("--------------")
  235.  
  236. y_pred=regressor.predict(X_test)
  237.  
  238. datf=pd.DataFrame({'Actual':y_test,'Predicted':y_pred})
  239. print(datf)
  240. print("--------------")
  241.  
  242. plt.scatter(X_test,y_test,color='gray')
  243. plt.plot(X_test,y_pred,color='red',linewidth=2)
  244. plt.show()
  245.  
  246. '''
  247.  
  248. # Множественная линейная регрессия
  249. import pandas as pd
  250. from pandas import read_csv
  251. import numpy as np
  252. import seaborn as sn
  253. from sklearn.model_selection import train_test_split
  254. from sklearn.linear_model import LinearRegression
  255. from sklearn import metrics
  256.  
  257.  
  258. url = r'https://raw.githubusercontent.com/aniruddhachoudhury/Red-Wine-Quality/master/winequality-red.csv'
  259. df = read_csv(url)
  260.  
  261. df_pH = df['pH']
  262.  
  263. df_alc_qual = [df['alcohol'], df['quality']]
  264.  
  265. #print(df_pH.head(5), df_alc_qual.head(5), sep='\n')
  266.  
  267. new_df_pH = np.array(df_pH)
  268. new_df_pH = new_df_pH.transpose()
  269.  
  270. new_df_alc_qual = np.array(df_alc_qual)
  271. new_df_alc_qual = new_df_alc_qual.transpose()
  272.  
  273.  
  274. df1 = pd.DataFrame(new_df_pH)
  275. df2 = pd.DataFrame(new_df_alc_qual)
  276.  
  277. df1 = df1.rename(columns = {0: 'pH'}, inplace=False)
  278. df2 = df2.rename(columns = {0: 'alcohol', 1: 'quality'}, inplace=False)
  279.  
  280.  
  281. frames = [df1, df2]
  282.  
  283. dataset = pd.concat(frames, axis=1, join="inner")
  284.  
  285. print(dataset.head(5))
  286.  
  287. print(dataset.shape)
  288.  
  289. df_pH = dataset['pH']
  290. df_alc_qual = dataset[['alcohol', 'quality']]
  291.  
  292. x_train, x_test, y_train, y_test = train_test_split(df_alc_qual, df_pH, test_size=0.2, random_state=42)
  293.  
  294. regressor = LinearRegression()
  295. regressor.fit(x_train, y_train)
  296.  
  297. coeff_df = pd.DataFrame(regressor.coef_, df_alc_qual.columns, columns=['Coefficient'])
  298.  
  299. print(coeff_df)
  300.  
  301. y_pred = regressor.predict(x_test)
  302.  
  303. df_final = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
  304.  
  305.  
  306. print(df_final)
  307.  
  308. print('Mean Squared Error: ', metrics.mean_squared_error(y_test, y_pred))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement