Advertisement
lancernik

AISDKolos

May 7th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue May 7 12:54:00 2019
  4.  
  5. @author: lancernik
  6. """
  7. from requests import get
  8. from requests.exceptions import RequestException
  9. from contextlib import closing
  10. from bs4 import BeautifulSoup
  11. import string
  12. import re
  13. from itertools import groupby
  14. import pandas as pd
  15. import time
  16. import matplotlib.pyplot as plt
  17. import numpy as np
  18. from scipy.stats import kde
  19. import seaborn as sns
  20. from sklearn.linear_model import LinearRegression
  21. from sklearn.model_selection import train_test_split
  22. from sklearn.metrics import mean_squared_error
  23. from sklearn.metrics import mean_absolute_error
  24. import sklearn.preprocessing
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. def regress2(x_train, x_test, y_train, y_test): #Model sample
  32.  
  33. r = LinearRegression()
  34. r.fit(x_train,y_train)
  35. y_train_pred = r.predict(x_train)
  36. y_test_pred = r.predict(x_test)
  37.  
  38. mse = mean_squared_error
  39. mae = mean_absolute_error
  40.  
  41. return{
  42. "r_score": r.score(x_train,y_train), #??????????
  43. "MSE_u": mse(y_train,y_train_pred),
  44. "MSE_t": mse(y_test,y_test_pred),
  45. "MSA_u": mae(y_train,y_train_pred),
  46. "MSA_t": mae(y_test,y_test_pred)}
  47.  
  48. def regress3(x,y): #Model wielomianowy
  49. x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2, random_state=12345)
  50. reg3 = sklearn.preprocessing.PolynomialFeatures(degree=2,include_bias = False)
  51. x2_train = reg3.fit_transform(x_train)
  52. x2_test = reg3.fit_transform(x_test)
  53. # print(reg3.powers_.T)
  54. params.append("zm. wielom")
  55. res.append(regress2(x2_train, x2_test, y_train, y_test))
  56.  
  57.  
  58. print(pd.DataFrame(res,index=params))
  59.  
  60.  
  61. def generator():
  62.  
  63. xlist=[]
  64. ylist=[]
  65. for i in range(0,100):
  66.  
  67. x = np.random.rand(1)*8 -4
  68. xlist.append(x)
  69. # print(x)
  70. y = 0.5*pow(x,4) + 2*pow(x,2)+1
  71. ylist.append(y)
  72. # print(out)
  73. df = pd.DataFrame({
  74. 'x':xlist,
  75. 'y': ylist})
  76. return df
  77.  
  78.  
  79.  
  80.  
  81. df = generator()
  82. a = np.array(df['x'].tolist()).reshape(-1,1)
  83. b = np.array(df['y'].tolist()).reshape(-1,1)
  84.  
  85.  
  86. params =['zm. liniowe']
  87. x_train, x_test, y_train, y_test = train_test_split(a,b,test_size = 0.2, random_state=12345)
  88. res = [regress2(x_train, x_test, y_train, y_test)]
  89. reg2 = pd.DataFrame(res,index=params)
  90.  
  91.  
  92.  
  93.  
  94.  
  95. print(regress3(a,b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement