# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.7.6 64-bit (''ds-Flask'': conda)'
#     name: python37664bitdsflaskconda5d6acf79da9542b3aeeb7103e7ad852b
# ---

# + [markdown] id="RfqZ-DJyMV6w" colab_type="text"
# # TFIDF KNN Approach 

# + id="2F9FXBToMbtd" colab_type="code" colab={}
import pandas as pd

import spacy
from spacy.tokenizer import Tokenizer
from spacy.lang.en import English

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neighbors import NearestNeighbors

import pickle

# + id="3lmqxnzPMbos" colab_type="code" colab={}
# Instantiate the tokenizer 
nlp=English()
tokenizer = Tokenizer(nlp.vocab)

# + id="zcHGr5LwMbmE" colab_type="code" colab={}
df = pd.read_csv("https://raw.githubusercontent.com/Build-Week-Med-Cabinet-6/DS/mark-dev/data/cannabis.csv")
df.head()

# + id="Ai8m8CZAMbjy" colab_type="code" colab={}
# drop nan or missing values
df = df.dropna()
df = df.drop(df.index[149])
#df = df.reset_index(drop=True)




# + id="O5NOVr4KQ1wz" colab_type="code" colab={}


# + id="A_tnPRrH22pO" colab_type="code" colab={}


# + id="DmG3Uj7DMbhJ" colab_type="code" colab={}
# Combine the Effects and Flavors in one column
df['Combined'] = df['Effects'] + ',' + df['Flavor']


# + id="Wb1hUFEiQXrD" colab_type="code" colab={}
# Instantiate vecorizer object - call tokenize
dtm_combined_tf = TfidfVectorizer(stop_words='english')


# dtm_combined  (vocabulary) and get word counts 
# effects and flavors combined 
dtm_combined = dtm_combined_tf.fit_transform(df['Combined'].values.astype('U'))
dtm_combined = pd.DataFrame(dtm_combined.todense(), columns=dtm_combined_tf.get_feature_names())
dtm_combined.head()

# + id="smEmTZ5ORe52" colab_type="code" colab={}
# Fit on TF-IDF Vectors
nn = NearestNeighbors(n_neighbors=5, algorithm='ball_tree')
nn.fit(dtm_combined)

# + id="BIMinWmrRp4H" colab_type="code" colab={}
# Practice passing a strain to the model with this string
ideal_strain = ['Creative,Energetic,Tingly,Euphoric,Relaxed,Earthy,Sweet,Citrus']


# + id="EyPWaR-wYMoO" colab_type="code" colab={}
# Query for similar strains using the test case
new = dtm_combined_tf.transform(ideal_strain)
results = nn.kneighbors(new.todense())

# + id="L7do34AQYMm3" colab_type="code" colab={}
# Results are returned in a tuple of arrays
results[1][0]

# + id="I2rPgrI3lA9M" colab_type="code" colab={}
type(results)

# + id="aR9PpMF7YMlS" colab_type="code" colab={}
# Pull the strain name from 1st value (0) of the 1st array (0) of the 2nd tuple (1) - the 0 index
df['Strain'][results[1][0][0]]


# + id="C4lPTojobvOa" colab_type="code" colab={}
df['Combined'][results[1][0][0]]

# + id="DrtjNSNubvMF" colab_type="code" colab={}
df['Description'][results[1][0][0]]

# + id="LFUpOCgSbvJh" colab_type="code" colab={}


# + [markdown] id="34NqaZSwboUl" colab_type="text"
# ### Second Value 

# + id="VMGgkCJGYMhK" colab_type="code" colab={}
# Pull the strain name from 2nd value (1) of the 1st array (0) of the 2nd tuple (1) - the 1972 index
df['Strain'][results[1][0][1]]

# + id="TwTwjUzpaLPW" colab_type="code" colab={}
# Pull the criteria from 2nd value (1) of the 1st array (0) of the 2nd tuple (1) - the 1972 index
df['Combined'][results[1][0][1]]

# + id="Qe2XQHC5YMeL" colab_type="code" colab={}
# Pull the criteria from 2nd value (1) of the 1st array (0) of the 2nd tuple (1) - the 1972 index
df['Description'][results[1][0][1]]

# + colab_type="code" id="E7fXJNKsfcF4" colab={}
# Imagine doing a detailed return of information afterwards 
# #output results here, rec strains criteria and description
# rec_str = [strains['Strain'][results[1][0][i]] for i in range(5)]
# rec_crit = [strains['Combined'][results[1][0][i]] for i in range(5)]
# rec_str_desc = [strains['Description'][results[1][0][i]] for i in range(5)]

# rec1 = rec_str[0] + ' * ' + rec_crit[0] + ' * ' + rec_str_desc[0]
# rec2 = rec_str[1] + ' * ' + rec_crit[1] + ' * ' + rec_str_desc[1]
# rec3 = rec_str[2] + ' * ' + rec_crit[2] + ' * ' + rec_str_desc[2]
# rec4 = rec_str[3] + ' * ' + rec_crit[3] + ' * ' + rec_str_desc[3]
# rec5 = rec_str[4] + ' * ' + rec_crit[4] + ' * ' + rec_str_desc[4]


# + [markdown] id="RrsvE1pZzn4i" colab_type="text"
# ### Pickling step here

# + id="USIl6KZOe3oa" colab_type="code" colab={}
# Pickle the dtm and tf for use in the prediction
#pickle.dump(dtm_combined, open('/content/dtm_combined.pkl', 'wb'))
pickle.dump(dtm_combined_tf, open('../pickles/dtm_combined_tf.pickle', 'wb'))
with open('../pickles/nn.pickle','wb') as fp:
    pickle.dump(nn,fp)

# + id="eH-wTv7pzqqD" colab_type="code" colab={}


# + [markdown] id="mMtpbHkWSPUp" colab_type="text"
# # Effects

# + id="-74HkjnWSVMH" colab_type="code" colab={}
# Instantiate vecorizer object - call tokenize
dtm_effects_tf = TfidfVectorizer(stop_words='english')


# dtm_effects  (vocabulary) and get word counts 
# effects and flavors effects 
dtm_effects = dtm_effects_tf.fit_transform(df['Effects'].values.astype('U'))
dtm_effects = pd.DataFrame(dtm_effects.todense(), columns=dtm_effects_tf.get_feature_names())
dtm_effects.head()

# + id="B4W2Ne_-XdmN" colab_type="code" colab={}
df.head()

# + [markdown] id="CL3MD1Q9SSyo" colab_type="text"
# # Flavors

# + id="ymTgOC1zS-MJ" colab_type="code" colab={}
# Instantiate vecorizer object - call tokenize
dtm_flavors_tf = TfidfVectorizer(stop_words='english')


# dtm_flavors  (vocabulary) and get word counts 
# flavors 
dtm_flavors = dtm_flavors_tf.fit_transform(df['Flavor'].values.astype('U'))
dtm_flavors = pd.DataFrame(dtm_flavors.todense(), columns=dtm_flavors_tf.get_feature_names())
dtm_flavors.head()

# + [markdown] id="5nJjum1ZvqGS" colab_type="text"
# # Leafly API EDA 
#

# + id="iBsKVRVbvtpP" colab_type="code" colab={}
import pandas as pd 

# + id="n3fY77Yw6IGx" colab_type="code" colab={}
df = pd.read_csv("https://raw.githubusercontent.com/Build-Week-Med-Cabinet-6/DS/mark-dev/data/cannabis.csv")
df.head()

# + id="L_lPy4oM8FEd" colab_type="code" colab={}
df.shape

# + id="F66Q0q4t8NwN" colab_type="code" colab={}
df = df.dropna()


# + id="wvXZ9vvZ8Ql8" colab_type="code" colab={}
df.shape

# + id="kv08GL068SKl" colab_type="code" colab={}
df.describe(exclude='number')

# + id="BPZhHbjSkY3Z" colab_type="code" colab={}
pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip


# + id="joUifMN58rua" colab_type="code" colab={}

import pandas_profiling
df.profile_report()

# + id="zBiqcySq8wYo" colab_type="code" colab={}


# + [markdown] id="WWd4YokVvChK" colab_type="text"
# # Kushy API 

# + id="XUbMm-CyVl68" colab_type="code" colab={}
import pandas as pd 

# + id="H1ZxPJP7VoF_" colab_type="code" colab={}
df = pd.read_csv("https://raw.githubusercontent.com/kushyapp/cannabis-dataset/master/Dataset/Strains/strains-kushy_api.2017-11-14.csv")

# + id="VR3ANWphVtva" colab_type="code" colab={}
df.head()

# + id="zCh6GGddW1vJ" colab_type="code" colab={}
df.shape

# + id="hD3qkLmxVvI6" colab_type="code" colab={}
df.isnull().values.sum()

# + id="nN0N1j3bVych" colab_type="code" colab={}
df.info()

# + id="4qGJ2NArWvEW" colab_type="code" colab={}
# These are the columns with a percentage of missing values. 
df.isnull().sum()/len(df)*100


# + id="1p0MIHSjXAqU" colab_type="code" colab={}
df.describe(exclude="number")

# + id="dinojcUpsY50" colab_type="code" colab={}
df.describe(include="number")

# + id="fy95Mk_VuiKC" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered