Get started using Visual Python by clicking the 🟧 orange button 🟧 on the right side!
# Visual Python: Data Analysis > Import
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
# Visual Python: Data Analysis > File
data = pd.read_csv('https://raw.githubusercontent.com/visualpython/visualpython/main/visualpython/data/sample_csv/iris.csv')
data
# Visual Python: Data Analysis > Subset
data.loc[(data['variety'] == 'Setosa')&(data['sepal.length'] < 5), ['variety','sepal.length','petal.length']]
# Visual Python: Visualization > Seaborn
sns.lineplot(data=data)
plt.show()
# Visual Python: Machine Learning > Data Split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(data[['sepal.length', 'sepal.width', 'petal.length', 'petal.width']], data['variety'])
# Visual Python: Machine Learning > Classifier
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
# Visual Python: Machine Learning > Fit/Predict
model.fit(X_train, y_train)
RandomForestClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
RandomForestClassifier()
# Visual Python: Machine Learning > Fit/Predict
pred = model.predict(X_test)
pred
array(['Setosa', 'Versicolor', 'Versicolor', 'Setosa', 'Versicolor', 'Virginica', 'Virginica', 'Setosa', 'Virginica', 'Virginica', 'Virginica', 'Virginica', 'Versicolor', 'Versicolor', 'Setosa', 'Setosa', 'Virginica', 'Virginica', 'Setosa', 'Versicolor', 'Virginica', 'Setosa', 'Virginica', 'Setosa', 'Virginica', 'Setosa', 'Setosa', 'Versicolor', 'Setosa', 'Setosa', 'Setosa', 'Setosa', 'Virginica', 'Versicolor', 'Virginica', 'Versicolor', 'Versicolor', 'Versicolor'], dtype=object)
# Visual Python: Machine Learning > Evaluation
from sklearn import metrics
# Visual Python: Machine Learning > Evaluation
from sklearn import metrics
# Visual Python: Machine Learning > Evaluation
# Confusion Matrix
pd.crosstab(y_test, pred, margins=True)
col_0 | Setosa | Versicolor | Virginica | All |
---|---|---|---|---|
variety | ||||
Setosa | 14 | 0 | 0 | 14 |
Versicolor | 0 | 10 | 2 | 12 |
Virginica | 0 | 1 | 11 | 12 |
All | 14 | 11 | 13 | 38 |
# Visual Python: Machine Learning > Evaluation
# Classification report
print(metrics.classification_report(y_test, pred))
precision recall f1-score support Setosa 1.00 1.00 1.00 14 Versicolor 0.91 0.83 0.87 12 Virginica 0.85 0.92 0.88 12 accuracy 0.92 38 macro avg 0.92 0.92 0.92 38 weighted avg 0.92 0.92 0.92 38