Drug Consumption Patterns#

Author: Salaar Saraj

Course Project, UC Irvine, Math 10, Summer 2023

Introduction#

Introduce your project here. Maybe 3 sentences.

I found a drug consumption dataset from https://www.kaggle.com/datasets/obeykhadija/drug-consumptions-uci that I plan to use to try to predict the effect of using alcohol, cannabis, and nicotine when it comes to using harder drugs. I a going to clean the data and use feature engineering to group categories of drugs. I plan to use all the personality and drug history information, to train ML models and test them to see what results I can gain from the data.

Displaying ML models for understanding drug use.#

import pandas as pd
import numpy as np
df =  pd.read_csv("Drug_Consumption_Quantified.csv")
df.head
<bound method NDFrame.head of         ID      Age   Gender  Education  Country  Ethnicity   Nscore   Escore  \
0        2 -0.07854 -0.48246    1.98437  0.96082   -0.31685 -0.67825  1.93886   
1        3  0.49788 -0.48246   -0.05921  0.96082   -0.31685 -0.46725  0.80523   
2        4 -0.95197  0.48246    1.16365  0.96082   -0.31685 -0.14882 -0.80615   
3        5  0.49788  0.48246    1.98437  0.96082   -0.31685  0.73545 -1.63340   
4        6  2.59171  0.48246   -1.22751  0.24923   -0.31685 -0.67825 -0.30033   
...    ...      ...      ...        ...      ...        ...      ...      ...   
1879  1884 -0.95197  0.48246   -0.61113 -0.57009   -0.31685 -1.19430  1.74091   
1880  1885 -0.95197 -0.48246   -0.61113 -0.57009   -0.31685 -0.24649  1.74091   
1881  1886 -0.07854  0.48246    0.45468 -0.57009   -0.31685  1.13281 -1.37639   
1882  1887 -0.95197  0.48246   -0.61113 -0.57009   -0.31685  0.91093 -1.92173   
1883  1888 -0.95197 -0.48246   -0.61113  0.21128   -0.31685 -0.46725  2.12700   

       Oscore   AScore  ...  Ecstasy  Heroin  Ketamine Legalh  LSD Meth  \
0     1.43533  0.76096  ...      CL4     CL0       CL2    CL0  CL2  CL3   
1    -0.84732 -1.62090  ...      CL0     CL0       CL0    CL0  CL0  CL0   
2    -0.01928  0.59042  ...      CL0     CL0       CL2    CL0  CL0  CL0   
3    -0.45174 -0.30172  ...      CL1     CL0       CL0    CL1  CL0  CL0   
4    -1.55521  2.03972  ...      CL0     CL0       CL0    CL0  CL0  CL0   
...       ...      ...  ...      ...     ...       ...    ...  ...  ...   
1879  1.88511  0.76096  ...      CL0     CL0       CL0    CL3  CL3  CL0   
1880  0.58331  0.76096  ...      CL2     CL0       CL0    CL3  CL5  CL4   
1881 -1.27553 -1.77200  ...      CL4     CL0       CL2    CL0  CL2  CL0   
1882  0.29338 -1.62090  ...      CL3     CL0       CL0    CL3  CL3  CL0   
1883  1.65653  1.11406  ...      CL3     CL0       CL0    CL3  CL3  CL0   

     Mushrooms Nicotine Semer  VSA  
0          CL0      CL4   CL0  CL0  
1          CL1      CL0   CL0  CL0  
2          CL0      CL2   CL0  CL0  
3          CL2      CL2   CL0  CL0  
4          CL0      CL6   CL0  CL0  
...        ...      ...   ...  ...  
1879       CL0      CL0   CL0  CL5  
1880       CL4      CL5   CL0  CL0  
1881       CL2      CL6   CL0  CL0  
1882       CL3      CL4   CL0  CL0  
1883       CL3      CL6   CL0  CL2  

[1884 rows x 32 columns]>
print(df["Alcohol"])
print(df["Cannabis"])
print(df["Nicotine"])
0       CL5
1       CL6
2       CL4
3       CL4
4       CL2
       ... 
1879    CL5
1880    CL5
1881    CL4
1882    CL5
1883    CL4
Name: Alcohol, Length: 1884, dtype: object
0       CL4
1       CL3
2       CL2
3       CL3
4       CL0
       ... 
1879    CL5
1880    CL3
1881    CL6
1882    CL6
1883    CL3
Name: Cannabis, Length: 1884, dtype: object
0       CL4
1       CL0
2       CL2
3       CL2
4       CL6
       ... 
1879    CL0
1880    CL5
1881    CL6
1882    CL4
1883    CL6
Name: Nicotine, Length: 1884, dtype: object
df["AMN"] = ((df["Cannabis"] != "CL0") & (df["Alcohol"] != "CL0") & (df["Nicotine"] != "CL0")) #People who have consumed Alcohol, Cannabis, and Nicotine
df["AMN"] = df["AMN"].astype(int)
hard_drugs = ["Meth", "Heroin", "Benzos", "Amphet", "Crack", "Ketamine"]
df["HRD"] = df[hard_drugs].apply(lambda rw: np.max([int(USAGE[2]) for USAGE in rw]), axis=1) #Highest level of consumption
print(df["AMN"])
print(df["HRD"])
0       1
1       0
2       1
3       1
4       0
       ..
1879    0
1880    1
1881    1
1882    1
1883    1
Name: AMN, Length: 1884, dtype: int64
0       3
1       0
2       3
3       1
4       0
       ..
1879    0
1880    4
1881    6
1882    0
1883    3
Name: HRD, Length: 1884, dtype: int64

Now we have cleaned the data such that Alcohol, Cannabis, and Nicotine are grouped together, against hard drugs. So now we can see if we can draw more relationships between them and what personality aspects can correlate to different drug uses. The data will be split into training and test separations so that we can check for overfitting and see how our models perform on unseen data.

from sklearn.model_selection import train_test_split
inputs_attr = ["Age", "Gender", "Education", "Country", "Ethnicity", "Nscore", "Escore", "Oscore", "AScore", "Cscore", "Impulsive", "SS", "AMN"]
targets_attr = "HRD"
X = df[inputs_attr]
y = df[targets_attr]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=21)
print(X_train, X_test, y_train, y_test)
          Age   Gender  Education  Country  Ethnicity   Nscore   Escore  \
1219 -0.95197 -0.48246   -0.61113 -0.57009   -0.31685 -0.24649  0.63779   
1812  1.09449 -0.48246    0.45468  0.96082   -0.31685 -1.86962  0.00332   
934  -0.95197 -0.48246   -0.61113 -0.28519   -0.31685 -0.05188  0.32197   
1159 -0.07854  0.48246    1.98437  0.96082   -0.31685 -0.14882 -0.94779   
1441  1.09449  0.48246    1.16365  0.96082   -0.31685 -1.43907  0.16767   
...       ...      ...        ...      ...        ...      ...      ...   
48   -0.07854  0.48246    1.16365  0.96082   -0.22166  0.82562 -0.57545   
772  -0.07854 -0.48246   -0.61113 -0.57009   -0.31685  0.13606 -0.43999   
1848 -0.07854 -0.48246   -0.05921 -0.57009    0.11440  0.41667  0.96248   
1231  1.82213 -0.48246    1.16365 -0.57009   -0.31685  0.13606  0.63779   
969  -0.95197 -0.48246   -0.61113 -0.57009   -0.31685  1.49158 -1.23177   

       Oscore   AScore   Cscore  Impulsive       SS  AMN  
1219  0.29338 -0.45321 -0.00665    1.29221  1.22470    1  
1812  0.14143 -0.76096 -0.14277    0.52975  0.40148    0  
934   0.58331  0.59042 -0.89891    0.88113  1.22470    1  
1159  0.29338  0.94156  2.04506   -0.71126 -0.52593    0  
1441  0.29338  0.94156  1.13407   -1.37983 -1.18084    1  
...       ...      ...      ...        ...      ...  ...  
48   -1.97495 -0.91699  0.25953    0.52975  1.22470    0  
772  -0.84732 -0.30172 -0.00665   -0.71126 -1.54858    1  
1848 -0.17779 -0.45321  0.75830    0.19268 -0.21575    1  
1231  0.58331 -0.30172  0.58489   -0.21712 -0.84637    1  
969  -1.11902  0.13136 -0.52745   -0.71126  0.40148    1  

[1318 rows x 13 columns]           Age   Gender  Education  Country  Ethnicity   Nscore   Escore  \
571   1.82213  0.48246   -0.61113 -0.57009   -0.31685 -0.92104  0.96248   
1336 -0.95197 -0.48246   -1.22751 -0.57009    0.11440 -0.58016  3.27393   
1290 -0.95197  0.48246   -0.61113 -0.57009   -0.31685  1.83990 -2.21069   
1432  1.82213  0.48246   -0.05921  0.96082   -0.31685 -0.79151 -0.57545   
945   0.49788 -0.48246   -0.05921 -0.57009   -0.31685  1.60383 -1.76250   
...       ...      ...        ...      ...        ...      ...      ...   
1577  1.09449  0.48246   -0.05921  0.96082   -0.31685  0.62967 -1.50796   
911  -0.95197  0.48246   -0.61113 -0.57009   -0.31685 -1.19430  2.32338   
1759 -0.95197  0.48246    0.45468 -0.28519   -0.31685 -0.79151 -1.09207   
996  -0.95197 -0.48246   -1.73790 -0.46841   -0.31685  1.23461 -1.09207   
1173 -0.95197 -0.48246   -0.61113 -0.28519   -0.31685  0.82562 -0.15487   

       Oscore   AScore   Cscore  Impulsive       SS  AMN  
571   0.44585 -0.60633 -0.40581    0.52975 -0.21575    0  
1336  2.15324 -0.60633  0.93949    1.86203  1.22470    1  
1290  0.72330 -0.76096 -1.01450   -0.21712 -0.52593    1  
1432  0.29338  0.28783 -1.13788    1.86203  0.40148    1  
945  -0.01928  0.13136 -1.51840    0.88113 -0.52593    1  
...       ...      ...      ...        ...      ...  ...  
1577 -2.39883 -0.91699  0.12331   -1.37983 -2.07848    0  
911   1.24033  0.94156  0.93949    0.19268  0.76540    1  
1759  1.88511  0.28783  3.00537   -0.21712  1.22470    1  
996  -0.31776 -0.15487 -0.65253    1.29221  0.76540    0  
1173 -0.01928 -1.34289 -0.89891   -1.37983  0.07987    0  

[566 rows x 13 columns] 1219    5
1812    0
934     3
1159    2
1441    0
       ..
48      0
772     6
1848    2
1231    3
969     3
Name: HRD, Length: 1318, dtype: int64 571     2
1336    5
1290    3
1432    0
945     1
       ..
1577    2
911     0
1759    3
996     0
1173    6
Name: HRD, Length: 566, dtype: int64
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_train, y_train)
reg.score(X_train, y_train)
0.3810814646930162
reg.score(X_test, y_test)
0.2954633205147168

We can see that the linear regression doesn’t give too much accuracy in predicting hard drug usage, based on knowledge of using common drugs and personality information. We can try including the specific data on alcohol, Cannabis, and Nicotine usage to see if these improve accuracy.

df["Alcohol_quantified"] = df["Alcohol"].str[2].astype(int)
df["Cannabis_quantified"] = df["Cannabis"].str[2].astype(int)
df["Nicotine_quantified"] = df["Nicotine"].str[2].astype(int)
inputs_attr += ["Alcohol_quantified"] + ["Cannabis_quantified"] + ["Nicotine_quantified"]
X = df[inputs_attr]
y = df[targets_attr]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=21)
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_train, y_train)
print(reg.score(X_train, y_train))
print(reg.score(X_test, y_test))
0.4075891647178682
0.3223516385977905

We notice there is an improvement, but not too substantially to predict accurately

import altair as alt
y_pred = reg.predict(X_test)
reg_results = pd.DataFrame({"Alc": X_test["Alcohol_quantified"], "Can": X_test["Cannabis_quantified"], "Nic": X_test["Nicotine_quantified"], "Pred": reg.predict(X_test)})
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)
reg_results["HRD"] = y_test
scatter_true = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="HRD",
    color=alt.value("red"),
    size="Nic:Q",
    opacity="Can:Q"
)
# scatter_true
alt.hconcat(scatter_pred, scatter_true)

The two-chart format uses values of Alcohol usage on the X-axis, opacity based on Cannabis usage, and size based on Nicotine usage. The y-value predictions are based on the minimum usage of hard drugs. The usage values for the drugs are classified in this format:

CL0: Never Used CL1: Used over a Decade Ago CL2: Used in Last Decade CL3: Used in Last Year CL4: Used in Last Month CL5: Used in Last Week CL6: Used in Last Day

Since we have classified the usage of hard drugs only on integer values, we can more accurately see how well our regression is performing based on rounding the values

from sklearn.metrics import r2_score
reg_results["Pred"] = np.round(reg.predict(X_test)).astype(int)
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)
print(reg_results)
truths = reg_results["Pred"] == reg_results["HRD"]
score = sum(truths)/len(reg_results)
print(score)
print(r2_score(reg_results["HRD"], reg_results["Pred"]))
alt.hconcat(scatter_pred, scatter_true)
      Alc  Can  Nic  Pred  HRD
571     5    4    0     3    2
1336    5    4    5     4    5
1290    5    6    3     4    3
1432    5    2    6     2    0
945     5    3    4     4    1
...   ...  ...  ...   ...  ...
1577    5    0    0     0    2
911     5    6    5     3    0
1759    4    4    3     3    3
996     5    4    0     3    0
1173    5    3    0     2    6

[566 rows x 5 columns]
0.2791519434628975
0.3155418549463084

From the graphs and the scores, you can interpret that the regression can start to see the trend of increased usage of hard drugs, with the increase of more common drugs, but not to a very exact extent. Let’s try some other learning models to see if we can further increase the accuracy of our predictions.

Found resources for Random Forest Regressions https://www.geeksforgeeks.org/random-forest-regression-in-python/

from sklearn.ensemble import RandomForestRegressor
rf_reg = RandomForestRegressor(n_estimators=100, random_state=0)
rf_reg.fit(X_train, y_train)
print(rf_reg.score(X_train,y_train))
print(rf_reg.score(X_test,y_test))
0.9085629004229634
0.33925950182140385

There seems to be overfitting in the training data, we can try to improve this

rf_reg = RandomForestRegressor(n_estimators=200, max_depth=20, min_samples_split=5, random_state=20)
rf_reg.fit(X_train, y_train)
print(rf_reg.score(X_train,y_train))
print(rf_reg.score(X_test,y_test))
0.8749400662039043
0.33404604622464595
rf_reg = RandomForestRegressor(n_estimators=500, max_depth=30, min_samples_split=10, random_state=400)
rf_reg.fit(X_train, y_train)
print(rf_reg.score(X_train,y_train))
print(rf_reg.score(X_test,y_test))
0.7953448330667504
0.34549849722414994

Adjusting this model also did not help much with the predictions, most likely meaning the relationships are very complex between the personality information and previous drug usage.

reg_results["Pred_rf"] = np.round(rf_reg.predict(X_test)).astype(int)
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred_rf",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)
alt.hconcat(scatter_pred, scatter_true)

Upon reviewing the data, and regressions above, it seems that since the target values are integers 0-6, the predictions could be found more accurately using classification rather than regressions.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(X_train, y_train)
/shared-libs/python3.7/py/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py:818: ConvergenceWarning: lbfgs failed to converge (status=1):
STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.

Increase the number of iterations (max_iter) or scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.html
Please also refer to the documentation for alternative solver options:
    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression
  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG,
LogisticRegression()
reg_results["Pred_lg"] = clf.predict(X_test)
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred_lg",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)


accuracy = (reg_results["Pred_lg"] == reg_results["HRD"]).sum()/len(reg_results["HRD"])
print(accuracy)
alt.hconcat(scatter_pred, scatter_true)
0.4134275618374558

Identifying the classification problem, led to a higher accuracy of 41%.

We can also use Random Forest Classifiers from sklearn

https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html

from sklearn.ensemble import RandomForestClassifier
clf_rf = RandomForestClassifier()
clf_rf.fit(X_train, y_train)
reg_results["Pred_rfc"] = clf_rf.predict(X_test)
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred_rfc",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)


accuracy = (reg_results["Pred_rfc"] == reg_results["HRD"]).sum()/len(reg_results["HRD"])
print(accuracy)
alt.hconcat(scatter_pred, scatter_true)
0.3957597173144876

The random forest classifier roughly has a 40% accuracy.

Due to the complex relationship of all the inputs, we can try neural network models using PyTorch to see if we get better results.

Found resources on creating PyTorch models here:

https://www.geeksforgeeks.org/getting-started-with-pytorch/

This link was used for coding the general class structures of the PyTorch ML model

https://saturncloud.io/blog/how-do-i-convert-a-pandas-dataframe-to-a-pytorch-tensor/

This link was used to make sure I was converting the data frame input and output information in a PyTorch tensor format.

https://pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html

This link was for adding additional neural network layers from “self.linear_relu_stack”

https://saturncloud.io/blog/how-to-test-pytorch-model-accuracy-a-guide-for-data-scientists/

This link was for checking the neural network model after I trained it with a loss function and an r2 score. I ended up replacing this as I understand that a classification loss function will work better with the usage level of hard drugs in the outputs

The link below helped me find the classification loss function

https://neptune.ai/blog/pytorch-loss-functions

https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html

import torch
from torch import nn
from torch.utils.data import DataLoader
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_sc = scaler.fit_transform(X_train)
X_test_sc = scaler.transform(X_test)
X_train_tensor = torch.tensor(X_train_sc, dtype=torch.float32)
X_test_tensor = torch.tensor(X_test_sc, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train.values, dtype=torch.long)
y_test_tensor = torch.tensor(y_test.values, dtype=torch.long)
# print(X_train_tensor)
# print(y_train_tensor)
#create DataLoader
data_loader = DataLoader(list(zip(X_train_tensor, y_train_tensor)), shuffle=True, batch_size=16)
input_sz = X_train_tensor.shape[1]
class Model(torch.nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(input_sz,64),
            nn.ReLU(),
            nn.Linear(64,32),
            nn.ReLU(),
            nn.Linear(32, 7)
        )
    def forward(self, x):
        x = self.flatten(x)
        y_pred = self.linear_relu_stack(x)
        return y_pred

model = Model()
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.AdamW(model.parameters(), lr= 0.01)
for epoch in range(100):
    for batch_idx, (data, target) in enumerate(data_loader):
        optimizer.zero_grad()
        # print(data, target)
        y_pred = model(data)
        # print(y_pred)
        loss = loss_fn(y_pred, target)
        loss.backward()
        optimizer.step()
    print(f'EPoch {epoch} and Loss {loss.item()}')
/shared-libs/python3.7/py/lib/python3.7/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
EPoch 0 and Loss 1.754495620727539
EPoch 1 and Loss 1.189595103263855
EPoch 2 and Loss 1.5417512655258179
EPoch 3 and Loss 2.447200059890747
EPoch 4 and Loss 1.0042314529418945
EPoch 5 and Loss 0.839275598526001
EPoch 6 and Loss 1.2422605752944946
EPoch 7 and Loss 0.9580034613609314
EPoch 8 and Loss 1.6602710485458374
EPoch 9 and Loss 1.1606720685958862
EPoch 10 and Loss 0.7814002633094788
EPoch 11 and Loss 0.8897814154624939
EPoch 12 and Loss 1.1023269891738892
EPoch 13 and Loss 1.2298903465270996
EPoch 14 and Loss 1.183815598487854
EPoch 15 and Loss 0.7505224347114563
EPoch 16 and Loss 1.4345954656600952
EPoch 17 and Loss 1.3358932733535767
EPoch 18 and Loss 1.0706067085266113
EPoch 19 and Loss 0.6281061768531799
EPoch 20 and Loss 0.9219965934753418
EPoch 21 and Loss 0.680879533290863
EPoch 22 and Loss 1.3265366554260254
EPoch 23 and Loss 0.8634664416313171
EPoch 24 and Loss 0.08845993131399155
EPoch 25 and Loss 0.877751886844635
EPoch 26 and Loss 0.6797552704811096
EPoch 27 and Loss 0.6267693042755127
EPoch 28 and Loss 0.32851865887641907
EPoch 29 and Loss 1.5337227582931519
EPoch 30 and Loss 0.15531256794929504
EPoch 31 and Loss 0.977714478969574
EPoch 32 and Loss 0.517052412033081
EPoch 33 and Loss 0.6993114352226257
EPoch 34 and Loss 0.4972832202911377
EPoch 35 and Loss 0.5908482670783997
EPoch 36 and Loss 0.5536584258079529
EPoch 37 and Loss 0.38264989852905273
EPoch 38 and Loss 0.30183252692222595
EPoch 39 and Loss 0.5068582892417908
EPoch 40 and Loss 0.6397512555122375
EPoch 41 and Loss 0.9711241722106934
EPoch 42 and Loss 0.5777873992919922
EPoch 43 and Loss 0.43225720524787903
EPoch 44 and Loss 0.6011208891868591
EPoch 45 and Loss 0.7212926745414734
EPoch 46 and Loss 0.5570889115333557
EPoch 47 and Loss 0.43298837542533875
EPoch 48 and Loss 0.0535506010055542
EPoch 49 and Loss 0.02506282366812229
EPoch 50 and Loss 0.9734498858451843
EPoch 51 and Loss 0.3817117214202881
EPoch 52 and Loss 0.0797887071967125
EPoch 53 and Loss 0.5173400044441223
EPoch 54 and Loss 0.3881460726261139
EPoch 55 and Loss 1.3919142484664917
EPoch 56 and Loss 0.37752678990364075
EPoch 57 and Loss 0.5902026891708374
EPoch 58 and Loss 0.18848882615566254
EPoch 59 and Loss 0.13294662535190582
EPoch 60 and Loss 0.9426941871643066
EPoch 61 and Loss 0.3457077443599701
EPoch 62 and Loss 0.11789185553789139
EPoch 63 and Loss 0.2122751623392105
EPoch 64 and Loss 0.04157606512308121
EPoch 65 and Loss 0.03768516704440117
EPoch 66 and Loss 0.014479073695838451
EPoch 67 and Loss 0.030924534425139427
EPoch 68 and Loss 0.7548474669456482
EPoch 69 and Loss 0.33424898982048035
EPoch 70 and Loss 0.03141066059470177
EPoch 71 and Loss 0.5781236886978149
EPoch 72 and Loss 0.2453569620847702
EPoch 73 and Loss 0.49851179122924805
EPoch 74 and Loss 0.49229922890663147
EPoch 75 and Loss 0.33484211564064026
EPoch 76 and Loss 0.11252496391534805
EPoch 77 and Loss 0.45720672607421875
EPoch 78 and Loss 0.045026298612356186
EPoch 79 and Loss 0.06932039558887482
EPoch 80 and Loss 0.12531429529190063
EPoch 81 and Loss 0.06639150530099869
EPoch 82 and Loss 0.853560209274292
EPoch 83 and Loss 0.559391438961029
EPoch 84 and Loss 0.7026881575584412
EPoch 85 and Loss 0.5633488297462463
EPoch 86 and Loss 0.18491607904434204
EPoch 87 and Loss 0.3345070779323578
EPoch 88 and Loss 0.2845603823661804
EPoch 89 and Loss 0.2835375964641571
EPoch 90 and Loss 0.07899539172649384
EPoch 91 and Loss 0.19927750527858734
EPoch 92 and Loss 0.09276064485311508
EPoch 93 and Loss 0.591977596282959
EPoch 94 and Loss 0.15167343616485596
EPoch 95 and Loss 0.5202950835227966
EPoch 96 and Loss 0.5427935719490051
EPoch 97 and Loss 0.5358689427375793
EPoch 98 and Loss 0.1735238879919052
EPoch 99 and Loss 0.5003050565719604

I also learned that I needed to do batch training to improve my results, and learned to use that from this link:

https://saturncloud.io/blog/how-batch-learning-in-pytorch-is-performed/

with torch.no_grad():
    test_pred = model(X_test_tensor)
    lost = loss_fn(test_pred, y_test_tensor)
    print(f' Lost {lost.item()}')
print(test_pred.numpy())
true_labels = np.argmax(test_pred.numpy(), axis=1)
print(true_labels)
 Lost 6.099795818328857
[[  0.2801118   4.451331   -5.673775  ... -15.762879    5.5148745
   -6.406772 ]
 [  3.1637866 -24.817417  -10.66201   ...  -3.3901188 -12.169625
    1.7688649]
 [-11.854221  -16.559755    2.022569  ... -12.569478   -4.0322824
   -7.82025  ]
 ...
 [ -4.515668  -12.162217    1.9123155 ...   2.4799595   2.2739716
   -5.16499  ]
 [  7.5589666 -33.745354  -13.040584  ...   1.2093605 -20.108915
   -5.0905213]
 [ -0.7626572 -24.218164  -10.509264  ...   5.085247  -18.93976
    1.6655252]]
[5 0 2 2 3 4 4 0 5 2 5 2 4 0 0 2 0 5 3 4 0 0 0 5 2 6 0 2 4 4 1 4 0 3 0 0 2
 0 0 3 6 1 2 5 3 3 6 4 1 0 3 0 6 6 1 0 0 1 6 1 2 0 3 1 2 4 6 0 5 6 3 0 0 3
 0 0 1 0 0 0 2 6 0 3 0 0 0 3 3 3 0 5 2 2 3 4 2 5 5 3 3 0 4 0 4 0 6 2 1 0 5
 3 6 2 5 6 4 1 5 0 6 1 0 0 5 4 6 1 4 1 2 3 6 0 6 1 2 0 2 0 1 5 0 2 0 1 0 0
 5 5 4 0 3 0 2 3 0 0 0 0 4 6 6 0 4 0 2 1 6 1 3 4 3 0 0 1 0 0 4 0 3 0 3 4 3
 0 0 3 4 4 4 1 1 0 0 0 6 0 0 6 0 5 3 0 3 2 3 5 3 0 6 2 0 6 6 0 3 6 0 0 0 6
 5 2 3 0 4 6 0 3 2 5 0 0 1 0 5 3 4 0 1 0 0 1 6 0 1 0 0 0 3 6 0 3 4 0 0 0 3
 4 0 0 4 1 0 0 0 5 6 0 3 1 0 6 5 0 2 0 5 5 1 3 4 0 3 0 0 2 4 0 0 6 1 2 1 1
 0 1 0 6 3 3 2 2 2 2 5 3 2 6 0 6 0 3 3 2 6 2 2 0 6 5 3 0 4 0 2 0 4 0 1 6 0
 0 0 2 3 1 5 1 1 6 0 0 6 2 1 1 0 0 0 3 4 6 6 0 0 0 3 0 1 6 3 4 0 6 0 0 5 0
 1 0 6 5 2 0 0 3 6 1 2 6 1 3 4 0 6 6 6 0 0 1 2 6 0 3 3 0 3 0 5 3 1 3 4 0 3
 2 6 3 2 1 0 0 5 6 3 0 4 2 0 6 1 4 4 0 0 0 0 5 0 3 3 0 1 6 3 0 6 0 4 0 6 0
 6 5 3 0 0 3 4 5 2 1 2 2 1 6 0 0 3 6 0 3 0 3 1 0 3 3 0 4 3 6 0 3 0 6 0 6 5
 6 0 0 0 0 0 4 0 1 0 1 5 0 6 2 2 1 0 3 3 2 0 1 3 5 0 6 2 0 0 1 6 0 4 6 0 1
 3 3 0 0 5 0 0 0 0 2 5 4 0 0 6 3 0 2 6 3 6 3 0 0 1 0 3 1 3 6 0 1 0 6 4 1 6
 0 0 0 2 0 0 0 5 4 0 4]

reg_results["Pred_nn"] = true_labels
accuracy = (reg_results["Pred_nn"] == reg_results["HRD"]).sum()/len(reg_results["HRD"])
print(accuracy)
scatter_pred = alt.Chart(reg_results).mark_circle().encode(
    x="Alc",
    y="Pred_nn",
    opacity="Can:Q",
    color=alt.value("green"),
    size="Nic:Q"
)
alt.hconcat(scatter_pred, scatter_true)
0.33568904593639576

From the neural network model above, it seems that using a classification loss function offers about 33% accuracy. Though this seems low, the scatter plot above shows that even when the predicted value isn’t precisely correct, the general trends of growth based on AMN (Alcohol, Marijuana, and Nicotine) from the scatter plot offer an accurate representation of their relationship with hard drug use. The linear and random forest regression seems to also start picking up on learning the patterns, and while the values aren’t exactly right, it does predict and recognize the pattern that the increased usage of alcohol, cannabis, or nicotine starts to increase the usage of more serious hard drugs.

print(reg_results)
model_names = ["Pred", "Pred_rf", "Pred_lg", "Pred_nn", "Pred_rfc"]
accuracies = []
for mdl in model_names:
    acc = (reg_results[mdl] == reg_results["HRD"]).sum()/len(reg_results["HRD"])
    print(acc)
    accuracies.append(acc)
df_acc = pd.DataFrame({"Model": model_names, "ACC": accuracies})
alt.Chart(df_acc).mark_bar().encode(
    x="Model",
    y="ACC"
)
      Alc  Can  Nic  Pred  HRD  Pred_rf  Pred_lg  Pred_rfc  Pred_nn
571     5    4    0     3    2        2        0         0        5
1336    5    4    5     4    5        4        3         4        0
1290    5    6    3     4    3        3        6         6        2
1432    5    2    6     2    0        2        1         1        2
945     5    3    4     4    1        4        6         6        3
...   ...  ...  ...   ...  ...      ...      ...       ...      ...
1577    5    0    0     0    2        1        0         0        0
911     5    6    5     3    0        2        3         5        5
1759    4    4    3     3    3        3        3         3        4
996     5    4    0     3    0        3        0         0        0
1173    5    3    0     2    6        2        0         0        4

[566 rows x 9 columns]
0.2791519434628975
0.26148409893992935
0.4134275618374558
0.33568904593639576
0.3957597173144876
accuracy_broader = []
for mdl in model_names:
    acc = (abs(reg_results[mdl] - reg_results["HRD"]) <= 1).sum()/len(reg_results["HRD"])
    print(acc)
    accuracy_broader.append(acc)
df_acc["ACC_brd"] = accuracy_broader
alt.Chart(df_acc).mark_bar().encode(
    x="Model",
    y="ACC_brd"
)
0.6378091872791519
0.6395759717314488
0.598939929328622
0.5547703180212014
0.5812720848056537

From the data, the logistic regression ended up offering the highest accuracy in predicting hard drug usage. However, the linear regression and random forest regression seem to be closer to the hard drug by 1 unit using the broader accuracy calculation.

When it comes to specific predictions, the classification models tend to predict better, and the neural network model can most likely use adjustments to its parameters to increase its accuracy.

This data can be continued for use with ML models, but maybe with different strategies or targetting specific drugs as targets, or using different ML models to see if any more information or predictions can be found. The models created above could possibly be applied to new user data to predict more patterns of using hard drugs based on their drug usage history and personality aspects.

Summary#

Either summarize what you did, or summarize the results. Maybe 3 sentences.

I found drug usage data on 1885 respondents, which also offered information on people’s personality measurements, and cleaned it so that we can use the user information to predict the level of usage of hard drugs. I created many ML models and compared them using regression and classification models, including a PyTorch classification neural network model. I concluded that since my targets were separated into classes that represented the extent of use of hard drugs, the classification models offered more accurate predictions and that the neural network model could be improved by trying new adjustments to its parameters.

#

References#

Your code above should include references. Here is some additional space for references.

  • What is the source of your dataset(s)?

https://www.kaggle.com/datasets/obeykhadija/drug-consumptions-uci

  • List any other references that you found helpful.

All references listed above in code

Created in deepnote.com Created in Deepnote