<jupyter_start><jupyter_text>Importing the modules<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Loading dataset from Github.[0 = Absent, 1 = Present]<jupyter_code>url = 'https://raw.githubusercontent.com/Azmal16/Covid_Symptoms_Predict_with_Machine_Learning/main/Cleaned-Data.csv'
df = pd.read_csv(url)
display(df)
print(df.shape)
display(df.describe())
df.info()
df.columns<jupyter_output><empty_output><jupyter_text>Putting all the columns related to severity in a single variable.<jupyter_code>severity_columns = df.filter(like='Severity_').columns
severity_columns<jupyter_output><empty_output><jupyter_text>Getting rid of unnecessary columns  We want to keep our dataset as simple as possible. Also,we want to take as less inputs as possible from the users. That’s why we are removing the columns we don’t want to use for the sake of simplicity(In deplyment, we have also removed the Age, Gender, Contact_Don't_know and Contact_No columns too).<jupyter_code>df.drop("Country",axis=1,inplace=True)
df.drop("None_Sympton",axis=1,inplace=True)
df.drop("None_Experiencing",axis=1,inplace=True)
df.drop(severity_columns,axis=1,inplace=True)
#Only in deployment
'''df.drop("Age_0-9",axis=1,inplace=True)
df.drop("Age_10-19",axis=1,inplace=True)
df.drop("Age_20-24",axis=1,inplace=True)
df.drop("Age_25-59",axis=1,inplace=True)
df.drop("Age_60+",axis=1,inplace=True)
df.drop("Gender_Female",axis=1,inplace=True)
df.drop("Gender_Transgender",axis=1,inplace=True)
df.drop("Contact_Dont-Know",axis=1,inplace=True)
df.drop("Contact_No",axis=1,inplace=True)'''
df<jupyter_output><empty_output><jupyter_text>Creating Target Variable As we can see, there is no attribute in this dataset which tells us whether a person is Covid positive or not. So, we are adding a new column called 'Symptoms_Score'. It will be our target variable. We will predict the 'Symptoms_Score's of the users from the data provided by themselves.<jupyter_code>df['Symptoms_Score'] = df.iloc[:,:9].sum(axis=1) + df.iloc[:,19:20].sum(axis=1)#for deployment 10:11
df<jupyter_output><empty_output><jupyter_text>This is the final dataset we want to use and apply our models to.<jupyter_code>display(df.describe())
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 316800 entries, 0 to 316799
Data columns (total 21 columns):
 #   Column                   Non-Null Count   Dtype
---  ------                   --------------   -----
 0   Fever                    316800 non-null  int64
 1   Tiredness                316800 non-null  int64
 2   Dry-Cough                316800 non-null  int64
 3   Difficulty-in-Breathing  316800 non-null  int64
 4   Sore-Throat              316800 non-null  int64
 5   Pains                    316800 non-null  int64
 6   Nasal-Congestion         316800 non-null  int64
 7   Runny-Nose               316800 non-null  int64
 8   Diarrhea                 316800 non-null  int64
 9   Age_0-9                  316800 non-null  int64
 10  Age_10-19                316800 non-null  int64
 11  Age_20-24                316800 non-null  int64
 12  Age_25-59                316800 non-null  int64
 13  Age_60+                  316800 non-null  int64
 14  Gender_Female            316800 non-[...]<jupyter_text>Thankfully, there is no non-null value in any column. Looking for correlations:<jupyter_code>corr_matrix = df.corr()
corr_matrix["Symptoms_Score"].sort_values(ascending=False)<jupyter_output><empty_output><jupyter_text>As we see here, Dry-Cough, Tiredness, Breathing Difficulty, Runny Nose and Nasal Congestion mostly affect the Symptoms_Score. Data Visualization<jupyter_code>import matplotlib.pyplot as plt
df.hist(bins = 10,figsize=(20,15))
plt.show()<jupyter_output><empty_output><jupyter_text>Separating Data and target<jupyter_code>X = df.iloc[:, :20].values #for deployment [:, :11]
y = df.iloc[:,20:21].values #for deployment [:, 11:12]
X
y
X.shape
y.shape<jupyter_output><empty_output><jupyter_text>Splitting Train and Test dataset.<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)<jupyter_output><empty_output><jupyter_text>A function for calculating the performance measurements of different Models.<jupyter_code>from sklearn import metrics
def performance_measurements(model_name,pred_data):
  cm = metrics.confusion_matrix(y_test, pred_data) 
  print("Confusion Matrix of"+model_name+":",cm)

  accuracy = metrics.accuracy_score(y_test, pred_data) 
  print("Accuracy score of "+model_name+":",accuracy)  

  precision = metrics.precision_score(y_test, pred_data,pos_label='positive',
                                           average='micro') 
  print("Precision score of"+model_name+":",precision)

  recall = metrics.recall_score(y_test, pred_data,pos_label='positive',
                                           average='micro') 
  print("Recall score of"+model_name+":",recall)

  f1_score = metrics.f1_score(y_test, pred_data,pos_label='positive',
                                           average='micro')
  print("F1 score of"+model_name+":",f1_score)<jupyter_output><empty_output><jupyter_text>Trying our Models:  1. Linear Regression Fitting:<jupyter_code>from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Prediction:<jupyter_code>y_pred_lr = lr.predict(X_test).astype(int)
y_pred_lr<jupyter_output><empty_output><jupyter_text>Performance Measurement of Linear Regression:<jupyter_code>performance_measurements("Linear Regression",y_pred_lr)<jupyter_output>Confusion Matrix ofLinear Regression: [[ 240    0    0    0    0    0    0    0    0    0    0]
 [2028  257    0    0    0    0    0    0    0    0    0]
 [   0 5921 1620    0    0    0    0    0    0    0    0]
 [   0    0 7852 4064    0    0    0    0    0    0    0]
 [   0    0    0 6356 6892    0    0    0    0    0    0]
 [   0    0    0    0 3912 7972    0    0    0    0    0]
 [   0    0    0    0    0 1570 6892    0    0    0    0]
 [   0    0    0    0    0    0  275 4492    0    0    0]
 [   0    0    0    0    0    0    0   27 2110    0    0]
 [   0    0    0    0    0    0    0    0    0  767    0]
 [   0    0    0    0    0    0    0    0    0    0  113]]
Accuracy score of Linear Regression: 0.5590119949494949
Precision score ofLinear Regression: 0.5590119949494949
Recall score ofLinear Regression: 0.5590119949494949
F1 score ofLinear Regression: 0.5590119949494949<jupyter_text>2. K-Neighbours-Regressor Fitting:<jupyter_code>from sklearn.neighbors import KNeighborsRegressor
knr = KNeighborsRegressor(n_neighbors=5)
knr.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Prediction:<jupyter_code>y_pred_knr = knr.predict(X_test).astype(int)<jupyter_output><empty_output><jupyter_text>Performance measurement of K-Neighbours-Regressor:<jupyter_code>performance_measurements("KNeighborsRegressor",y_pred_knr)<jupyter_output><empty_output><jupyter_text>3. Decision Tree Regressor Fitting:<jupyter_code>from sklearn.tree import DecisionTreeRegressor
dtr = DecisionTreeRegressor(random_state=0)
dtr.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Prediction:<jupyter_code>y_pred_dtr = dtr.predict(X_test).astype(int)<jupyter_output><empty_output><jupyter_text>Performance of Decision Tree Regressor:<jupyter_code>performance_measurements("Decision Tree  Regression",y_pred_dtr)<jupyter_output><empty_output><jupyter_text>K Neighbours Regressor and Decision Tree Regressor provides surprisingly good and similar accuracy. We can implement either of these models for our deployment. Evaluating Cross-Validation scores for each models.<jupyter_code>results = []
names = []
models = []
models.append(("LR", lr))
models.append(("KNR", knr))
models.append(("DTR", dtr))
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
for name, model in models:
	kfold = StratifiedKFold(n_splits=5, random_state=0, shuffle=True)
	cv_results = cross_val_score(model, X_train, y_train, cv=kfold)
	results.append(cv_results)
	names.append(name)
	print('%s: %f (%f)' % (name, cv_results.mean(), cv_results.std()))<jupyter_output><empty_output><jupyter_text>All of the models show similar CV scores.<jupyter_code>df<jupyter_output><empty_output><jupyter_text>Custom prediction with DTR model<jupyter_code>symptoms_matrix = [[1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]]
final_prediction = dtr.predict(symptoms_matrix).astype(int)*10
fp = final_prediction[0].astype(str)
if symptoms_matrix[0][19] == 1:
  print("Your symptoms match with " + fp + "% of the Covid symptoms.\nAs you have been in contact with a Covid patient,\n we highly recommend you to have a test and stay isolated unitil the result comes.")
elif symptoms_matrix[0][19] == 0:
  print("Your symptoms match with " + fp + "% of the Covid symptoms.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.105028927458834, 'fraction_numerical': 0.05763239875389408, 'mean_word_length': 4.090033975084938, 'pattern_counts': {'":': 5, '<': 92, '<?xml version=': 0, '>': 92, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '207281', 'license': 'MIT', 'n_tokens_mistral': 3477, 'n_tokens_neox': 3103, 'n_words': 732, 'path': 'Data Analysis and Feature Engineering/Covid_Symptoms_Checker.ipynb', 'repo_name': 'Azmal16/Covid_Symptoms_Predict_with_Machine_Learning', 'size': '8988'}