Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue May 7 12:54:00 2019
- @author: lancernik
- """
- from requests import get
- from requests.exceptions import RequestException
- from contextlib import closing
- from bs4 import BeautifulSoup
- import string
- import re
- from itertools import groupby
- import pandas as pd
- import time
- import matplotlib.pyplot as plt
- import numpy as np
- from scipy.stats import kde
- import seaborn as sns
- from sklearn.linear_model import LinearRegression
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import mean_squared_error
- from sklearn.metrics import mean_absolute_error
- import sklearn.preprocessing
- def regress2(x_train, x_test, y_train, y_test): #Model sample
- r = LinearRegression()
- r.fit(x_train,y_train)
- y_train_pred = r.predict(x_train)
- y_test_pred = r.predict(x_test)
- mse = mean_squared_error
- mae = mean_absolute_error
- return{
- "r_score": r.score(x_train,y_train), #??????????
- "MSE_u": mse(y_train,y_train_pred),
- "MSE_t": mse(y_test,y_test_pred),
- "MSA_u": mae(y_train,y_train_pred),
- "MSA_t": mae(y_test,y_test_pred)}
- def regress3(x,y): #Model wielomianowy
- x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.2, random_state=12345)
- reg3 = sklearn.preprocessing.PolynomialFeatures(degree=2,include_bias = False)
- x2_train = reg3.fit_transform(x_train)
- x2_test = reg3.fit_transform(x_test)
- # print(reg3.powers_.T)
- params.append("zm. wielom")
- res.append(regress2(x2_train, x2_test, y_train, y_test))
- print(pd.DataFrame(res,index=params))
- def generator():
- xlist=[]
- ylist=[]
- for i in range(0,100):
- x = np.random.rand(1)*8 -4
- xlist.append(x)
- # print(x)
- y = 0.5*pow(x,4) + 2*pow(x,2)+1
- ylist.append(y)
- # print(out)
- df = pd.DataFrame({
- 'x':xlist,
- 'y': ylist})
- return df
- df = generator()
- a = np.array(df['x'].tolist()).reshape(-1,1)
- b = np.array(df['y'].tolist()).reshape(-1,1)
- params =['zm. liniowe']
- x_train, x_test, y_train, y_test = train_test_split(a,b,test_size = 0.2, random_state=12345)
- res = [regress2(x_train, x_test, y_train, y_test)]
- reg2 = pd.DataFrame(res,index=params)
- print(regress3(a,b))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement