This commit is contained in:
Farina Louis 2021-12-08 15:03:00 +01:00
джерело b9636a6ad0
коміт 277dafbfb7
3 змінених файлів з 307 додано та 0 видалено

94
tp1.py Звичайний файл

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
import random
import time
"""
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
"""
"""
---Ex 1---
images = mnist.data.values.reshape((-1, 28, 28))
plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
print(mnist.target[0])
"""
"""
---Ex 2---
"""
indices = [i for i in range(len(mnist.data))]
random.shuffle(indices)
indices = indices[:5000]
data = [mnist.data.values[i] for i in indices]
target = [mnist.target[i] for i in indices]
bestClf = None
bestScore = 0
bestK = 0
scores = []
kvalues = [i for i in range(2,16)]
train_sizes = [0.05*i for i in range(1,20)]
pvalues = [i for i in range(1,11)]
k = 3
t = 0.9
start = time.time()
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=t)
clf = KNeighborsClassifier(3, n_jobs = 1)
clf.fit(xtrain, ytrain)
score = clf.score(xtest, ytest)
end = time.time()
print(f"n_jobs = 1, training + evaluating time : {end - start}")
start = time.time()
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=t)
clf = KNeighborsClassifier(3, n_jobs = -1)
clf.fit(xtrain, ytrain)
score = clf.score(xtest, ytest)
end = time.time()
print(f"n_jobs = -1, training + evaluating time : {end - start}")
"""
#for k in kvalues:
#for t in train_sizes:
for p in pvalues:
print(p)
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=t)
clf = KNeighborsClassifier(k, p = p)
clf.fit(xtrain, ytrain)
score = clf.score(xtest, ytest)
scores += [score]
if score > bestScore:
bestClf = clf
#bestK = k
#bestSize = t
bestP = p
bestScore = score
print(target[4])
print(bestClf.predict(data[4].reshape(1,-1)))
plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
plt.show()
#plt.plot(kvalues, scores)
#plt.plot(train_sizes, scores)
plt.plot(pvalues, scores)
print(bestScore)
#print(bestSize)
"""

119
tp2.py Звичайний файл

@ -0,0 +1,119 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.metrics import precision_score
import random
import time
"""
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
"""
"""
images = mnist.data.values.reshape((-1, 28, 28))
plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
print(mnist.target[0])
"""
indices = [i for i in range(len(mnist.data))]
random.shuffle(indices)
indices = indices[:15000]
data = [mnist.data.values[i] for i in indices]
target = [mnist.target[i] for i in indices]
bestClf = None
bestScore = 0
scores = []
train_sizes = [0.05*i for i in range(1,20)]
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
"""
network = MLPClassifier(hidden_layer_sizes = (50))
network.fit(xtrain, ytrain)
ytest_pred = clf.predict(xtest)
print("Average = macro:")
print(precision_score(ytest, ytest_pred, average = "macro"))
print("Average = None:")
print(precision_score(ytest, ytest_pred, average = None))
print(target[4])
print(network.predict(data[4].reshape(1,-1)))
plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
plt.show()
"""
"""
n_layers = [i for i in range(1,51)]
for n in n_layers:
print(n)
network = MLPClassifier(hidden_layer_sizes = (50,)*n)
network.fit(xtrain, ytrain)
scores += [network.score(xtest, ytest)]
plt.plot(n_layers, scores)
"""
"""
for _ in range(5):
n_layers = random.randint(1,3)
layer_size = tuple([random.randint(10,300) for _ in range(n_layers)])
print(layer_size)
network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu")
network.fit(xtrain, ytrain)
print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
"""
"""
solvers = ["lbfgs", "sgd", "adam"]
for s in solvers:
print(s + ":")
start_time = time.time()
network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s)
network.fit(xtrain, ytrain)
print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
"""
"""
functions = ["identity", "logistic", "tanh", "relu"]
for f in functions:
print(f + ":")
start_time = time.time()
network = MLPClassifier(hidden_layer_sizes = (50,)*5, activation = f)
network.fit(xtrain, ytrain)
print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
"""
times = []
scores = []
for i in range(-7,7):
alpha = 10**i
print(f"alpha = {alpha}:")
start_time = time.time()
network = MLPClassifier(hidden_layer_sizes = (50,)*5, alpha = alpha)
network.fit(xtrain, ytrain)
trainingTime = time.time() - start_time
score = network.score(xtest, ytest)
times +=[trainingTime]
scores += [score]
plt.plot([i for i in range(-7,7)], times)
plt.show()
plt.plot([i for i in range(-7,7)], scores)

94
tp3.py Звичайний файл

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.metrics import precision_score
import random
import time
"""
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
"""
"""
---Ex 1---
images = mnist.data.values.reshape((-1, 28, 28))
plt.imshow(images[0],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
print(mnist.target[0])
"""
"""
---Ex 2---
"""
indices = [i for i in range(len(mnist.data))]
random.shuffle(indices)
indices = indices[:15000]
data = [mnist.data.values[i] for i in indices]
target = [mnist.target[i] for i in indices]
bestClf = None
bestScore = 0
scores = []
train_sizes = [0.05*i for i in range(1,20)]
xtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size=0.7)
"""
network = MLPClassifier(hidden_layer_sizes = (50))
network.fit(xtrain, ytrain)
ytest_pred = clf.predict(xtest)
print("Average = macro:")
print(precision_score(ytest, ytest_pred, average = "macro"))
print("Average = None:")
print(precision_score(ytest, ytest_pred, average = None))
print(target[4])
print(network.predict(data[4].reshape(1,-1)))
plt.imshow(data[4].reshape(28, 28), cmap=plt.cm.gray_r, interpolation="nearest")
plt.show()
"""
"""
n_layers = [i for i in range(1,51)]
for n in n_layers:
print(n)
network = MLPClassifier(hidden_layer_sizes = (50,)*n)
network.fit(xtrain, ytrain)
scores += [network.score(xtest, ytest)]
plt.plot(n_layers, scores)
"""
"""
for _ in range(5):
n_layers = random.randint(1,3)
layer_size = tuple([random.randint(10,300) for _ in range(n_layers)])
print(layer_size)
network = MLPClassifier(hidden_layer_sizes = layer_size, solver = "adam", activation = "relu")
network.fit(xtrain, ytrain)
print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
"""
"""
solvers = ["lbfgs", "sgd", "adam"]
for s in solvers:
print(s + ":")
start_time = time.time()
network = MLPClassifier(hidden_layer_sizes = (50,)*5, solver = s)
network.fit(xtrain, ytrain)
print(f"training time = {time.time() - start_time}, score = {network.score(xtest, ytest)}")
"""