Files
ppse/HW5/testing_scikit_ntb.ipynb
2026-08-24 13:49:19 +02:00

387 KiB

In [1]:
from sklearn import datasets
import seaborn as sns


iris = datasets.load_iris(as_frame=True)
print(iris.frame)

sns.pairplot(iris.frame, hue='target')
Out [1]:
     sepal length (cm)  sepal width (cm)  petal length (cm)  petal width (cm)  \
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   
..                 ...               ...                ...               ...   
145                6.7               3.0                5.2               2.3   
146                6.3               2.5                5.0               1.9   
147                6.5               3.0                5.2               2.0   
148                6.2               3.4                5.4               2.3   
149                5.9               3.0                5.1               1.8   

     target  
0         0  
1         0  
2         0  
3         0  
4         0  
..      ...  
145       2  
146       2  
147       2  
148       2  
149       2  

[150 rows x 5 columns]
<seaborn.axisgrid.PairGrid at 0x7f3ce386fa10>
In [4]:
from sklearn.neighbors import KNeighborsClassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(iris.data, iris.target, test_size=0.2)

model = KNeighborsClassifier(n_neighbors=3)
model.fit(Xtrain, Ytrain)

Ypred = model.predict(Xtest)
print("True labels:", Ytest)
print("Predictions:", Ypred)
True labels: [1 1 1 2 2 1 2 1 2 2 2 0 0 0 1 1 1 1 0 0 2 2 2 1 0 1 1 0 0 1]
Predictions: [1 1 1 1 2 1 2 1 2 2 2 0 0 0 1 1 1 1 0 0 2 2 2 1 0 1 1 0 0 1]
In [5]:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
from sklearn import datasets
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(iris.data, iris.target, test_size=0.2)

#create the models
lda = LinearDiscriminantAnalysis(solver='eigen', shrinkage='auto')
qda = QuadraticDiscriminantAnalysis()

#train the models
lda.fit(Xtrain, Ytrain)
qda.fit(Xtrain, Ytrain)

#use the models
Ylda = lda.predict(Xtest)
Yqda = qda.predict(Xtest)
print("True labels:", Ytest)
print("LDA predictions:", Ylda)
print("QDA predictions:", Yqda)
True labels: [0 0 2 1 2 1 0 2 1 0 2 2 1 1 0 2 0 2 1 0 0 1 1 1 1 1 2 0 2 1]
LDA predictions: [0 0 2 1 2 1 0 2 1 0 2 2 1 1 0 2 0 2 1 0 0 1 1 1 1 1 2 0 2 2]
QDA predictions: [0 0 2 1 2 1 0 2 1 0 2 2 1 1 0 2 0 2 1 0 0 1 1 1 1 1 2 0 2 1]
In [7]:
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn import datasets
import matplotlib.pyplot as plt

iris = datasets.load_iris()
X = iris.data
y = iris.target

#create the LDA model with dimensionality reduction
lda = LinearDiscriminantAnalysis(n_components=2)

X_tr = lda.fit(X, y).transform(X)

plt.scatter(X_tr[:, 0], X_tr[:, 1], c=y, cmap='viridis', edgecolor='k')
plt.xlabel('LDA component 1')
plt.ylabel('LDA component 2')
Out [7]:
Text(0, 0.5, 'LDA component 2')
In [12]:
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
import numpy as np

values = np.array(['cat', 'dog', 'fish', 'cat', 'dog'])

label_encoder = LabelEncoder()

integer_encoded = label_encoder.fit_transform(values)
print("Integer encoded:", integer_encoded)
integer_encoded = integer_encoded.reshape(-1, 1)

onehot_encoder = OneHotEncoder(sparse_output=False)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
print("One-hot encoded:\n", onehot_encoded)

inverted = label_encoder.inverse_transform([np.argmax(onehot_encoded[0, :])])
print(onehot_encoded[0, :])
print(np.argmax(onehot_encoded[0, :]))
print("Inverted from one-hot:", inverted)
Integer encoded: [0 1 2 0 1]
One-hot encoded:
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]
 [1. 0. 0.]
 [0. 1. 0.]]
[1. 0. 0.]
0
Inverted from one-hot: ['cat']