Ml_test


Reading time: about 5 minutes
In [25]:
import matplotlib.pyplot as plt
import numpy as np
import torch
import pandas as pd
import tqdm
import sklearn.utils
import sklearn.datasets
from typing import cast
import math
import scipy.optimize
In [26]:
sklearn_iris = cast(sklearn.utils.Bunch, sklearn.datasets.load_iris())

iris = pd.DataFrame()

iris["sepal_length"] = sklearn_iris.data[:, 0]
iris["sepal_width"] = sklearn_iris.data[:, 1]
iris["petal_length"] = sklearn_iris.data[:, 2]
iris["petal_width"] = sklearn_iris.data[:, 3]

iris["is_setosa"] = (sklearn_iris.target == 0).astype(np.float32)
iris["is_versicolor"] = (sklearn_iris.target == 1).astype(np.float32)
iris["is_virginica"] = (sklearn_iris.target == 2).astype(np.float32)

iris
Out [26]:
sepal_length sepal_width petal_length petal_width is_setosa is_versicolor is_virginica
0 5.1 3.5 1.4 0.2 1.0 0.0 0.0
1 4.9 3.0 1.4 0.2 1.0 0.0 0.0
2 4.7 3.2 1.3 0.2 1.0 0.0 0.0
3 4.6 3.1 1.5 0.2 1.0 0.0 0.0
4 5.0 3.6 1.4 0.2 1.0 0.0 0.0
... ... ... ... ... ... ... ...
145 6.7 3.0 5.2 2.3 0.0 0.0 1.0
146 6.3 2.5 5.0 1.9 0.0 0.0 1.0
147 6.5 3.0 5.2 2.0 0.0 0.0 1.0
148 6.2 3.4 5.4 2.3 0.0 0.0 1.0
149 5.9 3.0 5.1 1.8 0.0 0.0 1.0

150 rows × 7 columns

In [27]:
FEATURES = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
TARGETS = ["is_setosa", "is_versicolor", "is_virginica"]
In [28]:
# Let's scale the data
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

iris[FEATURES] = scaler.fit_transform(iris[FEATURES])

iris
Out [28]:
sepal_length sepal_width petal_length petal_width is_setosa is_versicolor is_virginica
0 -0.900681 1.019004 -1.340227 -1.315444 1.0 0.0 0.0
1 -1.143017 -0.131979 -1.340227 -1.315444 1.0 0.0 0.0
2 -1.385353 0.328414 -1.397064 -1.315444 1.0 0.0 0.0
3 -1.506521 0.098217 -1.283389 -1.315444 1.0 0.0 0.0
4 -1.021849 1.249201 -1.340227 -1.315444 1.0 0.0 0.0
... ... ... ... ... ... ... ...
145 1.038005 -0.131979 0.819596 1.448832 0.0 0.0 1.0
146 0.553333 -1.282963 0.705921 0.922303 0.0 0.0 1.0
147 0.795669 -0.131979 0.819596 1.053935 0.0 0.0 1.0
148 0.432165 0.788808 0.933271 1.448832 0.0 0.0 1.0
149 0.068662 -0.131979 0.762758 0.790671 0.0 0.0 1.0

150 rows × 7 columns

In [29]:
# Drop class
iris_orig = iris.copy()

iris = iris.drop(TARGETS, axis=1)

iris
Out [29]:
sepal_length sepal_width petal_length petal_width
0 -0.900681 1.019004 -1.340227 -1.315444
1 -1.143017 -0.131979 -1.340227 -1.315444
2 -1.385353 0.328414 -1.397064 -1.315444
3 -1.506521 0.098217 -1.283389 -1.315444
4 -1.021849 1.249201 -1.340227 -1.315444
... ... ... ... ...
145 1.038005 -0.131979 0.819596 1.448832
146 0.553333 -1.282963 0.705921 0.922303
147 0.795669 -0.131979 0.819596 1.053935
148 0.432165 0.788808 0.933271 1.448832
149 0.068662 -0.131979 0.762758 0.790671

150 rows × 4 columns

The following pages link here

Citation

If you find this work useful, please cite it as:
@article{yaltirakliwikimltest,
  title   = "Ml_test",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/wiki/ml_test/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Ml_test", November, 2024. [Online]. Available: https://www.gkbrk.com/wiki/ml_test/. [Accessed Nov. 12, 2024].
APA Style
Yaltirakli, G. (2024, November 12). Ml_test. https://www.gkbrk.com/wiki/ml_test/
Bluebook Style
Gokberk Yaltirakli, Ml_test, GKBRK.COM (Nov. 12, 2024), https://www.gkbrk.com/wiki/ml_test/

Comments

© 2024 Gokberk Yaltirakli