forked from machinatoonist/prompt-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-xgboost-classifier-2.py
More file actions
40 lines (29 loc) · 1.12 KB
/
example-xgboost-classifier-2.py
File metadata and controls
40 lines (29 loc) · 1.12 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
# Prompt: write a python script to build a multi-class
# classification machine learning model using the iris sample dataset from sklearn and xgboost
# Temperature: 0.9
# Notes:
# Initial code suggestion failed because df['species'] was used to fit model and
# required numerical values. This was fixed by using dataset.target instead.
# import necessary libraries
import pandas as pd
from sklearn import datasets
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
# load the iris dataset
dataset = datasets.load_iris()
# build a dataframe from the dataset
df = pd.DataFrame(dataset.data, columns=dataset.feature_names)
# add the target variable to the dataframe
df['species'] = pd.Categorical.from_codes(dataset.target, dataset.target_names)
# create a list of feature columns
features = df.columns.drop('species')
# create the xgboost classifier
xgb = XGBClassifier()
df['species']
df[features]
# train the classifier
xgb.fit(df[features], dataset.target)
# make predictions
predictions = xgb.predict(df[features])
# print the accuracy
print(f'Accuracy: {accuracy_score(dataset.target, predictions)*100:.1f}%')