-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain_M3GP_classification_example.py
More file actions
45 lines (30 loc) · 1.08 KB
/
Main_M3GP_classification_example.py
File metadata and controls
45 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pandas
from m3gp.M3GP import M3GP
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore", category=FutureWarning,
message="From version 0.21, test_size will always complement",
module="sklearn")
#
# By using this file, you are agreeing to this product's EULA
#
# This product can be obtained in https://github.com/jespb/Python-M3GP
#
# Copyright ©2019-2025 J. E. Batista
#
filename= "heart.csv"
# Open the dataset
ds = pandas.read_csv("datasets/"+filename)
class_header = ds.columns[-1]
# Split the dataset
Tr_X, Te_X, Tr_Y, Te_Y = train_test_split(ds.drop(columns=[class_header]), ds[class_header],
train_size=0.7, random_state = 42, stratify = ds[class_header])
# Train a model
m3gp = M3GP(model_class=RandomForestClassifier(max_depth=6), fitnessType="2FOLD")
m3gp.fit(Tr_X, Tr_Y)
# Predict test results
pred = m3gp.predict(Te_X)
# Obtain test accuracy
print( accuracy_score(pred, Te_Y) )