# ---
# 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
# ---

# # Prediction of food allergy percetanges

# Load machine learning library scikit-learn

# +
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

import os.path as osp
import gc


# -

# Define a function that will receive data as input and will return a support vector machine trained with the best hiperparameters for that data.

def get_model(x, y):
	regr = SVR()
	pipe = Pipeline(steps=[('reg', regr)])
	param_grid = {
		'reg__kernel':('linear', 'rbf'),
		'reg__C': [0.01, 0.1, 1, 10],
		'reg__epsilon': [0.1, 0.2, 0.4, 0.5, 0.8, 1., 1.5, 2, 3],
		'reg__gamma': ['auto', 'scale'],
	}
	search = GridSearchCV(pipe, param_grid, iid=False, cv=5,
		return_train_score=False, n_jobs = 4)
	search.fit(x, y)
	return search.best_estimator_


# Define a function that loads the data. Source of data https://www.cdc.gov/nchs/hus/contents2017.htm#035

def read(file_name):
	data = pd.read_csv(file_name, sep = '\t')
	x = np.array([[float(year)] for year in list(data)])
	y = np.array([[year] for year in np.array(data).reshape(-1)]).reshape(-1, )
	return x, y

data_root = '../data/machine_learning'
file_names = ['black_african_american.tsv', 'female.tsv', 'hispanic_latino.tsv', 'male.tsv', 'under_18_years.tsv', 'white.tsv']
names = ['black african american', 'female ', 'hispanic latino', 'male', 'under 18 years', 'white']

# Define the years we want to predict food allergy percentages

query = np.array([[2018], [2019], [2020]]).reshape(-1,1)

# Predict food allergy percentages:

for fn, n in zip(file_names, names):
    x, y = read(osp.join(data_root, fn))

    model = get_model(x, y)
    y_model = model.predict(x)
    y_query = model.predict(query)

    fig =plt.figure()
    plt.title(n)
    plt.scatter(x, y,  color='green', label = 'train')
    plt.scatter(query, y_query,  color='black', label = 'test')
    plt.plot(x, y_model, color='blue', linewidth=2, label = 'model')
    plt.legend(loc = 'lower right')

    plt.show()
    for q, pq in zip(query, y_query):
        print("{:.2f} percentage of {} population will have food allergy in {}".format(pq, n, q[0]))


StarcoderdataJupyterScriptsDedupFiltered