<jupyter_start><jupyter_text>Member Task 1:<jupyter_code>import os
import math
# Science libraries
import pandas as pd
import numpy as np
# Ploting
import matplotlib.pyplot as plt
import plotly.express as px
import plotly
import plotly.graph_objs as go
# Machine learning
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error 
import statsmodels.formula.api as smf
# Create folder to hold the graphs
if not os.path.exists("images"):
    os.mkdir("images")
#read super Covid-19 dataframe with state as index. 
Read = pd.read_csv("../../../data/output/covid.csv")
df = Read.drop(["countyFIPS","stateFIPS","Unnamed: 0"], axis=1)
df = df.drop(df.loc[df['County Name'] == "Statewide Unallocated"].index)

#groups the column by States, and adds up cases of each county per state.
States = df.groupby(['State']).sum()

array = ['TX','CA','FL','NY','PA','NC']
MyStates = States.loc[States.index.isin(array)]

State_cases = MyStates.filter(regex = "x").T.rename_axis('Date')
State_deaths = MyStates.filter(regex = "y").T.rename_axis('Date')

#Calculates differences of cases and deaths of each day (New Cases & New Deaths)
New_State_Cases = State_cases.diff()
New_State_Deaths = State_deaths.diff()

#change date format for cases
MyStateCases  = New_State_Cases .T.rename(columns=lambda x: (pd.to_datetime(x.split('_')[0], errors= 'ignore')), errors='raise').T

#change date format for deaths
MyStateDeaths  = New_State_Deaths.T.rename(columns=lambda x: (pd.to_datetime(x.split('_')[0], errors= 'ignore')), errors='raise').T


MyStateCases = MyStateCases.fillna(0).reset_index()
MyStateDeaths = MyStateDeaths.fillna(0).reset_index()

print(MyStateCases.tail())
print(MyStateDeaths.tail())
TexasCases = MyStateCases.TX[43:].reset_index()
del TexasCases['index']


TexasDeaths = MyStateDeaths.TX[11:].reset_index()
del TexasDeaths['index']

TexasCases<jupyter_output><empty_output><jupyter_text>Linear Regression model of the State of Texas New Cases and Deaths.<jupyter_code># create X and y
X = TexasCases.index.values.reshape(-1, 1)
y = TexasCases<jupyter_output><empty_output><jupyter_text>Linear Regression of New Cases<jupyter_code># follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max()+50, 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit)
plt.title('Texas New Cases (Linear Regression)')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>[296.48581495]
[[29.33497351]]<jupyter_text>RMSE<jupyter_code>from sklearn.metrics import mean_squared_error 
import math
mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>4529.950706924409<jupyter_text>NonLinear Regression of New Cases<jupyter_code>poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y)
plt.scatter(X, y)
plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='orange')
plt.title('Texas New Cases (Polynomial)')
plt.xlabel('Number of Days')
plt.ylabel('Cases')

x = TexasCases.index
y = TexasCases["TX"] 
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()<jupyter_output><empty_output><jupyter_text>RMSE<jupyter_code>mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>4529.950706924409<jupyter_text>Linear Regression of New Deaths<jupyter_code># create X and y
X = TexasDeaths.index.values.reshape(-1, 1)
y = TexasDeaths
# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max()+50, 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Texas New Deaths (Linear Regression)')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>[-8.71239837]
[[0.53498839]]<jupyter_text>RMSE<jupyter_code>mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>86.33613994394332<jupyter_text>NonLinear Regression of New Deaths<jupyter_code>poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X)
pol_reg = LinearRegression()
pol_reg.fit(X_poly, y)
plt.scatter(X, y)
plt.plot(X, pol_reg.predict(poly_reg.fit_transform(X)), color='orange')
plt.title('Texas New Deaths (Polynomial)')
plt.xlabel('Number of Days')
plt.ylabel('Cases')

x = TexasDeaths.index
y = TexasDeaths["TX"] 
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()<jupyter_output><empty_output><jupyter_text>RMSE<jupyter_code>mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>86.33613994394332<jupyter_text>Top 5 Texas Counties Linear Regression<jupyter_code>#read texas county data from stage II
TX_county_Cases = pd.read_csv("../../Stage_II/Badesha/Data/TX_County_cases.csv")
TX_county_Deaths = pd.read_csv("../../Stage_II/Badesha/Data/TX_County_deaths.csv")

#fill na values with 0
TX_county_Cases = TX_county_Cases.fillna(0)
TX_county_Deaths = TX_county_Deaths.fillna(0)

#start dataset to first day of infection 
TX_county_Cases = TX_county_Cases[43:].reset_index()
del TX_county_Cases['index']

TX_county_Cases<jupyter_output><empty_output><jupyter_text>Bexar County<jupyter_code># create X and y
X = TX_county_Cases.index.values.reshape(-1, 1)
y = TX_county_Cases['Bexar County']

# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Bexar County New Cases')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>75.27985178323289
[1.56493472]<jupyter_text>Dallas County<jupyter_code># create X and y
X = TX_county_Cases.index.values.reshape(-1, 1)
y = TX_county_Cases['Dallas County']

# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Dallas County New Cases')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>100.39546086150983
[2.49875674]<jupyter_text>Harris County<jupyter_code># create X and y
X = TX_county_Cases.index.values.reshape(-1, 1)
y = TX_county_Cases['Harris County']

# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Harris County New Cases')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>120.52922649374693
[4.3976713]<jupyter_text>Hidalgo County<jupyter_code># create X and y
X = TX_county_Cases.index.values.reshape(-1, 1)
y = TX_county_Cases['Hidalgo County']

# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Hidalgo County New Cases')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>28.52274201018983
[0.93271991]<jupyter_text>Tarrant County<jupyter_code># create X and y
X = TX_county_Cases.index.values.reshape(-1, 1)
y = TX_county_Cases['Tarrant County']

# follow the usual sklearn pattern: import, instantiate, fit
model = LinearRegression()
model.fit(X, y)

# print intercept and coefficients
print (model.intercept_)
print (model.coef_)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max()+7, 1000)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Tarrant County New Cases')
plt.xlabel('Number of Days')
plt.ylabel('Cases')<jupyter_output>-8.120333487725873
[2.2306671]<jupyter_text>Analysis: It looks like Tarrant county linear regression is increasing more than the other counties. Tarrant County looks to be more at risk for more COVID Cases  Utilize Hospital Data to calculate point of no return:<jupyter_code>df = pd.read_csv("../../../data/output/COVID19_HOSBEDS_MERGE.csv")
df = df.drop(["countyFIPS","stateFIPS","Unnamed: 0"], axis=1)
Hospital = df.drop(df.loc[df['County Name'] == "Statewide Unallocated"].index)

#groups the column by States, and adds up cases of each county per state.
HospitalStates = Hospital.groupby(['State']).sum()

MyVar = HospitalStates[['10/21/20_y','NUM_ICU_BEDS']]
Hospital_deaths = MyVar.rename(columns ={"10/21/20_y" : "Deaths"})

Hospital_deaths['Percentage'] = (Hospital_deaths['Deaths']/Hospital_deaths['NUM_ICU_BEDS'])
Hospital_deaths[22:]<jupyter_output><empty_output><jupyter_text>Analysis: It looks like New Jersey had the large number of deaths compared to ICU Beds.  It seems that Wisconsin is closing in to the "point of no return" as its total deaths are nearing to the maximum number of ICU Beds.   Utilize decision tree, Random Forest, and Arima<jupyter_code># create X and y
X = TexasCases.index.values.reshape(-1, 1)
y = TexasCases<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code># Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max()+7, 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Texas New Cases: Random Forest Regression')
plt.xlabel('Number of Days')
plt.ylabel('Cases')

x = TexasCases.index
y = TexasCases["TX"] 
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()
mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>5055.508747066277<jupyter_text>ARIMA<jupyter_code>from pandas import read_csv
from pandas import datetime
from pandas import DataFrame
from statsmodels.tsa.arima_model import ARIMA
from matplotlib import pyplot

series = TexasCases.squeeze()
# fit model
model = ARIMA(series, order=(5,1,0))
model_fit = model.fit(disp=0)
print(model_fit.summary())
# plot residual errors
residuals = DataFrame(model_fit.resid)
residuals.plot()
pyplot.show()
residuals.plot(kind='kde')
pyplot.show()
print(residuals.describe())<jupyter_output>ARIMA Model Results                              
==============================================================================
Dep. Variable:                   D.TX   No. Observations:                  254
Model:                 ARIMA(5, 1, 0)   Log Likelihood               -2262.512
Method:                       css-mle   S.D. of innovations           1783.940
Date:                Sun, 15 Nov 2020   AIC                           4539.025
Time:                        20:34:42   BIC                           4563.786
Sample:                             1   HQIC                          4548.986
                                                                              
==============================================================================
                 coef    std err          z      P>|z|      [0.025      0.975]
------------------------------------------------------------------------------
const         38.8343     34.295      1.132      0.2[...]<jupyter_text>Decision Tree<jupyter_code>from sklearn.tree import DecisionTreeRegressor<jupyter_output><empty_output><jupyter_text>https://scikit-learn.org/stable/auto_examples/tree/plot_tree_regression.html<jupyter_code># Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max()+7, 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Days")
plt.ylabel("Cases")
plt.title("Decision Tree Regression")
plt.legend()
x = TexasCases.index
y = TexasCases["TX"] 
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
plt.show()
mse = mean_squared_error(X_test, y_1)

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>4754.596944055297<jupyter_text>Analysis: The three graphs show different variations of prediction of COVID-19 Cases.  Predict Spread rate(cases and death) of COVID-19 in a county using enrichment variables.<jupyter_code>HospitalVar = Hospital[['County Name','10/21/20_y','NUM_ICU_BEDS','AVG_VENTILATOR_USAGE','BED_UTILIZATION','NUM_STAFFED_BEDS','Potential_Increase_In_Bed_Capac']]
HospitalVar  = HospitalVar.rename(columns ={"10/21/20_y" : "Deaths"})

HospitalVar = HospitalVar.fillna(0)

HospitalVar<jupyter_output><empty_output><jupyter_text>Number of ICU Beds compared to Deaths (Random Forest and Decision Tree)<jupyter_code># create X and y
X = HospitalVar.NUM_ICU_BEDS.values.reshape(-1, 1)
y = HospitalVar['Deaths']
# Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Number Of ICU Beds vs Deaths: Random Forest Regression')
plt.xlabel('Number of ICU Beds')
plt.ylabel('Deaths')

# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max(), 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Number of ICU Beds")
plt.ylabel("Deaths")
plt.title("Number of ICU Beds vs Deaths: Decision Tree Regression")
plt.legend()
plt.show()

mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)

mse = mean_squared_error(X_test, y_1 )

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>C:\Users\harin\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Average Ventilator Usage compared to Deaths (Random Forest and Decision Tree)<jupyter_code># create X and y
X = HospitalVar.AVG_VENTILATOR_USAGE.values.reshape(-1, 1)
y = HospitalVar['Deaths']
# Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Average Ventilator vs Deaths: Random Forest Regression')
plt.xlabel('Average Ventilator Usage')
plt.ylabel('Deaths')

# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max(), 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Average Ventilator Usage")
plt.ylabel("Deaths")
plt.title("Average Ventilator vs Deaths: Decision Tree Regression")
plt.legend()
plt.show()

mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)

mse = mean_squared_error(X_test, y_1 )

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>C:\Users\harin\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Number of Staffed Beds compared to Deaths (Random Forest and Decision Tree)<jupyter_code># create X and y
X = HospitalVar.NUM_STAFFED_BEDS.values.reshape(-1, 1)
y = HospitalVar['Deaths']
# Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Number of Staffed Beds vs Deaths: Random Forest Regression')
plt.xlabel('Number of Staffed Beds')
plt.ylabel('Deaths')


# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max(), 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Number of Staffed Beds")
plt.ylabel("Deaths")
plt.title("Number of Staffed Beds vs Deaths: Decision Tree Regression")
plt.legend()
plt.show()

mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)

mse = mean_squared_error(X_test, y_1 )

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>C:\Users\harin\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Bed Utilization compared to Deaths (Random Forest and Decision Tree)<jupyter_code># create X and y
X = HospitalVar.BED_UTILIZATION.values.reshape(-1, 1)
y = HospitalVar['Deaths']
# Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Bed Utilization vs Deaths: Random Forest Regression')
plt.xlabel('Bed Utilization ')
plt.ylabel('Deaths')


# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max(), 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Bed Utilization ")
plt.ylabel("Deaths")
plt.title("Bed Utilization  vs Deaths: Decision Tree Regression")
plt.legend()
plt.show()

mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)

mse = mean_squared_error(X_test, y_1 )

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>C:\Users\harin\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Potential increase to bed capacity compared to Deaths (Random Forest and Decision Tree)<jupyter_code># create X and y
X = HospitalVar.Potential_Increase_In_Bed_Capac.values.reshape(-1, 1)
y = HospitalVar['Deaths']
# Fit a Random Forest
from sklearn.ensemble import RandomForestRegressor
model = RandomForestRegressor()
model.fit(X, y)

# Plot the data and the model prediction
X_fit = np.linspace(X.min(), X.max(), 100)[:, np.newaxis]
y_fit = model.predict(X_fit)

plt.plot(X.squeeze(), y, 'o')
plt.plot(X_fit.squeeze(), y_fit);
plt.title('Potential increase to bed capacity vs Deaths: Random Forest Regression')
plt.xlabel('Potential increase to bed capacity')
plt.ylabel('Deaths')


# Fit regression model
regr_1 = DecisionTreeRegressor(max_depth=2)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X, y)
regr_2.fit(X, y)

# Predict
X_test = np.arange(0.0, X.max(), 0.01)[:, np.newaxis]
y_1 = regr_1.predict(X_test)
y_2 = regr_2.predict(X_test)

# Plot the results
plt.figure()
plt.scatter(X, y, s=20, edgecolor="black",
            c="darkorange", label="data")
plt.plot(X_test, y_1, color="cornflowerblue",
         label="max_depth=2", linewidth=2)
plt.plot(X_test, y_2, color="yellowgreen", label="max_depth=5", linewidth=2)
plt.xlabel("Potential increase to bed capacity")
plt.ylabel("Deaths")
plt.title("Potential increase to bed capacity  vs Deaths: Decision Tree Regression")
plt.legend()
plt.show()

mse = mean_squared_error(X_fit, y_fit)

rmse = math.sqrt(mse)
print(rmse)

mse = mean_squared_error(X_test, y_1 )

rmse = math.sqrt(mse)
print(rmse)<jupyter_output>C:\Users\harin\Anaconda3\lib\site-packages\sklearn\ensemble\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>Anaylsis: We can see that a lot of the enrichment variable have a similar trend in random forest regression plot.  Forest Regression RMSE seems to be greater in bed capacity and bed utilization but its RMSE is lesser in number of staffed beds, ventilator usage and ICU beds ICU beds and Staffed Beds are relatively important variables because their trend has a very positive slope which predicts that the more beds that are staffed, the more deaths are occuring  Task 2: Confidence Interval, Trend Line, Path<jupyter_code>TexasCases = TexasCases.reset_index()
MyTexasCases = TexasCases.rename(columns={"index":"Days","TX":'Cases'})
MyTexasCases<jupyter_output><empty_output><jupyter_text>Replot linear regression for Texas New cases using StatModels for Trend Line and Prediction path<jupyter_code>import statsmodels.formula.api as smf

lm = smf.ols(formula='Cases ~ Days', data=MyTexasCases).fit()

X_new = pd.DataFrame({'Days': [MyTexasCases.Days.min(),MyTexasCases.Days.max()]})

preds = lm.predict(X_new)

MyTexasCases.plot(kind='scatter', x = 'Days', y = 'Cases')
plt.plot(X_new, preds, c='red', linewidth=2)<jupyter_output><empty_output><jupyter_text>Confidence Interval for Linear Texas New Cases<jupyter_code>lm.conf_int()<jupyter_output><empty_output><jupyter_text>Replot linear regression for Texas New deaths using StatModels for trend line and prediction path<jupyter_code>TexasDeaths = TexasDeaths.reset_index()
MyTexasDeaths = TexasDeaths.rename(columns={"index":"Days","TX":'Deaths'})
import statsmodels.formula.api as smf

lm = smf.ols(formula='Deaths ~ Days', data=MyTexasDeaths ).fit()

X_new = pd.DataFrame({'Days': [MyTexasDeaths.Days.min(),MyTexasDeaths.Days.max()]})

preds = lm.predict(X_new)

MyTexasDeaths .plot(kind='scatter', x = 'Days', y = 'Deaths')
plt.plot(X_new, preds, c='red', linewidth=2)<jupyter_output><empty_output><jupyter_text>confidence Interval for Texas New Death<jupyter_code>lm.conf_int()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1448446499553825, 'fraction_numerical': 0.03094832481544577, 'mean_word_length': 5.433977035490606, 'pattern_counts': {'":': 4, '<': 114, '<?xml version=': 0, '>': 115, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '33', 'dir': 'jupyter-structured-clean-dedup', 'id': '439584', 'license': 'MIT', 'n_tokens_mistral': 10058, 'n_tokens_neox': 9166, 'n_words': 2065, 'path': 'src/Stage_III/Badesha/StageIII_Member.ipynb', 'repo_name': 'HarinB4/Covid-19DataProject', 'size': '24654'}