Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn import datasets, linear_model
- from sklearn.metrics import r2_score, mean_squared_error
- from sklearn.preprocessing import PolynomialFeatures
- from sklearn.linear_model import LinearRegression
- from sklearn.pipeline import Pipeline
- from sklearn.model_selection import cross_val_score
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- ## подкл выборку
- x_train = pd.read_table ("tic/ticdata2000.txt", header = None).iloc [0:4000, 0:85]
- x_test = x_train.iloc[0:4000, 84]
- y_train = pd.read_table ("tic/ticeval2000.txt", header = None)
- y_test = pd.read_table ("tic/tictgts2000.txt", header = None)
- ## обучение выборки
- regression = linear_model.LinearRegression()
- regression.fit (x_train, y_train)
- train_predict = regression.predict (x_train)
- print('Коэффициенты: \n', regression.coef_)
- print("Cреднеквадратичная ошибка: %.2f" % mean_squared_error(y_train, train_predict))
- print('Оценка отклонения: %.2f' % r2_score(y_train, train_predict))
- ## Проверка точности модели по тестовой выборке и запись в результирующий файл
- y_pr = pd.DataFrame(train_predict)
- y_test = y_test.reset_index(drop = True)
- res = pd.concat([y_pr, y_test], axis=1)
- res.to_csv("result.txt", index = False)
- ## Построение модели с использованием полиномиальной функции (где-то тут ошибка)
- degrees = [1,2,3]
- err = [1,2,3]
- pol_index = x_test
- x_train_pol = x_train
- y_train_pol = y_train
- x_test_pol = x_test
- y_test_pol = y_test
- for i in range(len(degrees)):
- polynomial_features = PolynomialFeatures(degree = degrees[i],
- include_bias = False)
- linear_regression = LinearRegression()
- pipeline = Pipeline([("polynomial_features", polynomial_features),
- ("linear_regression", linear_regression)])
- pipeline.fit(x_train_pol, y_train_pol)
- scores = cross_val_score(pipeline, x_train_pol, y_train_pol,
- scoring="neg_mean_squared_error")
- pred_y = pipeline.predict(x_test_pol)
- err[i] = -scores.mean()
- print("\nСтепень: {}\nСреднеквадратичная ошибка = {}(+/- {})".format(degrees[i], -scores.mean(),
- scores.std()))
- print('Показатель отклонения: %.3f' % r2_score(y_test_pol, pred_y))
Advertisement
Add Comment
Please, Sign In to add comment