Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- #Задание 1
- import numpy as np
- from numpy import *
- from numpy.random import *
- from scipy import linalg
- import matplotlib.pyplot as plt
- #Экстерполяция полинома 1-ой степени
- x=np.array([0,1,2,3,4,5])
- y=np.array([-3,-1.2,0.8,2.4,3.2,5])
- A=np.vstack([x,np.ones(len(x))]).T
- print(A)
- m,c=np.linalg.lstsq(A,y,rcond=None)[0]
- print(m,c)
- plt.plot(x,y,'o',label='Исходные данные',markersize=10)
- plt.plot(x,m*x+c,'r',label='Линейная экстраполяция')
- plt.legend()
- plt.show()
- #Экстерполяция полинома 2-ой степени
- delta=1.0
- x=linspace(-8,8,18)
- y=x**2+delta*(rand(18)-0.5)
- x+=delta*(rand(18)-0.5)
- x.tofile('x_data.txt','\n')
- y.tofile('y_data.txt','\n')
- x=fromfile('x_data.txt',float,sep='\n')
- y=fromfile('y_data.txt',float,sep='\n')
- print(x)
- print(y)
- m=vstack((x**2,x,ones(18))).T
- s=np.linalg.lstsq(m,y,rcond=None)[0]
- x_prec=linspace(-8,8,164)
- plt.plot(x,y,'D')
- plt.plot(x_prec,s[0]*x_prec**2+s[1]*x_prec+s[2],'-',lw=2)
- plt.grid()
- plt.savefig('парабола.png')
- #Экстерполяция полинома 3-ей степени
- m=vstack((x**3,x**2,x,ones(18))).T
- s=np.linalg.lstsq(m,y,rcond=None)[0]
- x_prec=linspace(-8,8,164)
- plt.plot(x,y,'D')
- plt.plot(x_prec,s[0]*x_prec**3+s[1]*x_prec**2+s[2]*x_prec+s[3],'-',lw=3)
- plt.grid()
- plt.savefig('парабола.png')
- #Задание 2
- import matplotlib.pyplot as plt
- from scipy import linalg
- import scipy as sp
- from scipy.optimize import curve_fit
- import numpy as np
- from numpy import *
- from numpy.random import *
- #Функция 1
- beta = (0.5, 1)
- def f(x, b0, b1):
- return b0 + b1 * x
- xdata = np.linspace(0, 8, 80)
- y = f(xdata, *beta)
- ydata = y + 0.05 * np.random.randn(len(xdata))
- beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
- print (beta_opt)
- lin_dev = sum(beta_cov[0])
- print(lin_dev)
- residuals = ydata - f(xdata,*beta_opt)
- fres = sum(residuals**2)
- print(fres)
- fig, ax = plt.subplots()
- ax.scatter(xdata, ydata)
- ax.plot(xdata, y, 'r', lw=2)
- ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
- ax.set_xlim(0, 8)
- ax.set_xlabel(r"$x$", fontsize=18)
- ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
- plt.show()
- #Функция 2
- beta = (0.5, 1.5, 1)
- def f(x, b0, b1, b2):
- return b0+ b1 * x + b2*x*x
- xdata = np.linspace(0, 8, 80)
- y = f(xdata, *beta)
- ydata = y + 0.05 * np.random.randn(len(xdata))
- beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
- print(beta_opt)
- lin_dev = sum(beta_cov[0])
- print(lin_dev)
- residuals = ydata - f(xdata, *beta_opt)
- fres = sum(residuals**2)
- fig, ax = plt.subplots()
- ax.scatter(xdata, ydata)
- ax.plot(xdata, y, 'r', lw=2)
- ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
- ax.set_xlim(0, 8)
- ax.set_xlabel(r"$x$", fontsize=18)
- ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
- plt.show()
- #Функция 3
- beta = (2, 4)
- def f(x, b0, b1):
- return b0 + b1 * np.log(x)
- xdata = np.linspace(1, 8, 80)
- y = f(xdata, *beta)
- ydata = y + 0.05 * np.random.randn(len(xdata))
- beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
- print(beta_opt)
- lin_dev = sum(beta_cov[0])
- print(lin_dev)
- residuals = ydata - f(xdata, *beta_opt)
- fres = sum(residuals**2)
- print(fres)
- fig, ax = plt.subplots()
- ax.scatter(xdata, ydata)
- ax.plot(xdata, y, 'r', lw=2)
- ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
- ax.set_xlim(0, 8)
- ax.set_xlabel(r"$x$", fontsize=18)
- ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
- plt.show()
- #Функция 4
- beta = (2, 4)
- def f(x, b0, b1):
- return b0 * x ** b1
- xdata = np.linspace(1, 8, 80)
- y = f(xdata, *beta)
- ydata = y + 0.05 * np.random.randn(len(xdata))
- beta_opt, beta_cov = sp.optimize.curve_fit(f, xdata, ydata)
- print(beta_opt)
- lin_dev = sum(beta_cov[0])
- print(lin_dev)
- residuals = ydata - f(xdata, *beta_opt)
- fres = sum(residuals**2)
- print(fres)
- fig, ax = plt.subplots()
- ax.scatter(xdata, ydata)
- ax.plot(xdata, y, 'r', lw=2)
- ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)
- ax.set_xlim(0, 8)
- ax.set_xlabel(r"$x$", fontsize=18)
- ax.set_ylabel(r"$f(x, \beta)$", fontsize=18)
- plt.show()
- #Задание 3
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from pandas import DataFrame,Series
- from sklearn.model_selection import train_test_split
- from sklearn.linear_model import LinearRegression
- url="https://raw.githubusercontent.com/AnnaShestova/salary-years-simple-linear-regression/master/Salary_Data.csv"
- df=pd.read_csv(url)
- print(df.head(5))
- print("--------------")
- print(df.shape)
- print("--------------")
- print(df.describe())
- print("--------------")
- plt.scatter(df['YearsExperience'],df['Salary'],color='b',label="данные опыта работы и ЗП")
- plt.xlabel("Опыт работы")
- plt.ylabel("Заработная плата")
- plt.show()
- X=df.iloc[:,:-1].values
- y=df.iloc[:,1].values
- print(X)
- print("--------------")
- print(y)
- print("--------------")
- X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
- regressor=LinearRegression()
- regressor.fit(X_train,y_train)
- print(regressor.intercept_)
- print("--------------")
- print(regressor.coef_)
- print("--------------")
- y_pred=regressor.predict(X_test)
- datf=pd.DataFrame({'Actual':y_test,'Predicted':y_pred})
- print(datf)
- print("--------------")
- plt.scatter(X_test,y_test,color='gray')
- plt.plot(X_test,y_pred,color='red',linewidth=2)
- plt.show()
- '''
- # Множественная линейная регрессия
- import pandas as pd
- from pandas import read_csv
- import numpy as np
- import seaborn as sn
- from sklearn.model_selection import train_test_split
- from sklearn.linear_model import LinearRegression
- from sklearn import metrics
- url = r'https://raw.githubusercontent.com/aniruddhachoudhury/Red-Wine-Quality/master/winequality-red.csv'
- df = read_csv(url)
- df_pH = df['pH']
- df_alc_qual = [df['alcohol'], df['quality']]
- #print(df_pH.head(5), df_alc_qual.head(5), sep='\n')
- new_df_pH = np.array(df_pH)
- new_df_pH = new_df_pH.transpose()
- new_df_alc_qual = np.array(df_alc_qual)
- new_df_alc_qual = new_df_alc_qual.transpose()
- df1 = pd.DataFrame(new_df_pH)
- df2 = pd.DataFrame(new_df_alc_qual)
- df1 = df1.rename(columns = {0: 'pH'}, inplace=False)
- df2 = df2.rename(columns = {0: 'alcohol', 1: 'quality'}, inplace=False)
- frames = [df1, df2]
- dataset = pd.concat(frames, axis=1, join="inner")
- print(dataset.head(5))
- print(dataset.shape)
- df_pH = dataset['pH']
- df_alc_qual = dataset[['alcohol', 'quality']]
- x_train, x_test, y_train, y_test = train_test_split(df_alc_qual, df_pH, test_size=0.2, random_state=42)
- regressor = LinearRegression()
- regressor.fit(x_train, y_train)
- coeff_df = pd.DataFrame(regressor.coef_, df_alc_qual.columns, columns=['Coefficient'])
- print(coeff_df)
- y_pred = regressor.predict(x_test)
- df_final = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
- print(df_final)
- print('Mean Squared Error: ', metrics.mean_squared_error(y_test, y_pred))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement