Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- import numpy as np
- import pandas as pd
- import math
- import matplotlib.pyplot as plt
- from sklearn import datasets, linear_model
- from sklearn.metrics import mean_squared_error, r2_score
- from sklearn import preprocessing
- from sklearn.pipeline import Pipeline
- from sklearn.preprocessing import PolynomialFeatures
- data = pd.read_csv('ticdata20001.txt', sep=" ", header=None, engine='python')
- data.columns = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
- y = data.loc[:, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]]
- x = data.loc[:, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"] ]
- all_rows = len(data.index)
- train_data = int(math.floor(all_rows * 0.5))
- test_data = int(math.floor(all_rows * 0.5))
- x_train = x[:train_data]
- x_test = x[train_data:]
- y_train = y.iloc[:train_data]
- y_test = y.iloc[train_data:]
- x_train = preprocessing.scale(x_train)
- x_test = preprocessing.scale(x_test)
- regr = linear_model.LinearRegression()
- # передаем тренировочную выборку в качестве параметра
- regr.fit(x_train, y_train)
- # предсказываем значение по модели
- y_pred = regr.predict(x_test)
- print('Coefficients: \n', regr.coef_)
- print("Mean squared error: %.100f" % mean_squared_error(y_test, y_pred))
- print('Variance score: %.2f' % r2_score(y_test, y_pred))
- print('\nPOLINOM\n')
- # степень полинома в массиве
- d = [1, 2, 3, 4, 5]
- varScr = []
- for i in d:
- polynomial_features = PolynomialFeatures(degree=i, include_bias=False)
- # построение полимиальной функции
- pipeline = Pipeline([("polynomial_features", polynomial_features), ("linear_regression", regr)])
- # расчет модели
- pipeline.fit(np.array(x_train), np.array(y_train))
- # получаем предсказанные значения на основе тестовой выборки
- y_pred = pipeline.predict(np.array(x_test))
- # сравнение тестовых и предсказанных значений
- r2 = r2_score(y_test, y_pred)
- varScr.append(r2)
- print("Mean squared error: %.100f" % mean_squared_error(y_test, y_pred))
- print('Variance score: %.2f' % r2)
- fig = plt.figure()
- plt.plot(d, varScr)
- plt.xlabel('Degree polinome', fontsize=15)
- plt.ylabel('Variance score', fontsize=15)
- plt.show()
- print('\nLASSO\n')
- alphas = [0.0001, 0.001, 0.01, 0.1, 1]
- varScr = []
- for i in alphas:
- regr = linear_model.Lasso(alpha=i)
- regr.fit(x_train, y_train)
- y_pred = regr.predict(x_test)
- r2 = r2_score(y_test, y_pred)
- varScr.append(r2)
- # The mean squared error
- print("Mean squared error: %.100f"
- % mean_squared_error(y_test, y_pred))
- # Explained variance score: 1 is perfect prediction
- print('Variance score: %.2f' % r2)
- fig = plt.figure()
- plt.plot(alphas, varScr)
- plt.xlabel('Regularization coefficient (d)', fontsize=15)
- plt.ylabel('Variance score (varScr)', fontsize=15)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement