• For any query, contact us at
  • +91-9872993883
  • +91-8283824812
  • info@ris-ai.com

Classify Marijuana is Legal or Not

We firstly import necessary library for this project

In [7]:
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

Now we read CSV file in which data for legal and illegal marijuana states

In [8]:
dataset = pd.read_csv('state_marijuana_legalization_dataset.csv')
X = dataset.iloc[:, :5]
y = dataset.iloc[:, -1]
print(X)
    bachelors_degrees  coastline_length  population_density  forest_cover  \
0                37.9                31                 618          37.9   
1                30.8               750                 222          17.0   
2                28.0              6640                   1          30.4   
3                36.8               130                1218          39.5   
4                37.6                96                 741          54.7   
5                40.5               192                 871          52.5   
6                34.9                13                 148          77.5   
7                36.3               112                 212          60.7   
8                31.4               840                 251          17.8   
9                32.9               157                 107          40.7   
10               38.1                 0                  52          17.5   
11               33.7                 0                  68          28.9   
12               31.1                 0                  36           8.9   
13               30.0                28                 485          30.0   
14               34.2               127                 420          50.9   
15               27.7                 0                  10           1.0   
16               25.7                 0                   6           9.2   
17               32.3                 0                 231          11.5   
18               31.9                40                1021          50.8   
19               36.0                 0                  67          75.7   
20               28.6                 0                 286          55.3   
21               27.6               367                 105           7.0   
22               27.8                 0                 106          45.2   
23               29.3                 0                  24           1.8   
24               26.7                 0                  55           5.4   
25               30.8               296                  41          38.8   
26               31.0                 0                  36           2.8   
27               27.0                 0                  11           3.1   
28               23.0                 0                  26           0.5   
29               29.0               228                  43          89.0   
30               27.5                 0                  60           4.8   
31               28.8               100                 177          64.2   
32               26.9                 0                 175          51.2   
33               26.1                 0                 284          28.9   
34               24.1                 0                 184          18.9   
35               27.1                 0                  88          30.3   
36               29.5                 0                   7          20.6   
37               27.3              1350                 378          42.4   
38               24.1                 0                  57          14.2   
39               25.9                 0                  20          31.8   
40               28.4               301                 206          59.9   
41               24.9                 0                 160          52.9   
42               25.8               187                 162          63.8   
43               22.5               397                 108          49.2   
44               26.3                 0                  17           5.6   
45               22.3                 0                 112          48.6   
46               23.5                53                  95          70.6   
47               19.2                 0                  76          77.2   
48               21.1                 0                  57          55.1   
49               20.7                44                  63          61.9   

    per_capita_income  
0               36338  
1               29736  
2               33062  
3               37288  
4               39373  
5               36593  
6               34691  
7               34052  
8               30441  
9               31841  
10              32357  
11              32638  
12              24877  
13              30488  
14              33095  
15              33071  
16              29698  
17              30417  
18              30830  
19              29178  
20              29220  
21              27125  
22              28213  
23              27446  
24              28361  
25              27646  
26              27870  
27              26959  
28              25773  
29              27978  
30              25715  
31              25615  
32              26613  
33              26937  
34              25140  
35              26126  
36              25989  
37              26582  
38              25229  
39              23938  
40              25774  
41              24922  
42              24596  
43              24800  
44              23683  
45              23684  
46              23606  
47              22714  
48              22883  
49              21036  

Using train set method by RandomForest model for classification

In [9]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
model = RandomForestClassifier()
model.fit(X_train, y_train)
/home/webtunix/.local/lib/python3.5/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)
Out[9]:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
                       max_depth=None, max_features='auto', max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=10,
                       n_jobs=None, oob_score=False, random_state=None,
                       verbose=0, warm_start=False)

Take input value from user

In [11]:
print("ENTER THE VALUES TO CLASSIFY MARIJUANA IS LEGAL OR NOT: ")

a=int(input("bachelors_degrees: "))
b=int(input("coastline_length: "))
c=int(input("population_density: "))
d=int(input("forest_cover: "))
e=int(input("per_capita_income: "))
ENTER THE VALUES TO CLASSIFY MARIJUANA IS LEGAL OR NOT: 
bachelors_degrees: 37
coastline_length: 31
population_density: 618
forest_cover: 37
per_capita_income: 36338
In [12]:
y=[]
y.append(a)
y.append(b)
y.append(c)
y.append(d)
y.append(e)
In [13]:
test = []
test.append(y)

Make empty list and append data in it.

We make another list in which we append all the input list

We predict the model by passing the list.

In [14]:
result = model.predict(test)

Now, we check the result. If the result is one then that state is legal for marijuana and if the result is zero then that state is ill-legal for marijuana.

In [16]:
if result==[1]:
    print("marijuana is leagal")
else:
    print("marijuana is ill-leagal")
marijuana is leagal

We use RandomForestClassifier model because it's depend upon probability as it occur in between 0 and 1

In [ ]: