Decision Tree Classifier for the IRIS dataset¶
import numpy as np # for number crunching
import pandas as pd # for data handling
import matplotlib.pyplot as plt # for plotting
from sklearn import datasets # collection of datasets including the iris dataset
#for creating and plotting a picture of the decision tree
from sklearn import tree
import pydotplus
from IPython.display import Image
# for performance evaluation
from sklearn.metrics import accuracy_score
iris = datasets.load_iris()
from sklearn.tree import DecisionTreeClassifier
To train a Decision Tree Classifier model on a specific data, we need to split the data that we have into a training set and a test set. First we need to import the function that will do that and split the dataset randomly.
from sklearn.model_selection import train_test_split
First we will use some conventional variables that hold our data
X = iris.data # it is convention to use a big X for the data
#(as the data is usally a matrix or multi-dimensional array )
y = iris.target # it is convention to use a small y for the tartets
#(as the targets are usually a vector one-dimensional array)
X
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4],
[5.1, 3.5, 1.4, 0.3],
[5.7, 3.8, 1.7, 0.3],
[5.1, 3.8, 1.5, 0.3],
[5.4, 3.4, 1.7, 0.2],
[5.1, 3.7, 1.5, 0.4],
[4.6, 3.6, 1. , 0.2],
[5.1, 3.3, 1.7, 0.5],
[4.8, 3.4, 1.9, 0.2],
[5. , 3. , 1.6, 0.2],
[5. , 3.4, 1.6, 0.4],
[5.2, 3.5, 1.5, 0.2],
[5.2, 3.4, 1.4, 0.2],
[4.7, 3.2, 1.6, 0.2],
[4.8, 3.1, 1.6, 0.2],
[5.4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, 1.9],
[7.9, 3.8, 6.4, 2. ],
[6.4, 2.8, 5.6, 2.2],
[6.3, 2.8, 5.1, 1.5],
[6.1, 2.6, 5.6, 1.4],
[7.7, 3. , 6.1, 2.3],
[6.3, 3.4, 5.6, 2.4],
[6.4, 3.1, 5.5, 1.8],
[6. , 3. , 4.8, 1.8],
[6.9, 3.1, 5.4, 2.1],
[6.7, 3.1, 5.6, 2.4],
[6.9, 3.1, 5.1, 2.3],
[5.8, 2.7, 5.1, 1.9],
[6.8, 3.2, 5.9, 2.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]])
y
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
Now we split the data into a training set and a test set. Each of those contain a portion of the X data and the respective portion of the y data.
Here we chose the test set to be 20% (0.2) of the data that we have.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
Let's have a look at the training and test data
X_train
array([[6.3, 2.5, 4.9, 1.5],
[6.7, 2.5, 5.8, 1.8],
[6.2, 3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.7, 2.6, 3.5, 1. ],
[5.4, 3.7, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[6.1, 3. , 4.9, 1.8],
[4.3, 3. , 1.1, 0.1],
[5. , 3.3, 1.4, 0.2],
[6.1, 3. , 4.6, 1.4],
[6.3, 2.3, 4.4, 1.3],
[4.9, 2.4, 3.3, 1. ],
[5.4, 3.9, 1.3, 0.4],
[7.7, 2.8, 6.7, 2. ],
[6.1, 2.8, 4. , 1.3],
[7. , 3.2, 4.7, 1.4],
[5. , 3.4, 1.5, 0.2],
[7.7, 3. , 6.1, 2.3],
[6. , 3. , 4.8, 1.8],
[4.8, 3. , 1.4, 0.3]])
y_test
array([1, 1, 2, 2, 1, 2, 2, 1, 0, 0, 2, 0, 0, 1, 0, 0, 2, 0, 0, 1, 1, 1,
0, 2, 1, 1, 0, 2, 2, 0])
To be able to plot the data nicely we can use the dataframe.
df_X_train = pd.DataFrame(X_train, columns = iris.feature_names) # A dataframe that contains the training data, X
colormap = np.array(['Red', 'Blue', 'Green'])
visual_X_train = pd.plotting.scatter_matrix(df_X_train, figsize = [10,10], c = colormap[y_train], s = 150)
df_X_test = pd.DataFrame(X_test, columns = iris.feature_names) # A dataframe that contains the test data
visual_X_test = pd.plotting.scatter_matrix(df_X_test, figsize = [10,10], c = colormap[y_test], s = 150)
From the two plots above you can see how your training and test data looks like.
You would want to investigate that they both are good representatives of their respective classes.
And you would like to see that the particularities for the dataset are represented in both, the training set and the test set.
In this case, we already know that the versicolor (blue) and virginica (green) are overlapping somewhat. We want to see this also in the training set and in the test set.
Since the train test split includes some randomness, everytime you split the data it will be a little bit different.
y_test
array([1, 1, 2, 2, 1, 2, 2, 1, 0, 0, 2, 0, 0, 1, 0, 0, 2, 0, 0, 1, 1, 1,
0, 2, 1, 1, 0, 2, 2, 0])
X_2_train, X_2_test, y_2_train, y_2_test = train_test_split(X, y, test_size = 0.2)
y_2_test
array([1, 2, 1, 1, 1, 2, 0, 1, 0, 1, 2, 2, 0, 1, 2, 0, 2, 2, 2, 0, 1, 2,
2, 1, 1, 2, 1, 1, 0, 2])
If you want to make sure that your train-test-split is the exact same as the one somebody else uses, you need to seed the random number generator.
X_42_train, X_42_test, y_42_train, y_42_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
y_42_test
array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0, 2,
0, 2, 2, 2, 2, 2, 0, 0])
X_23_train, X_23_test, y_23_train, y_23_test = train_test_split(X, y, test_size = 0.2, random_state = 23)
y_23_test
array([2, 2, 1, 0, 2, 1, 0, 2, 0, 1, 1, 0, 2, 0, 0, 2, 1, 1, 2, 0, 2, 0,
0, 0, 2, 0, 0, 2, 1, 1])
Let's build a Decision Tree Classifier¶
model_1 = DecisionTreeClassifier()
Now we train the model, which also is called fitting the model to the data.
model_1.fit(X_train,y_train)
DecisionTreeClassifier()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
DecisionTreeClassifier()
It follows some code to print out the tree. First we have to convert the tree structure into a dot_data wich then is transformed into a graph, which we then can print.
dot_data_1 = tree.export_graphviz(model_1, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_1 = pydotplus.graph_from_dot_data(dot_data_1)
Image(graph_1.create_png())
A decision tree is a very transparent classifier, meaning it is easy for us to understand which rules for classification the tree is using.
How deep it the tree?
Are all samples from the training dataset classified correctly?
Is there anything in the tree that strikes us as particularly unnessessary?
Let's compare the tree with a similar tree but build from a different train-test split
model_2 = DecisionTreeClassifier()
model_2.fit(X_2_train,y_2_train)
dot_data_2 = tree.export_graphviz(model_2, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_2 = pydotplus.graph_from_dot_data(dot_data_2)
Image(graph_2.create_png())
model_42 = DecisionTreeClassifier()
model_42.fit(X_42_train,y_42_train)
dot_data_42 = tree.export_graphviz(model_42, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_42 = pydotplus.graph_from_dot_data(dot_data_42)
Image(graph_42.create_png())
model_23 = DecisionTreeClassifier()
model_23.fit(X_23_train,y_23_train)
dot_data_23 = tree.export_graphviz(model_23, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_23 = pydotplus.graph_from_dot_data(dot_data_23)
Image(graph_23.create_png())
Performance Analysis of our trees¶
How will does the model work on the training data?
predictions_train = model_1.predict(X_train) # predicitons on the training set. This should be good, since the model has trained on this data.
accuracy_score(y_train, predictions_train)
1.0
predictions_2_train = model_2.predict(X_2_train) # predicitons on the training set. This should be good, since the model has trained on this data.
accuracy_score(y_2_train, predictions_2_train)
1.0
predictions_42_train = model_42.predict(X_42_train) # predicitons on the training set. This should be good, since the model has trained on this data.
accuracy_score(y_42_train, predictions_42_train)
1.0
predictions_23_train = model_23.predict(X_23_train) # predicitons on the training set. This should be good, since the model has trained on this data.
accuracy_score(y_23_train, predictions_23_train)
1.0
Predictions on the test data
predictions_test = model_1.predict(X_test)
accuracy_score(y_test, predictions_test)
0.9333333333333333
pd.crosstab(y_test, predictions_test, rownames=['labels'], colnames=['prediction'])
| prediction | 0 | 1 | 2 |
|---|---|---|---|
| labels | |||
| 0 | 11 | 0 | 0 |
| 1 | 0 | 9 | 1 |
| 2 | 0 | 1 | 8 |
predictions_2_test = model_2.predict(X_2_test)
accuracy_score(y_2_test, predictions_2_test)
0.9333333333333333
pd.crosstab(y_2_test, predictions_2_test, rownames=['labels'], colnames=['prediction'])
| prediction | 0 | 1 | 2 |
|---|---|---|---|
| labels | |||
| 0 | 9 | 0 | 0 |
| 1 | 0 | 10 | 0 |
| 2 | 0 | 2 | 9 |
predictions_42_test = model_42.predict(X_42_test)
accuracy_score(y_42_test, predictions_42_test)
1.0
pd.crosstab(y_42_test, predictions_42_test, rownames=['labels'], colnames=['prediction'])
| prediction | 0 | 1 | 2 |
|---|---|---|---|
| labels | |||
| 0 | 10 | 0 | 0 |
| 1 | 0 | 9 | 0 |
| 2 | 0 | 0 | 11 |
predictions_23_test = model_23.predict(X_23_test)
accuracy_score(y_23_test, predictions_23_test)
0.9666666666666667
pd.crosstab(y_23_test, predictions_23_test, rownames=['labels'], colnames=['prediction'])
| prediction | 0 | 1 | 2 |
|---|---|---|---|
| labels | |||
| 0 | 12 | 0 | 0 |
| 1 | 0 | 8 | 0 |
| 2 | 0 | 1 | 9 |
model_d2 = DecisionTreeClassifier(max_depth = 2)
model_d2.fit(X_42_train,y_42_train)
dot_data_d2 = tree.export_graphviz(model_d2, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_d2 = pydotplus.graph_from_dot_data(dot_data_d2)
Image(graph_d2.create_png())
predictions_d2_test = model_d2.predict(X_42_test)
accuracy_score(y_42_test, predictions_d2_test)
0.9666666666666667
pd.crosstab(y_42_test, predictions_d2_test, rownames=['labels'], colnames=['prediction'])
| prediction | 0 | 1 | 2 |
|---|---|---|---|
| labels | |||
| 0 | 10 | 0 | 0 |
| 1 | 0 | 8 | 1 |
| 2 | 0 | 0 | 11 |
model_d3 = DecisionTreeClassifier(max_depth = 3)
model_d3.fit(X_42_train,y_42_train)
dot_data_d3 = tree.export_graphviz(model_d3, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_d3 = pydotplus.graph_from_dot_data(dot_data_d3)
Image(graph_d3.create_png())
predictions_d3_test = model_d3.predict(X_42_test)
accuracy_score(y_42_test, predictions_d3_test)
1.0
We have seen that depending on the datasplit between training and test data the tree looks different, the rules for classifiation are different and the performance varies between the different solutions.
How can we then express if a decision tree of a certain design is a good classifier for the dataset?
Answer: We use statistics.
from sklearn.model_selection import cross_val_score
model_CV = DecisionTreeClassifier()
scores_model_CV = cross_val_score(model_CV, X,y, cv=10, scoring = "accuracy")
print (scores_model_CV)
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.93333333 1. 1. 1. ]
print (scores_model_CV.mean())
0.96
model_CV_d2 = DecisionTreeClassifier(max_depth = 2)
scores_model_CV_d2 = cross_val_score(model_CV_d2, X,y, cv=10, scoring = "accuracy")
print (scores_model_CV_d2)
print ('mean:', scores_model_CV_d2.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.86666667 1. 1. 1. ] mean: 0.9533333333333334
model_CV_d3 = DecisionTreeClassifier(max_depth = 3)
scores_model_CV_d3 = cross_val_score(model_CV_d3, X,y, cv=10, scoring = "accuracy")
print (scores_model_CV_d3)
print ('mean:', scores_model_CV_d3.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. ] mean: 0.96
model_CV_d4 = DecisionTreeClassifier(max_depth = 4)
scores_model_CV_d4 = cross_val_score(model_CV_d4, X,y, cv=10, scoring = "accuracy")
print (scores_model_CV_d4)
print ('mean:', scores_model_CV_d4.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.93333333 0.93333333 1. 1. ] mean: 0.9533333333333334
model_ss_15 = DecisionTreeClassifier(min_samples_split = 15)
scores_model_ss_15 = cross_val_score(model_ss_15, X,y, cv=10, scoring = "accuracy")
print (scores_model_ss_15)
print ('mean:', scores_model_ss_15.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. ] mean: 0.96
model_ss_30 = DecisionTreeClassifier(min_samples_split = 30)
scores_model_ss_30 = cross_val_score(model_ss_30, X,y, cv=10, scoring = "accuracy")
print (scores_model_ss_30)
print ('mean:', scores_model_ss_30.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. ] mean: 0.96
model_sl_5 = DecisionTreeClassifier(min_samples_leaf = 5)
scores_model_sl_5 = cross_val_score(model_sl_5, X,y, cv=10, scoring = "accuracy")
print (scores_model_sl_5)
print ('mean:', scores_model_sl_5.mean())
[1. 0.93333333 1. 1. 0.93333333 0.86666667 0.86666667 1. 1. 1. ] mean: 0.9600000000000002
model_sl_4 = DecisionTreeClassifier(min_samples_leaf = 4)
scores_model_sl_4 = cross_val_score(model_sl_4, X,y, cv=10, scoring = "accuracy")
print (scores_model_sl_4)
print ('mean:', scores_model_sl_4.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.86666667 1. 1. 1. ] mean: 0.96
model_sl_3 = DecisionTreeClassifier(min_samples_leaf = 3)
scores_model_sl_3 = cross_val_score(model_sl_3, X,y, cv=10, scoring = "accuracy")
print (scores_model_sl_3)
print ('mean:', scores_model_sl_3.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. 1. ] mean: 0.9666666666666666
A short note on cross validation:¶
scores_model_CV = cross_val_score(model_CV, X,y, cv=10, scoring = "accuracy")
print (scores_model_CV)
print (scores_model_CV.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.93333333 1. 1. 1. ] 0.96
scores_model_CV = cross_val_score(model_CV, X,y, cv=5, scoring = "accuracy")
print (scores_model_CV)
print (scores_model_CV.mean())
[0.96666667 0.96666667 0.9 0.96666667 1. ] 0.9600000000000002
scores_model_CV = cross_val_score(model_CV, X,y, cv=2, scoring = "accuracy")
print (scores_model_CV)
print (scores_model_CV.mean())
[0.96 0.96] 0.96
scores_model_CV = cross_val_score(model_CV, X,y, cv=50, scoring = "accuracy")
print (scores_model_CV)
print (scores_model_CV.mean())
[1. 1. 1. 1. 1. 1. 0.66666667 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.66666667 0.66666667 1. 1. 1. 1. 1. 1. 0.66666667 1. 0.66666667 1. 1. 1. 0.66666667 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. ] 0.96
model_sl_3 = DecisionTreeClassifier(min_samples_leaf = 4)
model_sl_3.fit(X_42_train,y_42_train)
dot_data_sl_3 = tree.export_graphviz(model_sl_3, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_sl_3 = pydotplus.graph_from_dot_data(dot_data_sl_3)
Image(graph_sl_3.create_png())
The above tree is only for a specific dataset, so we do not know how the general performace of a tree with parameter settings would perform. Therfore we can use the same parameter settings and test in in cross validation.
model_ss_44 = DecisionTreeClassifier(min_samples_split = 44)
model_ss_44.fit(X_42_train,y_42_train)
dot_data_ss_44 = tree.export_graphviz(model_ss_44, out_file=None,
feature_names=['sepal-length','sepal-width','petal-length','petal-width'],
label='all', rounded=True, filled=True)
graph_ss_44 = pydotplus.graph_from_dot_data(dot_data_ss_44)
Image(graph_ss_44.create_png())
model_ss_44 = DecisionTreeClassifier(min_samples_split = 44)
scores_model_ss_44 = cross_val_score(model_ss_44, X,y, cv=10, scoring = "accuracy")
print (scores_model_ss_44)
print ('mean:', scores_model_ss_44.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. 1. ] mean: 0.9666666666666666
Feature scaling¶
from sklearn import preprocessing
normalized_X = preprocessing.normalize(X)
standardized_X = preprocessing.scale(X)
X
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4],
[5.1, 3.5, 1.4, 0.3],
[5.7, 3.8, 1.7, 0.3],
[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 6.9, 2.3],
[6. , 2.2, 5. , 1.5],
[6.9, 3.2, 5.7, 2.3],
[5.6, 2.8, 4.9, 2. ],
[7.7, 2.8, 6.7, 2. ],
[6.3, 2.7, 4.9, 1.8],
[6.7, 3.3, 5.7, 2.1],
[7.2, 3.2, 6. , 1.8],
[6.2, 2.8, 4.8, 1.8],
[6.1, 3. , 4.9, 1.8],
[6.4, 2.8, 5.6, 2.1],
[7.2, 3. , 5.8, 1.6],
[7.4, 2.8, 6.1, 1.9],
[7.9, 3.8, 6.4, 2. ],
[6.4, 2.8, 5.6, 2.2],
[6.3, 2.8, 5.1, 1.5],
[6.1, 2.6, 5.6, 1.4],
[7.7, 3. , 6.1, 2.3],
[6.3, 3.4, 5.6, 2.4],
[6.4, 3.1, 5.5, 1.8],
[6. , 3. , 4.8, 1.8],
[6.9, 3.1, 5.4, 2.1],
[6.7, 3.1, 5.6, 2.4],
[6.9, 3.1, 5.1, 2.3],
[5.8, 2.7, 5.1, 1.9],
[6.8, 3.2, 5.9, 2.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]])
normalized_X
array([[0.80377277, 0.55160877, 0.22064351, 0.0315205 ],
[0.82813287, 0.50702013, 0.23660939, 0.03380134],
[0.80533308, 0.54831188, 0.2227517 , 0.03426949],
[0.80003025, 0.53915082, 0.26087943, 0.03478392],
[0.790965 , 0.5694948 , 0.2214702 , 0.0316386 ],
[0.78417499, 0.5663486 , 0.2468699 , 0.05808704],
[0.78010936, 0.57660257, 0.23742459, 0.0508767 ],
[0.80218492, 0.54548574, 0.24065548, 0.0320874 ],
[0.80642366, 0.5315065 , 0.25658935, 0.03665562],
[0.81803119, 0.51752994, 0.25041771, 0.01669451],
[0.80373519, 0.55070744, 0.22325977, 0.02976797],
[0.786991 , 0.55745196, 0.26233033, 0.03279129],
[0.82307218, 0.51442011, 0.24006272, 0.01714734],
[0.8025126 , 0.55989251, 0.20529392, 0.01866308],
[0.81120865, 0.55945424, 0.16783627, 0.02797271],
[0.77381111, 0.59732787, 0.2036345 , 0.05430253],
[0.79428944, 0.57365349, 0.19121783, 0.05883625],
[0.80327412, 0.55126656, 0.22050662, 0.04725142],
[0.8068282 , 0.53788547, 0.24063297, 0.04246464],
[0.77964883, 0.58091482, 0.22930848, 0.0458617 ],
[0.8173379 , 0.51462016, 0.25731008, 0.03027177],
[0.78591858, 0.57017622, 0.23115252, 0.06164067],
[0.77577075, 0.60712493, 0.16864581, 0.03372916],
[0.80597792, 0.52151512, 0.26865931, 0.07901744],
[0.776114 , 0.54974742, 0.30721179, 0.03233808],
[0.82647451, 0.4958847 , 0.26447184, 0.03305898],
[0.79778206, 0.5424918 , 0.25529026, 0.06382256],
[0.80641965, 0.54278246, 0.23262105, 0.03101614],
[0.81609427, 0.5336001 , 0.21971769, 0.03138824],
[0.79524064, 0.54144043, 0.27072022, 0.03384003],
[0.80846584, 0.52213419, 0.26948861, 0.03368608],
[0.82225028, 0.51771314, 0.22840286, 0.06090743],
[0.76578311, 0.60379053, 0.22089897, 0.0147266 ],
[0.77867447, 0.59462414, 0.19820805, 0.02831544],
[0.81768942, 0.51731371, 0.25031309, 0.03337508],
[0.82512295, 0.52807869, 0.19802951, 0.03300492],
[0.82699754, 0.52627116, 0.19547215, 0.03007264],
[0.78523221, 0.5769053 , 0.22435206, 0.01602515],
[0.80212413, 0.54690282, 0.23699122, 0.03646019],
[0.80779568, 0.53853046, 0.23758697, 0.03167826],
[0.80033301, 0.56023311, 0.20808658, 0.04801998],
[0.86093857, 0.44003527, 0.24871559, 0.0573959 ],
[0.78609038, 0.57170209, 0.23225397, 0.03573138],
[0.78889479, 0.55222635, 0.25244633, 0.09466737],
[0.76693897, 0.57144472, 0.28572236, 0.06015208],
[0.82210585, 0.51381615, 0.23978087, 0.05138162],
[0.77729093, 0.57915795, 0.24385598, 0.030482 ],
[0.79594782, 0.55370283, 0.24224499, 0.03460643],
[0.79837025, 0.55735281, 0.22595384, 0.03012718],
[0.81228363, 0.5361072 , 0.22743942, 0.03249135],
[0.76701103, 0.35063361, 0.51499312, 0.15340221],
[0.74549757, 0.37274878, 0.52417798, 0.17472599],
[0.75519285, 0.33928954, 0.53629637, 0.16417236],
[0.75384916, 0.31524601, 0.54825394, 0.17818253],
[0.7581754 , 0.32659863, 0.5365549 , 0.17496355],
[0.72232962, 0.35482858, 0.57026022, 0.16474184],
[0.72634846, 0.38046824, 0.54187901, 0.18446945],
[0.75916547, 0.37183615, 0.51127471, 0.15493173],
[0.76301853, 0.33526572, 0.53180079, 0.15029153],
[0.72460233, 0.37623583, 0.54345175, 0.19508524],
[0.76923077, 0.30769231, 0.53846154, 0.15384615],
[0.73923462, 0.37588201, 0.52623481, 0.187941 ],
[0.78892752, 0.28927343, 0.52595168, 0.13148792],
[0.73081412, 0.34743622, 0.56308629, 0.16772783],
[0.75911707, 0.3931142 , 0.48800383, 0.17622361],
[0.76945444, 0.35601624, 0.50531337, 0.16078153],
[0.70631892, 0.37838513, 0.5675777 , 0.18919257],
[0.75676497, 0.35228714, 0.53495455, 0.13047672],
[0.76444238, 0.27125375, 0.55483721, 0.18494574],
[0.76185188, 0.34011245, 0.53057542, 0.14964948],
[0.6985796 , 0.37889063, 0.56833595, 0.21312598],
[0.77011854, 0.35349703, 0.50499576, 0.16412362],
[0.74143307, 0.29421947, 0.57667016, 0.17653168],
[0.73659895, 0.33811099, 0.56754345, 0.14490471],
[0.76741698, 0.34773582, 0.51560829, 0.15588157],
[0.76785726, 0.34902603, 0.51190484, 0.16287881],
[0.76467269, 0.31486523, 0.53976896, 0.15743261],
[0.74088576, 0.33173989, 0.55289982, 0.18798594],
[0.73350949, 0.35452959, 0.55013212, 0.18337737],
[0.78667474, 0.35883409, 0.48304589, 0.13801311],
[0.76521855, 0.33391355, 0.52869645, 0.15304371],
[0.77242925, 0.33706004, 0.51963422, 0.14044168],
[0.76434981, 0.35581802, 0.51395936, 0.15814134],
[0.70779525, 0.31850786, 0.60162596, 0.1887454 ],
[0.69333409, 0.38518561, 0.57777841, 0.1925928 ],
[0.71524936, 0.40530797, 0.53643702, 0.19073316],
[0.75457341, 0.34913098, 0.52932761, 0.16893434],
[0.77530021, 0.28304611, 0.54147951, 0.15998258],
[0.72992443, 0.39103094, 0.53440896, 0.16944674],
[0.74714194, 0.33960997, 0.54337595, 0.17659719],
[0.72337118, 0.34195729, 0.57869695, 0.15782644],
[0.73260391, 0.36029701, 0.55245541, 0.1681386 ],
[0.76262994, 0.34186859, 0.52595168, 0.1577855 ],
[0.76986879, 0.35413965, 0.5081134 , 0.15397376],
[0.73544284, 0.35458851, 0.55158213, 0.1707278 ],
[0.73239618, 0.38547167, 0.53966034, 0.15418867],
[0.73446047, 0.37367287, 0.5411814 , 0.16750853],
[0.75728103, 0.3542121 , 0.52521104, 0.15878473],
[0.78258054, 0.38361791, 0.4603415 , 0.16879188],
[0.7431482 , 0.36505526, 0.5345452 , 0.16948994],
[0.65387747, 0.34250725, 0.62274045, 0.25947519],
[0.69052512, 0.32145135, 0.60718588, 0.22620651],
[0.71491405, 0.30207636, 0.59408351, 0.21145345],
[0.69276796, 0.31889319, 0.61579374, 0.1979337 ],
[0.68619022, 0.31670318, 0.61229281, 0.232249 ],
[0.70953708, 0.28008043, 0.61617694, 0.1960563 ],
[0.67054118, 0.34211284, 0.61580312, 0.23263673],
[0.71366557, 0.28351098, 0.61590317, 0.17597233],
[0.71414125, 0.26647062, 0.61821183, 0.19185884],
[0.69198788, 0.34599394, 0.58626751, 0.24027357],
[0.71562645, 0.3523084 , 0.56149152, 0.22019275],
[0.71576546, 0.30196356, 0.59274328, 0.21249287],
[0.71718148, 0.31640359, 0.58007326, 0.22148252],
[0.6925518 , 0.30375079, 0.60750157, 0.24300063],
[0.67767924, 0.32715549, 0.59589036, 0.28041899],
[0.69589887, 0.34794944, 0.57629125, 0.25008866],
[0.70610474, 0.3258945 , 0.59747324, 0.1955367 ],
[0.69299099, 0.34199555, 0.60299216, 0.19799743],
[0.70600618, 0.2383917 , 0.63265489, 0.21088496],
[0.72712585, 0.26661281, 0.60593821, 0.18178146],
[0.70558934, 0.32722984, 0.58287815, 0.23519645],
[0.68307923, 0.34153961, 0.59769433, 0.24395687],
[0.71486543, 0.25995106, 0.62202576, 0.18567933],
[0.73122464, 0.31338199, 0.56873028, 0.20892133],
[0.69595601, 0.3427843 , 0.59208198, 0.21813547],
[0.71529453, 0.31790868, 0.59607878, 0.17882363],
[0.72785195, 0.32870733, 0.56349829, 0.21131186],
[0.71171214, 0.35002236, 0.57170319, 0.21001342],
[0.69594002, 0.30447376, 0.60894751, 0.22835532],
[0.73089855, 0.30454106, 0.58877939, 0.1624219 ],
[0.72766159, 0.27533141, 0.59982915, 0.18683203],
[0.71578999, 0.34430405, 0.5798805 , 0.18121266],
[0.69417747, 0.30370264, 0.60740528, 0.2386235 ],
[0.72366005, 0.32162669, 0.58582004, 0.17230001],
[0.69385414, 0.29574111, 0.63698085, 0.15924521],
[0.73154399, 0.28501714, 0.57953485, 0.21851314],
[0.67017484, 0.36168166, 0.59571097, 0.2553047 ],
[0.69804799, 0.338117 , 0.59988499, 0.196326 ],
[0.71066905, 0.35533453, 0.56853524, 0.21320072],
[0.72415258, 0.32534391, 0.56672811, 0.22039426],
[0.69997037, 0.32386689, 0.58504986, 0.25073566],
[0.73337886, 0.32948905, 0.54206264, 0.24445962],
[0.69052512, 0.32145135, 0.60718588, 0.22620651],
[0.69193502, 0.32561648, 0.60035539, 0.23403685],
[0.68914871, 0.33943145, 0.58629069, 0.25714504],
[0.72155725, 0.32308533, 0.56001458, 0.24769876],
[0.72965359, 0.28954508, 0.57909015, 0.22005426],
[0.71653899, 0.3307103 , 0.57323119, 0.22047353],
[0.67467072, 0.36998072, 0.58761643, 0.25028107],
[0.69025916, 0.35097923, 0.5966647 , 0.21058754]])
standardized_X
array([[-9.00681170e-01, 1.01900435e+00, -1.34022653e+00,
-1.31544430e+00],
[-1.14301691e+00, -1.31979479e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.38535265e+00, 3.28414053e-01, -1.39706395e+00,
-1.31544430e+00],
[-1.50652052e+00, 9.82172869e-02, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.24920112e+00, -1.34022653e+00,
-1.31544430e+00],
[-5.37177559e-01, 1.93979142e+00, -1.16971425e+00,
-1.05217993e+00],
[-1.50652052e+00, 7.88807586e-01, -1.34022653e+00,
-1.18381211e+00],
[-1.02184904e+00, 7.88807586e-01, -1.28338910e+00,
-1.31544430e+00],
[-1.74885626e+00, -3.62176246e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.14301691e+00, 9.82172869e-02, -1.28338910e+00,
-1.44707648e+00],
[-5.37177559e-01, 1.47939788e+00, -1.28338910e+00,
-1.31544430e+00],
[-1.26418478e+00, 7.88807586e-01, -1.22655167e+00,
-1.31544430e+00],
[-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,
-1.44707648e+00],
[-1.87002413e+00, -1.31979479e-01, -1.51073881e+00,
-1.44707648e+00],
[-5.25060772e-02, 2.16998818e+00, -1.45390138e+00,
-1.31544430e+00],
[-1.73673948e-01, 3.09077525e+00, -1.28338910e+00,
-1.05217993e+00],
[-5.37177559e-01, 1.93979142e+00, -1.39706395e+00,
-1.05217993e+00],
[-9.00681170e-01, 1.01900435e+00, -1.34022653e+00,
-1.18381211e+00],
[-1.73673948e-01, 1.70959465e+00, -1.16971425e+00,
-1.18381211e+00],
[-9.00681170e-01, 1.70959465e+00, -1.28338910e+00,
-1.18381211e+00],
[-5.37177559e-01, 7.88807586e-01, -1.16971425e+00,
-1.31544430e+00],
[-9.00681170e-01, 1.47939788e+00, -1.28338910e+00,
-1.05217993e+00],
[-1.50652052e+00, 1.24920112e+00, -1.56757623e+00,
-1.31544430e+00],
[-9.00681170e-01, 5.58610819e-01, -1.16971425e+00,
-9.20547742e-01],
[-1.26418478e+00, 7.88807586e-01, -1.05603939e+00,
-1.31544430e+00],
[-1.02184904e+00, -1.31979479e-01, -1.22655167e+00,
-1.31544430e+00],
[-1.02184904e+00, 7.88807586e-01, -1.22655167e+00,
-1.05217993e+00],
[-7.79513300e-01, 1.01900435e+00, -1.28338910e+00,
-1.31544430e+00],
[-7.79513300e-01, 7.88807586e-01, -1.34022653e+00,
-1.31544430e+00],
[-1.38535265e+00, 3.28414053e-01, -1.22655167e+00,
-1.31544430e+00],
[-1.26418478e+00, 9.82172869e-02, -1.22655167e+00,
-1.31544430e+00],
[-5.37177559e-01, 7.88807586e-01, -1.28338910e+00,
-1.05217993e+00],
[-7.79513300e-01, 2.40018495e+00, -1.28338910e+00,
-1.44707648e+00],
[-4.16009689e-01, 2.63038172e+00, -1.34022653e+00,
-1.31544430e+00],
[-1.14301691e+00, 9.82172869e-02, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 3.28414053e-01, -1.45390138e+00,
-1.31544430e+00],
[-4.16009689e-01, 1.01900435e+00, -1.39706395e+00,
-1.31544430e+00],
[-1.14301691e+00, 1.24920112e+00, -1.34022653e+00,
-1.44707648e+00],
[-1.74885626e+00, -1.31979479e-01, -1.39706395e+00,
-1.31544430e+00],
[-9.00681170e-01, 7.88807586e-01, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.01900435e+00, -1.39706395e+00,
-1.18381211e+00],
[-1.62768839e+00, -1.74335684e+00, -1.39706395e+00,
-1.18381211e+00],
[-1.74885626e+00, 3.28414053e-01, -1.39706395e+00,
-1.31544430e+00],
[-1.02184904e+00, 1.01900435e+00, -1.22655167e+00,
-7.88915558e-01],
[-9.00681170e-01, 1.70959465e+00, -1.05603939e+00,
-1.05217993e+00],
[-1.26418478e+00, -1.31979479e-01, -1.34022653e+00,
-1.18381211e+00],
[-9.00681170e-01, 1.70959465e+00, -1.22655167e+00,
-1.31544430e+00],
[-1.50652052e+00, 3.28414053e-01, -1.34022653e+00,
-1.31544430e+00],
[-6.58345429e-01, 1.47939788e+00, -1.28338910e+00,
-1.31544430e+00],
[-1.02184904e+00, 5.58610819e-01, -1.34022653e+00,
-1.31544430e+00],
[ 1.40150837e+00, 3.28414053e-01, 5.35408562e-01,
2.64141916e-01],
[ 6.74501145e-01, 3.28414053e-01, 4.21733708e-01,
3.95774101e-01],
[ 1.28034050e+00, 9.82172869e-02, 6.49083415e-01,
3.95774101e-01],
[-4.16009689e-01, -1.74335684e+00, 1.37546573e-01,
1.32509732e-01],
[ 7.95669016e-01, -5.92373012e-01, 4.78571135e-01,
3.95774101e-01],
[-1.73673948e-01, -5.92373012e-01, 4.21733708e-01,
1.32509732e-01],
[ 5.53333275e-01, 5.58610819e-01, 5.35408562e-01,
5.27406285e-01],
[-1.14301691e+00, -1.51316008e+00, -2.60315415e-01,
-2.62386821e-01],
[ 9.16836886e-01, -3.62176246e-01, 4.78571135e-01,
1.32509732e-01],
[-7.79513300e-01, -8.22569778e-01, 8.07091462e-02,
2.64141916e-01],
[-1.02184904e+00, -2.43394714e+00, -1.46640561e-01,
-2.62386821e-01],
[ 6.86617933e-02, -1.31979479e-01, 2.51221427e-01,
3.95774101e-01],
[ 1.89829664e-01, -1.97355361e+00, 1.37546573e-01,
-2.62386821e-01],
[ 3.10997534e-01, -3.62176246e-01, 5.35408562e-01,
2.64141916e-01],
[-2.94841818e-01, -3.62176246e-01, -8.98031345e-02,
1.32509732e-01],
[ 1.03800476e+00, 9.82172869e-02, 3.64896281e-01,
2.64141916e-01],
[-2.94841818e-01, -1.31979479e-01, 4.21733708e-01,
3.95774101e-01],
[-5.25060772e-02, -8.22569778e-01, 1.94384000e-01,
-2.62386821e-01],
[ 4.32165405e-01, -1.97355361e+00, 4.21733708e-01,
3.95774101e-01],
[-2.94841818e-01, -1.28296331e+00, 8.07091462e-02,
-1.30754636e-01],
[ 6.86617933e-02, 3.28414053e-01, 5.92245988e-01,
7.90670654e-01],
[ 3.10997534e-01, -5.92373012e-01, 1.37546573e-01,
1.32509732e-01],
[ 5.53333275e-01, -1.28296331e+00, 6.49083415e-01,
3.95774101e-01],
[ 3.10997534e-01, -5.92373012e-01, 5.35408562e-01,
8.77547895e-04],
[ 6.74501145e-01, -3.62176246e-01, 3.08058854e-01,
1.32509732e-01],
[ 9.16836886e-01, -1.31979479e-01, 3.64896281e-01,
2.64141916e-01],
[ 1.15917263e+00, -5.92373012e-01, 5.92245988e-01,
2.64141916e-01],
[ 1.03800476e+00, -1.31979479e-01, 7.05920842e-01,
6.59038469e-01],
[ 1.89829664e-01, -3.62176246e-01, 4.21733708e-01,
3.95774101e-01],
[-1.73673948e-01, -1.05276654e+00, -1.46640561e-01,
-2.62386821e-01],
[-4.16009689e-01, -1.51316008e+00, 2.38717193e-02,
-1.30754636e-01],
[-4.16009689e-01, -1.51316008e+00, -3.29657076e-02,
-2.62386821e-01],
[-5.25060772e-02, -8.22569778e-01, 8.07091462e-02,
8.77547895e-04],
[ 1.89829664e-01, -8.22569778e-01, 7.62758269e-01,
5.27406285e-01],
[-5.37177559e-01, -1.31979479e-01, 4.21733708e-01,
3.95774101e-01],
[ 1.89829664e-01, 7.88807586e-01, 4.21733708e-01,
5.27406285e-01],
[ 1.03800476e+00, 9.82172869e-02, 5.35408562e-01,
3.95774101e-01],
[ 5.53333275e-01, -1.74335684e+00, 3.64896281e-01,
1.32509732e-01],
[-2.94841818e-01, -1.31979479e-01, 1.94384000e-01,
1.32509732e-01],
[-4.16009689e-01, -1.28296331e+00, 1.37546573e-01,
1.32509732e-01],
[-4.16009689e-01, -1.05276654e+00, 3.64896281e-01,
8.77547895e-04],
[ 3.10997534e-01, -1.31979479e-01, 4.78571135e-01,
2.64141916e-01],
[-5.25060772e-02, -1.05276654e+00, 1.37546573e-01,
8.77547895e-04],
[-1.02184904e+00, -1.74335684e+00, -2.60315415e-01,
-2.62386821e-01],
[-2.94841818e-01, -8.22569778e-01, 2.51221427e-01,
1.32509732e-01],
[-1.73673948e-01, -1.31979479e-01, 2.51221427e-01,
8.77547895e-04],
[-1.73673948e-01, -3.62176246e-01, 2.51221427e-01,
1.32509732e-01],
[ 4.32165405e-01, -3.62176246e-01, 3.08058854e-01,
1.32509732e-01],
[-9.00681170e-01, -1.28296331e+00, -4.30827696e-01,
-1.30754636e-01],
[-1.73673948e-01, -5.92373012e-01, 1.94384000e-01,
1.32509732e-01],
[ 5.53333275e-01, 5.58610819e-01, 1.27429511e+00,
1.71209594e+00],
[-5.25060772e-02, -8.22569778e-01, 7.62758269e-01,
9.22302838e-01],
[ 1.52267624e+00, -1.31979479e-01, 1.21745768e+00,
1.18556721e+00],
[ 5.53333275e-01, -3.62176246e-01, 1.04694540e+00,
7.90670654e-01],
[ 7.95669016e-01, -1.31979479e-01, 1.16062026e+00,
1.31719939e+00],
[ 2.12851559e+00, -1.31979479e-01, 1.61531967e+00,
1.18556721e+00],
[-1.14301691e+00, -1.28296331e+00, 4.21733708e-01,
6.59038469e-01],
[ 1.76501198e+00, -3.62176246e-01, 1.44480739e+00,
7.90670654e-01],
[ 1.03800476e+00, -1.28296331e+00, 1.16062026e+00,
7.90670654e-01],
[ 1.64384411e+00, 1.24920112e+00, 1.33113254e+00,
1.71209594e+00],
[ 7.95669016e-01, 3.28414053e-01, 7.62758269e-01,
1.05393502e+00],
[ 6.74501145e-01, -8.22569778e-01, 8.76433123e-01,
9.22302838e-01],
[ 1.15917263e+00, -1.31979479e-01, 9.90107977e-01,
1.18556721e+00],
[-1.73673948e-01, -1.28296331e+00, 7.05920842e-01,
1.05393502e+00],
[-5.25060772e-02, -5.92373012e-01, 7.62758269e-01,
1.58046376e+00],
[ 6.74501145e-01, 3.28414053e-01, 8.76433123e-01,
1.44883158e+00],
[ 7.95669016e-01, -1.31979479e-01, 9.90107977e-01,
7.90670654e-01],
[ 2.24968346e+00, 1.70959465e+00, 1.67215710e+00,
1.31719939e+00],
[ 2.24968346e+00, -1.05276654e+00, 1.78583195e+00,
1.44883158e+00],
[ 1.89829664e-01, -1.97355361e+00, 7.05920842e-01,
3.95774101e-01],
[ 1.28034050e+00, 3.28414053e-01, 1.10378283e+00,
1.44883158e+00],
[-2.94841818e-01, -5.92373012e-01, 6.49083415e-01,
1.05393502e+00],
[ 2.24968346e+00, -5.92373012e-01, 1.67215710e+00,
1.05393502e+00],
[ 5.53333275e-01, -8.22569778e-01, 6.49083415e-01,
7.90670654e-01],
[ 1.03800476e+00, 5.58610819e-01, 1.10378283e+00,
1.18556721e+00],
[ 1.64384411e+00, 3.28414053e-01, 1.27429511e+00,
7.90670654e-01],
[ 4.32165405e-01, -5.92373012e-01, 5.92245988e-01,
7.90670654e-01],
[ 3.10997534e-01, -1.31979479e-01, 6.49083415e-01,
7.90670654e-01],
[ 6.74501145e-01, -5.92373012e-01, 1.04694540e+00,
1.18556721e+00],
[ 1.64384411e+00, -1.31979479e-01, 1.16062026e+00,
5.27406285e-01],
[ 1.88617985e+00, -5.92373012e-01, 1.33113254e+00,
9.22302838e-01],
[ 2.49201920e+00, 1.70959465e+00, 1.50164482e+00,
1.05393502e+00],
[ 6.74501145e-01, -5.92373012e-01, 1.04694540e+00,
1.31719939e+00],
[ 5.53333275e-01, -5.92373012e-01, 7.62758269e-01,
3.95774101e-01],
[ 3.10997534e-01, -1.05276654e+00, 1.04694540e+00,
2.64141916e-01],
[ 2.24968346e+00, -1.31979479e-01, 1.33113254e+00,
1.44883158e+00],
[ 5.53333275e-01, 7.88807586e-01, 1.04694540e+00,
1.58046376e+00],
[ 6.74501145e-01, 9.82172869e-02, 9.90107977e-01,
7.90670654e-01],
[ 1.89829664e-01, -1.31979479e-01, 5.92245988e-01,
7.90670654e-01],
[ 1.28034050e+00, 9.82172869e-02, 9.33270550e-01,
1.18556721e+00],
[ 1.03800476e+00, 9.82172869e-02, 1.04694540e+00,
1.58046376e+00],
[ 1.28034050e+00, 9.82172869e-02, 7.62758269e-01,
1.44883158e+00],
[-5.25060772e-02, -8.22569778e-01, 7.62758269e-01,
9.22302838e-01],
[ 1.15917263e+00, 3.28414053e-01, 1.21745768e+00,
1.44883158e+00],
[ 1.03800476e+00, 5.58610819e-01, 1.10378283e+00,
1.71209594e+00],
[ 1.03800476e+00, -1.31979479e-01, 8.19595696e-01,
1.44883158e+00],
[ 5.53333275e-01, -1.28296331e+00, 7.05920842e-01,
9.22302838e-01],
[ 7.95669016e-01, -1.31979479e-01, 8.19595696e-01,
1.05393502e+00],
[ 4.32165405e-01, 7.88807586e-01, 9.33270550e-01,
1.44883158e+00],
[ 6.86617933e-02, -1.31979479e-01, 7.62758269e-01,
7.90670654e-01]])
df_X = pd.DataFrame(X, columns = ['sepal_length',
'sepal_width',
'petal_length',
'petal_width'])
df_X.head()
| sepal_length | sepal_width | petal_length | petal_width | |
|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 |
visual_X = pd.plotting.scatter_matrix(df_X, figsize = [10,10], c = colormap[y], s = 150)
df_X_normalized = pd.DataFrame(normalized_X, columns = ['sepal_length',
'sepal_width',
'petal_length',
'petal_width'])
df_X_normalized.head()
| sepal_length | sepal_width | petal_length | petal_width | |
|---|---|---|---|---|
| 0 | 0.803773 | 0.551609 | 0.220644 | 0.031521 |
| 1 | 0.828133 | 0.507020 | 0.236609 | 0.033801 |
| 2 | 0.805333 | 0.548312 | 0.222752 | 0.034269 |
| 3 | 0.800030 | 0.539151 | 0.260879 | 0.034784 |
| 4 | 0.790965 | 0.569495 | 0.221470 | 0.031639 |
df_X_standardized = pd.DataFrame(standardized_X, columns = ['sepal_length',
'sepal_width',
'petal_length',
'petal_width'])
df_X_standardized.head()
| 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 |
visual_X_standardized = pd.plotting.scatter_matrix(df_X_standardized, figsize = [10,10], c = colormap[y], s = 150)
model_normalized_CV = DecisionTreeClassifier()
scores_model_normalized_CV = cross_val_score(model_normalized_CV, normalized_X,y, cv=10, scoring = "accuracy")
print (scores_model_normalized_CV)
print (scores_model_normalized_CV.mean())
[1. 0.93333333 0.93333333 1. 0.93333333 1. 0.86666667 1. 0.86666667 1. ] 0.9533333333333334
model_standardized_CV = DecisionTreeClassifier()
scores_model_standardized_CV = cross_val_score(model_standardized_CV, standardized_X,y, cv=10, scoring = "accuracy")
print (scores_model_standardized_CV)
print (scores_model_standardized_CV.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.93333333 1. 1. 1. ] 0.96
model_CV = DecisionTreeClassifier()
scores_model_CV = cross_val_score(model_CV,X,y, cv=10, scoring = "accuracy")
print (scores_model_CV)
print (scores_model_CV.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.86666667 0.93333333 1. 1. 1. ] 0.96
model_standardized_CV_ss_44 = DecisionTreeClassifier(min_samples_split = 44)
scores_model_standardized_CV_ss_44 = cross_val_score(model_standardized_CV_ss_44, standardized_X,y, cv=10, scoring = "accuracy")
print (scores_model_standardized_CV_ss_44)
print (scores_model_standardized_CV_ss_44.mean())
[1. 0.93333333 1. 0.93333333 0.93333333 0.93333333 0.93333333 1. 1. 1. ] 0.9666666666666666
You can also use the seaborn library to plot.¶
import seaborn as sns
pairplot.set(xlim=(0,10), ylim = (0,10))
<seaborn.axisgrid.PairGrid at 0x1e4cada04d0>
iris_sns = sns.load_dataset("iris")
sns.pairplot(iris_sns, hue = 'species')
# you get a warning about something that will be removed from the function in the future. We can ignore that for now.
# after the warning the pairplot will appear.
C:\Users\stei\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\stei\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\stei\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
C:\Users\stei\AppData\Local\anaconda3\Lib\site-packages\seaborn\_oldcore.py:1119: FutureWarning: use_inf_as_na option is deprecated and will be removed in a future version. Convert inf values to NaN before operating instead.
with pd.option_context('mode.use_inf_as_na', True):
<seaborn.axisgrid.PairGrid at 0x1e4d5ff64d0>