Muhammad al-xorazmiy nomidagi toshkent axborot texnalogiyalari universiteti kiberxavfsizlik fakulteti


Download 231.75 Kb.
Sana21.11.2023
Hajmi231.75 Kb.
#1793283
Bog'liq
19 VARIAN


O’zbekiston Respublikasi Raqamli texnologiyalar vazirligi
MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNALOGIYALARI UNIVERSITETI
KIBERXAVFSIZLIK FAKULTETI




Mashinali o’qitishga kirish
Mustaqil ish


Guruh: 715-21
Bajarildi: Xayrullayev Akobirxon
Fan o’qituvchisi: Ochilov Mannon

Toshkent 2023



19-AMALIY MASHG’ULOT
MAVZU: YIRTQICH HAYVONLARNI SINFLASH

import numpy as np


import pandas as pd
import matplotlib.pyplot as plt
import cv2
import os
from random import shuffle
from tqdm import tqdm
from PIL import Image
import pandas as pd

import pandas as pd


import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
data = pd.read_csv(‘wildlife.csv’)
features = data.drop([‘Species’], axis=1)
targets = data[‘Species’]
seed = 42
np.random.seed(seed)
train_features, test_features, train_targets, test_targets = train_test_split(features, targets, test_size=0.3, random_state=seed)
knn = KNeighborsClassifier(n_neighbors=5, algorithm=‘auto’, metric=‘minkowski’, leaf_size=30, p=2, weights=‘uniform’)
knn.fit(train_features, np.ravel(train_targets))
predictions = knn.predict(test_features)
acc = np.average(predictions==test_targets)

clf.fit(X_train, y_train) #


# /content/drive/MyDrive/Room/images/images
from google.colab import drive
drive.mount('/content/drive')
Mounted at /content/drive
train_messy = "/content/drive/MyDrive/Room/images/images/train/messy"
train_clean= "/content/drive/MyDrive/Room/images/images/train/clean"

test_messy= "/content/drive/MyDrive/Room/images/images/val/messy"


test_clean= "/content/drive/MyDrive/Room/images/images/val/clean"

image_size = 128

I mage.open("/content/drive/MyDrive/Room/images/images/train/messy/76.png")
Image.open("/content/drive/MyDrive/Room/images/images/train/clean/76.png")

for image in tqdm(os.listdir(train_messy)):
path = os.path.join(train_messy, image)
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (image_size, image_size)).flatten()
np_img=np.asarray(img)

for image2 in tqdm(os.listdir(train_clean)):


path = os.path.join(train_clean, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (image_size, image_size)).flatten()
np_img2=np.asarray(img2)

plt.figure(figsize=(10,10))


plt.subplot(1, 2, 1)
plt.imshow(np_img.reshape(image_size, image_size))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np_img2.reshape(image_size, image_size))
plt.axis('off')
plt.title("Messy and Clean Rooms in GrayScale")
100%|██████████| 96/96 [01:15<00:00, 1.27it/s]
100%|██████████| 96/96 [01:12<00:00, 1.33it/s]
Text(0.5, 1.0, 'Messy and Clean Rooms in GrayScale')

def train_data():
train_data_messy = []
train_data_clean=[]
for image1 in tqdm(os.listdir(train_messy)):
path = os.path.join(train_messy, image)
img1 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img1 = cv2.resize(img1, (image_size, image_size))
train_data_messy.append(img1)
for image2 in tqdm(os.listdir(train_clean)):
path = os.path.join(train_clean, image)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (image_size, image_size))
train_data_clean.append(img2)

train_data= np.concatenate((np.asarray(train_data_messy),np.asarray(train_data_clean)),axis=0)


return train_data
def test_data():
test_data_messy = []
test_data_clean=[]
for image1 in tqdm(os.listdir(test_messy)):
path = os.path.join(test_messy, image1)
img1 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img1 = cv2.resize(img1, (image_size, image_size))
test_data_messy.append(img1)
for image2 in tqdm(os.listdir(test_clean)):
path = os.path.join(test_clean, image2)
img2 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (image_size, image_size))
test_data_clean.append(img2)

test_data= np.concatenate((np.asarray(test_data_messy),np.asarray(test_data_clean)),axis=0)


return test_data
train_data = train_data()
test_data = test_data()
100%|██████████| 96/96 [00:00<00:00, 162.29it/s]
100%|██████████| 96/96 [00:00<00:00, 161.09it/s]
100%|██████████| 10/10 [00:07<00:00, 1.29it/s]
100%|██████████| 10/10 [00:07<00:00, 1.30it/s]
train_data
array([[[219, 220, 228, ..., 145, 145, 145], [202, 201, 221, ..., 148, 149, 149], [190, 190, 201, ..., 149, 148, 143], ..., [ 43, 43, 45, ..., 19, 18, 18], [ 43, 43, 49, ..., 20, 22, 18], [ 44, 45, 53, ..., 18, 21, 17]], [[219, 220, 228, ..., 145, 145, 145], [202, 201, 221, ..., 148, 149, 149], [190, 190, 201, ..., 149, 148, 143], ..., [ 43, 43, 45, ..., 19, 18, 18], [ 43, 43, 49, ..., 20, 22, 18], [ 44, 45, 53, ..., 18, 21, 17]], [[219, 220, 228, ..., 145, 145, 145], [202, 201, 221, ..., 148, 149, 149], [190, 190, 201, ..., 149, 148, 143], ..., [ 43, 43, 45, ..., 19, 18, 18], [ 43, 43, 49, ..., 20, 22, 18], [ 44, 45, 53, ..., 18, 21, 17]], ..., [[171, 173, 173, ..., 131, 149, 154], [172, 173, 173, ..., 126, 157, 151], [173, 174, 175, ..., 52, 53, 67], ..., [ 47, 47, 64, ..., 109, 110, 107], [ 47, 46, 85, ..., 111, 98, 107], [ 48, 48, 97, ..., 107, 111, 117]], [[171, 173, 173, ..., 131, 149, 154], [172, 173, 173, ..., 126, 157, 151], [173, 174, 175, ..., 52, 53, 67], ..., [ 47, 47, 64, ..., 109, 110, 107], [ 47, 46, 85, ..., 111, 98, 107], [ 48, 48, 97, ..., 107, 111, 117]], [[171, 173, 173, ..., 131, 149, 154], [172, 173, 173, ..., 126, 157, 151], [173, 174, 175, ..., 52, 53, 67], ..., [ 47, 47, 64, ..., 109, 110, 107], [ 47, 46, 85, ..., 111, 98, 107], [ 48, 48, 97, ..., 107, 111, 117]]], dtype=uint8)
x_data=np.concatenate((train_data,test_data),axis=0)

norm_max=np.max(x_data)


norm_min=np.min(x_data)

x_data = (x_data-norm_min)/(norm_max-norm_min)


test_data.shape
(20, 128, 128)
z1 = np.zeros(96)
o1 = np.ones(96)
Y_train = np.concatenate((o1, z1), axis=0)
z = np.zeros(10)
o = np.ones(10)
Y_test = np.concatenate((o, z), axis=0)
Y_test
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
y_data=np.concatenate((Y_train,Y_test),axis=0).reshape(x_data.shape[0],1)
print("X shape: " , x_data.shape)
print("Y shape: " , y_data.shape)
X shape: (212, 128, 128)
Y shape: (212, 1)
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.15, random_state=42)

number_of_train = x_train.shape[0]


number_of_test = x_test.shape[0]
x_train_flatten = x_train.reshape(number_of_train,x_train.shape[1]*x_train.shape[2])
x_test_flatten = x_test .reshape(number_of_test,x_test.shape[1]*x_test.shape[2])

print("X train flatten",x_train_flatten.shape)


print("X test flatten",x_test_flatten.shape)
X train flatten (180, 16384)
X test flatten (32, 16384)
x_train = x_train_flatten.T
x_test = x_test_flatten.T

y_test = y_test.T


y_train = y_train.T

print("x train: ",x_train.shape)


print("x test: ",x_test.shape)
print("y train: ",y_train.shape)
print("y test: ",y_test.shape)
x train: (16384, 180)
x test: (16384, 32)
y train: (1, 180)
y test: (1, 32)
LOGISTIC REGRESSION
def initialize_weights_and_bias(dimension):
w = np.full((dimension,1),0.01)
b = 0.0
return w, b

def sigmoid(z):


y_head = 1/(1+np.exp(-z))
return y_head

def forward_backward_propagation(w,b,x_train,y_train):


# forward propagation
z = np.dot(w.T,x_train) + b
y_head = sigmoid(z)
loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)
cost = (np.sum(loss))/x_train.shape[1]
# backward propagation
derivative_weight = (np.dot(x_train,((y_head-y_train).T)))/x_train.shape[1]
derivative_bias = np.sum(y_head-y_train)/x_train.shape[1]
gradients = {"derivative_weight": derivative_weight,"derivative_bias": derivative_bias}
return cost,gradients
def update(w, b, x_train, y_train, learning_rate,number_of_iterarion):
cost_list = []
cost_list2 = []
index = []

for i in range(number_of_iterarion):


cost,gradients = forward_backward_propagation(w,b,x_train,y_train)


cost_list.append(cost)

w = w - learning_rate * gradients["derivative_weight"]


b = b - learning_rate * gradients["derivative_bias"]
if i % 100 == 0:
cost_list2.append(cost)
index.append(i)
print ("Cost after iteration %i: %f" %(i, cost))

parameters = {"weight": w,"bias": b}


plt.plot(index,cost_list2)
plt.xticks(index,rotation='vertical')
plt.xlabel("Number of Iterarion")
plt.ylabel("Cost")
plt.show()
return parameters, gradients, cost_list

def predict(w,b,x_test):


z = sigmoid(np.dot(w.T,x_test)+b)


Y_prediction = np.zeros((1,x_test.shape[1]))

for i in range(z.shape[1]):


if z[0,i]<= 0.5:
Y_prediction[0,i] = 0
else:
Y_prediction[0,i] = 1

return Y_prediction


def logistic_regression(x_train, y_train, x_test, y_test, learning_rate , num_iterations):


dimension = x_train.shape[0]


w,b = initialize_weights_and_bias(dimension)

parameters, gradients, cost_list = update(w, b, x_train, y_train, learning_rate,num_iterations)


y_prediction_test = predict(parameters["weight"],parameters["bias"],x_test)


y_prediction_train = predict(parameters["weight"],parameters["bias"],x_train)

print("Test Accuracy: {} %".format(round(100 - np.mean(np.abs(y_prediction_test - y_test)) * 100,2)))


print("Train Accuracy: {} %".format(round(100 - np.mean(np.abs(y_prediction_train - y_train)) * 100,2)))
logistic_regression(x_train, y_train, x_test, y_test,learning_rate = 0.01, num_iterations = 1500)
:14: RuntimeWarning: divide by zero encountered in log
loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)
:14: RuntimeWarning: invalid value encountered in multiply
loss = -y_train*np.log(y_head)-(1-y_train)*np.log(1-y_head)
Cost after iteration 0: nan
Cost after iteration 100: 0.023990
Cost after iteration 200: 0.013663
Cost after iteration 300: 0.009604
Cost after iteration 400: 0.007400
Cost after iteration 500: 0.006012
Cost after iteration 600: 0.005057
Cost after iteration 700: 0.004361
Cost after iteration 800: 0.003831
Cost after iteration 900: 0.003415
Cost after iteration 1000: 0.003079
Cost after iteration 1100: 0.002803
Cost after iteration 1200: 0.002571
Cost after iteration 1300: 0.002375
Cost after iteration 1400: 0.002206

Test Accuracy: 90.62 %
Train Accuracy: 100.0 %
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV

grid={"C":np.logspace(-3,3,7),"penalty":["l1","l2"]},


logistic_regression=LogisticRegression(random_state=42)


log_reg_cv=GridSearchCV(logistic_regression,grid,cv=10)


log_reg_cv.fit(x_train.T,y_train.T)


/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/model_selection/_validation.py:378: FitFailedWarning:
70 fits failed out of a total of 140.
The score on these train-test partitions for these parameters will be set to nan.
If these failures are not expected, you can try to debug them by setting error_score='raise'.

Below are more details about the failures:


--------------------------------------------------------------------------------
70 fits failed with the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/sklearn/model_selection/_validation.py", line 686, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "/usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py", line 1162, in fit
solver = _check_solver(self.solver, self.penalty, self.dual)
File "/usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py", line 54, in _check_solver
raise ValueError(
ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.

warnings.warn(some_fits_failed_message, FitFailedWarning)


/usr/local/lib/python3.10/dist-packages/sklearn/model_selection/_search.py:952: UserWarning: One or more of the test scores are non-finite: [ nan 0.96111111 nan 0.95 nan 0.94444444
nan 0.95555556 nan 0.95555556 nan 0.93888889
nan 0.94444444]
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
GridSearchCV
estimator: LogisticRegression
LogisticRegression
print("best hyperparameters: ", log_reg_cv.best_params_)
print("accuracy: ", log_reg_cv.best_score_)
best hyperparameters: {'C': 0.001, 'penalty': 'l2'}
accuracy: 0.961111111111111 log_reg= LogisticRegression(C=0.001,penalty="l2")

log_reg.fit(x_train.T,y_train.T)


/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
LogisticRegression
LogisticRegression(C=0.001)
print("test accuracy: {} ".format(log_reg.fit(x_test.T, y_test.T).score(x_test.T, y_test.T)))
print("train accuracy: {} ".format(log_reg.fit(x_train.T, y_train.T).score(x_train.T, y_train.T)))
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
/usr/local/lib/python3.10/dist-packages/sklearn/utils/validation.py:1143: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
y = column_or_1d(y, warn=True)
test accuracy: 0.96875
train accuracy: 0.9666666666666667
[ ]
log_reg.predict(x_test.T[0].reshape(1,-1))
account_circle
array([1.])
[ ]
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
y_pred=log_reg.predict(x_train.T)
result = confusion_matrix(y_train[0], y_pred)
print(result)
account_circle
[[87 1]
[ 5 87]]
[ ]
new_test_path="/content/drive/MyDrive/Room/images/images/test/8.png"
Image.open(new_test_path)
account_circle
[ ]
test_img = cv2.imread(new_test_path, cv2.IMREAD_GRAYSCALE)
test_img = cv2.resize(test_img, (image_size, image_size))

test_img_1 = (test_img-norm_min)/(norm_max-norm_min)



log_reg.predict(test_img_1.reshape(1,image_size*image_size))
account_circle
array([1.])
[ ]
from sklearn.neighbors import KNeighborsClassifier
[ ]
classifier = KNeighborsClassifier(n_neighbors = 127)
classifier.fit(x_train.T,y_train.T)
y_pred=classifier.predict(x_test.T)
result = confusion_matrix(y_test[0], y_pred)
print(result)

account_circle
[[18 0]
[ 1 13]]
/usr/local/lib/python3.10/dist-packages/sklearn/neighbors/_classification.py:215: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples,), for example using ravel().
return self._fit(X, y)
Download 231.75 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling