Lab-06 (1)

pdf

School

University of North Texas *

*We aren’t endorsed by this school

Course

5502

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

5

Uploaded by venkatasai1999

Report
1. Create a list with the names called image_files The following list has been made for you to simplify this. Note, in practice you would likely store these files in separate folders and simply read all the files in each folder so that you could add/remove files without editing source code. 2. Create the scatter plot in the first page In [1]: import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np from PIL import Image In [2]: img_files = [ 'farm1.jpg' , 'farm2.jpg' , 'farm3.jpg' , 'farm4.jpg' , 'farm5.jpg' , 'farm6.jpg' , 'farm7.jpg' , 'farm8.jpg' , 'city1.jpg' , 'city2.jpg' , 'city3.jpg' , 'city4.jpg' , 'city5.jpg' , 'city6.jpg' , 'city7.jpg' , 'city8.jpg' , 'desert1.jpg' , 'desert2.jpg' , 'desert3.jpg' , 'desert4.jpg' , 'desert5.jpg' , 'desert6.jpg' , 'desert7.jpg' , 'desert8.jpg' ] In [3]: percent_green = [] percent_blue = [] for i in img_files : #Reads the image of a sunset into an array img = mpimg . imread ( 'images2/' + i ) RGBtuple = np . array ( img ) . mean ( axis = ( 0 , 1 )) averageRed , averageGreen , averageBlue = RGBtuple [ 0 ], RGBtuple [ 1 ], RGBtuple [ 2 ] percentageGreen = averageGreen / ( averageRed + averageGreen + averageBlue ) percentageBlue = averageBlue / ( averageRed + averageGreen + averageBlue ) percent_green . append ( percentageGreen ) percent_blue . append ( percentageBlue ) In [4]: plt . xlabel ( 'percent of green' ) plt . ylabel ( 'percent of blue' ) plt . title ( 'Images of Farm, City and Desert' ) plt . scatter ( percent_green [ 0 : 8 ], percent_blue [ 0 : 8 ], s = 100 , color = "green" ) plt . scatter ( percent_green [ 8 : 16 ], percent_blue [ 8 : 16 ], s = 100 , color = "blue" ) plt . scatter ( percent_green [ 16 : 24 ], percent_blue [ 16 : 24 ], s = 100 , color = "brown" ) plt . gcf () . set_size_inches (( 10 , 5 )) plt . legend ([ "Farm" , "City" , "Desert" ], loc = "upper right" ) plt . show ()
3. Now create an array of strings called training_target with the category of each. 4. Create an empty array of zeros called training_data that will eventually store the percent green and percent blue values. You will be filling this soon. Given the needs of your data set (24 samples and 2 columns), it should have 24 rows and 2 columns. In [5]: training_target = [ 'farm' , 'farm' , 'farm' , 'farm' , 'farm' , 'farm' , 'farm' , 'farm' , 'city' , 'city' , 'city' , 'city' , 'city' , 'city' , 'city' , 'city' , 'desert' , 'desert' , 'desert' , 'desert' , 'desert' , 'desert' , 'desert' , 'desert' ] In [6]: training_data = np . zeros ( shape = ( 24 , 2 )) training_data
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.]]) 5. Now fill the training_data array with the proper values for each image, and observe the values in the array after it is finished. You already have the percent of blue and the percent of green for all images from step 1. Make sure to put those two values in the proper place in the training_data array. Out[6]: In [7]: for i in range ( len ( training_data )): training_data [ i ][ 0 ] = percent_green [ i ] training_data [ i ][ 1 ] = percent_blue [ i ] In [8]: training_data
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
array([[0.38537916, 0.27250258], [0.38947877, 0.2416675 ], [0.37176749, 0.2923693 ], [0.38534941, 0.25567274], [0.38368854, 0.26974449], [0.37822351, 0.34243724], [0.35577841, 0.26138973], [0.36318264, 0.32079251], [0.33384679, 0.33987008], [0.31457989, 0.31740955], [0.32982159, 0.30761097], [0.33021422, 0.40329483], [0.31267745, 0.37068047], [0.3620055 , 0.35922372], [0.33263931, 0.28122414], [0.33155648, 0.31387494], [0.28899154, 0.26478622], [0.32887465, 0.29461288], [0.32171351, 0.24749944], [0.35209261, 0.23171261], [0.32718513, 0.21564911], [0.33655681, 0.2638719 ], [0.34419192, 0.13749538], [0.32732039, 0.26438328]]) 6. Create your classifier. This can often be done in one line. In this case, we suggest using the k-Nearest Neighbors classifier as shown in the tutorial (use k=1), but you can try others if you are interested. 7. Train your classifier. 8. Now create an empty test_data array and fill it with the proper values for each test image, and observe the filled array and consider if it matches your expectations based on your observations of the images Out[8]: In [9]: from sklearn import neighbors #k-NN classifier for k=1 #By using ‘distance’, closer neighbors will have greater weight #than further ones k1 = neighbors . KNeighborsClassifier ( n_neighbors = 1 , weights = 'distance' ) In [10]: k1 . fit ( training_data , training_target ) Out[10]: In [11]: test = [ "test1.jpg" , "test2.jpg" , "test3.jpg" ] percent_green_test = [] percent_blue_test = [] for i in test : img = mpimg . imread ( 'images2/' + i ) RGBtuple = np . array ( img ) . mean ( axis = ( 0 , 1 )) averageRed = RGBtuple [ 0 ] KNeighborsClassifier KNeighborsClassifier(n_neighbors=1, weights='distance')
array([[0.3269592 , 0.32688513], [0.33429384, 0.17936789], [0.35004008, 0.24578861]]) 9. Predict the class of the test images. Now predict the classes given the test_data array. This should only take one line of code if the test_data array is prepared. 10. Print the prediction from the test images compare with the actual images shown below. Make this comparison clear in the output of your code (e.g.prepend with ‘predicted:’ and ‘actual:’). Actual : ['city', 'desert', 'farm'] Predicted : ['city' 'desert' 'desert'] Accuracy : 66.66666666666666 averageGreen = RGBtuple [ 1 ] averageBlue = RGBtuple [ 2 ] percentageGreen = averageGreen / ( averageRed + averageGreen + averageBlue ) percentageBlue = averageBlue / ( averageRed + averageGreen + averageBlue ) percent_green_test . append ( percentageGreen ) percent_blue_test . append ( percentageBlue ) In [12]: test_targets = [ 'city' , 'desert' , 'farm' ] test_data = np . zeros ( shape = ( 3 , 2 )) In [13]: for i in range ( len ( test_data )): test_data [ i ][ 0 ] = percent_green_test [ i ] test_data [ i ][ 1 ] = percent_blue_test [ i ] In [14]: test_data Out[14]: In [15]: k1_pred = k1 . predict ( test_data ) In [16]: print ( "Actual : " , test_targets ) print ( "Predicted : " , k1_pred ) In [17]: from sklearn import metrics from sklearn.metrics import accuracy_score accuracy_k1 = accuracy_score ( test_targets , k1_pred ) print ( "Accuracy : " , accuracy_k1 * 100 ) In [ ]: