import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle

# for random functions
seed_number = 123

# load the dataset to pandas
# method 1 - import from sklearn
from sklearn import datasets
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)


test_proportion = 0.3
X_train, X_test, y_train, y_test = train_test_split(
    df[iris.feature_names],
    iris.target,
    test_size=test_proportion,
    stratify=iris.target,
    random_state=seed_number,
)

# define the model
n_trees = 100
rf = RandomForestClassifier(
    n_estimators=n_trees, oob_score=True, random_state=seed_number
)

# train the model
rf.fit(X_train, y_train)

# score our model and print the output
predicted = rf.predict(X_test)
accuracy = accuracy_score(y_test, predicted)
print(
    "Out-of-bag score estimate: {0:.3f}\n"
    "Mean accuracy score: {1:.3f}".format(rf.oob_score_, accuracy
    )
)

# to save (pickle) an object
with open('../models/irisdatamodel.pkl', 'wb') as file:
    pickle.dump(rf, file)
    
# to load (unpickle) an object
with open('../models/irisdatamodel.pkl', 'rb') as file:
    new_object_name = pickle.load(file)
14c6622fe0cb011a7de12cf8d01da500a7b33ec0
<mask token>

<mask token>
rf.fit(X_train, y_train)
<mask token>
print("""Out-of-bag score estimate: {0:.3f}
Mean accuracy score: {1:.3f}"""
    .format(rf.oob_score_, accuracy))
with open('../models/irisdatamodel.pkl', 'wb') as file:
    pickle.dump(rf, file)
with open('../models/irisdatamodel.pkl', 'rb') as file:
    new_object_name = pickle.load(file)

<mask token>
seed_number = 123
<mask token>
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
test_proportion = 0.3
X_train, X_test, y_train, y_test = train_test_split(df[iris.feature_names],
    iris.target, test_size=test_proportion, stratify=iris.target,
    random_state=seed_number)
n_trees = 100
rf = RandomForestClassifier(n_estimators=n_trees, oob_score=True,
    random_state=seed_number)
rf.fit(X_train, y_train)
predicted = rf.predict(X_test)
accuracy = accuracy_score(y_test, predicted)
print("""Out-of-bag score estimate: {0:.3f}
Mean accuracy score: {1:.3f}"""
    .format(rf.oob_score_, accuracy))
with open('../models/irisdatamodel.pkl', 'wb') as file:
    pickle.dump(rf, file)
with open('../models/irisdatamodel.pkl', 'rb') as file:
    new_object_name = pickle.load(file)

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle
seed_number = 123
from sklearn import datasets
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
test_proportion = 0.3
X_train, X_test, y_train, y_test = train_test_split(df[iris.feature_names],
    iris.target, test_size=test_proportion, stratify=iris.target,
    random_state=seed_number)
n_trees = 100
rf = RandomForestClassifier(n_estimators=n_trees, oob_score=True,
    random_state=seed_number)
rf.fit(X_train, y_train)
predicted = rf.predict(X_test)
accuracy = accuracy_score(y_test, predicted)
print("""Out-of-bag score estimate: {0:.3f}
Mean accuracy score: {1:.3f}"""
    .format(rf.oob_score_, accuracy))
with open('../models/irisdatamodel.pkl', 'wb') as file:
    pickle.dump(rf, file)
with open('../models/irisdatamodel.pkl', 'rb') as file:
    new_object_name = pickle.load(file)

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle

# for random functions
seed_number = 123

# load the dataset to pandas
# method 1 - import from sklearn
from sklearn import datasets
iris = datasets.load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)


test_proportion = 0.3
X_train, X_test, y_train, y_test = train_test_split(
    df[iris.feature_names],
    iris.target,
    test_size=test_proportion,
    stratify=iris.target,
    random_state=seed_number,
)

# define the model
n_trees = 100
rf = RandomForestClassifier(
    n_estimators=n_trees, oob_score=True, random_state=seed_number
)

# train the model
rf.fit(X_train, y_train)

# score our model and print the output
predicted = rf.predict(X_test)
accuracy = accuracy_score(y_test, predicted)
print(
    "Out-of-bag score estimate: {0:.3f}\n"
    "Mean accuracy score: {1:.3f}".format(rf.oob_score_, accuracy
    )
)

# to save (pickle) an object
with open('../models/irisdatamodel.pkl', 'wb') as file:
    pickle.dump(rf, file)
    
# to load (unpickle) an object
with open('../models/irisdatamodel.pkl', 'rb') as file:
    new_object_name = pickle.load(file)