import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import Ridge
from sklearn import linear_model
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.preprocessing import StandardScaler
import pandas as pd
import matplotlib.pyplot as plt
#Create dataframe from dataset
df = pd.read_json("/content/data.json")
#X_train = []
df["t"] = df["t"].astype(float)
X_train = df["t"]
X_train = np.array(X_train)[:, None]
#Y_train_c = []
df["c"] = df["c"].astype(float)
Y_train_c = df["c"]
Y_train_c = np.array(Y_train_c)[:, None]
#Y_train_ma = []
df["ma"] = df["ma"].astype(float)
Y_train_ma = df["ma"]
Y_train_ma = np.array(Y_train_ma)[:, None]
Y_train = [Y_train_c, Y_train_ma]
# Fit regression model
model_gbr_c = GradientBoostingRegressor()
model_gbr_c.fit(X_train, Y_train_c)
model_pr_c = PolynomialFeatures(2)
poly_c = model_pr_c.fit_transform(X_train)
model_pr_c.fit(poly_c, Y_train_c)
model_ridge_c = Ridge(alpha=1.0)
model_ridge_c.fit(X_train, Y_train_c)
model_lasso_c = linear_model.Lasso(alpha=0.1)
model_lasso_c.fit(X_train, Y_train_c)
model_gbr_ma = GradientBoostingRegressor()
model_gbr_ma.fit(X_train, Y_train_ma)
model_pr_ma = PolynomialFeatures(2)
poly_ma = model_pr_ma.fit_transform(X_train)
model_pr_ma.fit(poly_ma, Y_train_ma)
model_ridge_ma = Ridge(alpha=1.0)
model_ridge_ma.fit(X_train, Y_train_ma)
model_lasso_ma = linear_model.Lasso(alpha=0.1)
model_lasso_ma.fit(X_train, Y_train_ma)
# Predict
X_test = np.arange(1, 4902, 1.0)[:, np.newaxis]
#X_test = X_test.reshape(1, -1)
Y_test_gbr_c = model_gbr_c.predict(X_test)
Y_test_gbr_c.reshape(-1, 1)
model_gbr_c.predict(X_test)
Y_test_gbr_ma = model_gbr_c.predict(X_test)
Y_test_gbr_ma.reshape(-1, 1)
model_gbr_ma.predict(X_test)
Y_test_ridge_c = model_ridge_ma.predict(X_test)
Y_test_ridge_ma = model_ridge_ma.predict(X_test)
Y_test_lasso_c = model_lasso_c.predict(X_test)
Y_test_lasso_ma = model_lasso_ma.predict(X_test)
#Plot the results
plt.figure()
#plt.scatter(X_train, Y_train_c, s=20, edgecolor="lightblue", label="data")
plt.plot(X_test, Y_test_gbr_c, color="red", label="GB Regression", linewidth=2)
plt.plot(X_test, Y_test_ridge_c, color="yellow", label="Ridge-regression", linewidth=2)
plt.plot(X_test, Y_test_lasso_c, color="green", label="Lasso-regression", linewidth=2)
plt.plot(X_train, Y_train_c, color="black", label="data c")
plt.xlabel("t")
plt.ylabel("c")
plt.title("Regressions")
plt.legend()
plt.show()
plt.savefig("metric_score_c.png",bbox_inches='tight')
plt.figure()
#plt.scatter(X_train, Y_train_ma, s=20, edgecolor="darkblue", label="data")
plt.plot(X_test, Y_test_gbr_ma, color="darkviolet", label="GB Regression")
plt.plot(X_test, Y_test_ridge_ma, color="yellow", label="Ridge-regression", linewidth=2)
plt.plot(X_test, Y_test_lasso_ma, color="green", label="Lasso-regression", linewidth=2)
plt.plot(X_train, Y_train_ma, color="gray", label="data ma")
plt.xlabel("t")
plt.ylabel("ma")
plt.title("Regressions")
plt.legend()
plt.show()
plt.savefig("metric_score_ma.png",bbox_inches='tight')
Никаких ошибок нет. График данных, на которых модель обучается, выводится, а ни одна регрессия - нет.