Created
July 1, 2021 13:26
-
-
Save BenutoKing/dd0370576ae4afd02d77482252296532 to your computer and use it in GitHub Desktop.
Regresión
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import datetime as dt | |
import sklearn | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error | |
from sklearn.metrics import r2_score | |
from sklearn.model_selection import train_test_split | |
mainpath = "C:/Users/Alex1/Documents/Proyecto modular/Datasets" | |
filepath = "02.06.2021_da_radar_pruebas_covid.csv" | |
fullpath = os.path.join(mainpath,filepath) | |
data = pd.read_csv(fullpath,sep = ';') | |
data.head() | |
data.tail() | |
data.drop(data.tail(1).index,inplace=True) | |
data["Fecha"] = pd.to_datetime(data["Fecha"],dayfirst=True) | |
data.tail() | |
fecha_actual = pd.to_datetime("06/18/2021") | |
fecha_actual | |
dias = fecha_actual.toordinal() - data["Fecha"].map(dt.datetime.toordinal) | |
data["Dias transcurridos"] = dias | |
data.tail() | |
data = data.loc[data["Resultado"] == "Confirmados"] | |
data.tail() | |
## Regresión lineal simple | |
## X Variable independiente: Número de días desde el origen de la pandemia | |
## Y Variable dependiente: Número de pruebas confirmadas, descartadas, y sospechosas | |
y = data["Federal"].values.reshape(-1,1) | |
x = data["Dias transcurridos"].values.reshape(-1,1) | |
modelo = LinearRegression() | |
modelo.fit(x,y) | |
plt.scatter(x,y) | |
plt.xlabel("Días transcurridos") | |
plt.ylabel("Pruebas federales") | |
plt.show() | |
x_train, x_test, y_train, y_test = train_test_split(x,y) | |
regresion = LinearRegression() | |
regresion.fit(x_train,y_train) | |
predict = regresion.predict(x_test) | |
plt.scatter(x_test, y_test) | |
plt.plot(x_test,predict,color = 'red', linewidth = 3) | |
plt.title("Regresión lineal") | |
plt.xlabel("Días transcurridos") | |
plt.ylabel("Valor medio") | |
plt.show() | |
print(regresion.coef_) ## "a" | |
print(regresion.intercept_) ## "b" | |
print('y = ', regresion.coef_, 'x ', regresion.intercept_ ) # Ecuacion | |
print(regresion.score(x_train,y_train)) | |
data.tail() | |
valor = (-256.85991993*-1) + (98314.39260225) | |
print(valor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment