# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
## Based on
#  - https://realpython.com/linear-regression-in-python/
#  - https://stackabuse.com/linear-regression-in-python-with-scikit-learn/
# -

import pandas as pd
import numpy as np

# Local data cache
datadir = '../common-data/covid/'
datafile = datadir + "data.csv"

# Read data from local file
data = pd.read_csv(datafile, parse_dates=['date'])
data.head()

data.info()

# Finding the number of cases in a state and county
condition = (data['state'] =='South Carolina') 
data_sc = data[condition]

# See a sample
data_sc.head()

# Get its statistics
data_sc.info()

# Seeing visually if there is a relationship between cases and deaths
import matplotlib.pyplot as plt
# %matplotlib inline

data_sc.plot(x='cases', y='deaths', style='o')
plt.title('Cases vs Deaths')
plt.xlabel('Number Cases')
plt.ylabel('Number Deaths')
plt.show()

# We will create a column to signify whether data is for Richland county or not
data_sc['richland?'] = data_sc.apply(lambda x: ('Y' if (x['county'] == 'Richland') else 'N'), axis=1 )

data_sc.head()

# Imports for classification
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.linear_model import LogisticRegression

# Get the data of interest
x = data_sc ['cases']
y = data_sc ['richland?']

data_sc['richland?'].value_counts()

# Type and shape changes
x, y = np.array(x), np.array(y)

x = x.reshape(-1,1)

# Get a subset for taining and testing
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

print (x_train.size, x_test.size, y_train.size, y_test.size)

# Train a linear regression mode
logreg = LogisticRegression(C=1e5)
model = logreg.fit(x_train, y_train)

# Model information
print (' Model details:')
print (' -  intercept : ', model.intercept_)
print (' -  coeff : ', model.coef_)

# Predicted values for train and test
y_pred_train = model.predict(x_train)
y_pred_test = model.predict(x_test)

# Confusion matrix
from sklearn.metrics import confusion_matrix

CM = confusion_matrix(y_test, y_pred_test)
print (CM)



StarcoderdataJupyterScriptsDedupFiltered