CS256
Chris Pollett
Sep 29, 2021
a = np.array([1, 2, 3], int) a.tolist() # [1, 2, 3] list(a) # [1, 2, 3] # same as above tuple(a) # (1, 2, 3) a.tofile("bob.txt") #writes to the file bob.txt c = a.tobytes() # '\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00 # \x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
b = np.fromfile("bob.txt",int) d = np.frombuffer(c)
pip install pillow
from PIL import Image
im = Image.open("myphoto.jpg") #Pillow supports many formats #png, ppm, gif, tiff, etc im.show() #this would display the image in a new window
im = Image.new('RGBA', (100, 100), (255,255,255,255)) #100x100 size image with white opaque background im.show();
from PIL import Image, ImageGrab im = ImageGrab.grab() # capture whole screen im.show() im = ImageGrab.grab((10,10,250,250)) #captures from (10,10) to (250,250) of screen im.show()
out_im = im.resize((50,200)) #out_im is a rescaled image of size 50x 200 #can also specify as a second argument how to resample from: #Image.NEAREST , BILINEAR, BICUBIC, LANCZOS
from PIL import Image, ImageFilter im = Image.open("myphoto.jpg") out_im = im.rotate(30) out_im = im.transpose(Image.FLIP_LEFT_RIGHT) #other possibilities are # FLIP_TOP_BOTTOM, ROTATE_90, ROTATE_180, ROTATE_270 out_im = im.filter(ImageFilter.BLUR) #other possibilities are: # CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, # FIND_EDGES, SMOOTH, SMOOTH_MORE, and SHARPEN # MinFilter(some_num), MaxFilter(some_num), MedianFilter(some_num), # ModeFilter(some_num), Kernel(size, kernel, scale=None, offset=0) # RankFilter(size, rank)
box = (20, 20, 80, 80) out_im = im.crop(box) #new image given by rectangle with top left (20,20) #bottom left (80, 80) box2 = (0, 0, 60, 60) im.paste(out_im, box2) # paste image back into old image at the given coords #rectangle in box2 has to be of same size as dimension of image
im1 = Image.open("myphoto1.jpg") im2 = Image.open("myphoto2.jpg") q = 0.5 out = Image.blend(im1, im2, q) #adds images to make output image #with weight 1-q to im1 and q to im2.
r, g, b, a = 0, 1, 2, 3 source = im1.split(); # creates 4 images one for each color component
blue_mask = source[b].point(lambda i: i > 50 and 255) # if i > 50 output 255; otherwise 0 mod_green = source[g].point(lambda i: i * 0.4) source[g].paste(mod_green, None, blue_mask) #mod_green is pasted back only where mask says #None component could have been a rectangle to restrict paste to
im1 = Image.merge(im1.mode, source)
from PIL import Image, ImageDraw, ImageFont im = Image.new('RGB', (100, 100), (0,0,0)) draw = ImageDraw.Draw(im) draw.point(((5, 5), (85, 5), (85, 6)), fill=(255, 0, 0)) #draw three red points draw.line((10,15,80,90), fill=(255,255,255)) #draw a white line draw.ellipse((30,30,40,50), fill=(0,0,255)) # draw a blue ellipse draw.text((20, 55), "hello world", font=ImageFont.load_default()) del draw im.show()
im = Image.new('RGB', (100, 100), (0,0,0)) draw = ImageDraw.Draw(im) draw.point((5, 5), fill=(255, 0, 0)) del draw im.getpixel((5,5)) # (255, 0, 0)
list(im.getdata()) # Since im was in rgb, this will output a # sequence of triples [(0, 0, 0), (0, 0, 0), ... import numpy as np im_array = np.frombuffer(im.tobytes(), dtype=np.uint8) #1-dimensional im_array.reshape((im.size[1], im.size[0], 3)) # make into 3d array where 3rd dimension color
im.save("out_image.png", "PNG") #so you can choose image file format when save
What kind of hypothesis we have is dependent on the things we can actually measure with an NN experiment. On the next few slides I give some examples of the kinds of things we can measure. After this, I give some example hypothesis which could be tested using these kind of experiments.
TPR | FPR |
FNR | TNR |
Observed Label | ||||
---|---|---|---|---|
A | B | C | ||
Predicted Label | A | 5 | 1 | 2 |
B | 3 | 7 | 4 | |
C | 0 | 2 | 9 |
pip install matplotlib
import matplotlib.pyplot as plt import numpy as np a = np.arange(0, 10,.5, dtype=float); b = a * a plt.title("The function y=x^2") plt.xlabel("x-axis") plt.ylabel("y-axis") plt.plot(a,b) plt.show()
import matplotlib.pyplot as plt import numpy as np a = np.arange(0, 10,.5, dtype=float); plt.title("Growth rates y=x, y=x^2, and y=x^3") plt.xlabel("x-axis") plt.ylabel("y-axis") id_line, = plt.plot(a,a, color="blue", label="y=x", linestyle='dashed', linewidth=2) quad_line, = plt.plot(a, a**2, color="red", label="y=x^2", linestyle='dotted') cube_line, = plt.plot(a, a**3, color="green", label="y=x^2", linestyle='dashdot') plt.legend(handles=[id_line, quad_line, cube_line], loc=2) #loc can be a number 1-4, number represents #which corner plt.show()
import matplotlib.pyplot as plt import numpy as np a = np.arange(0, 10,.5, dtype=float); plt.title("Various polynomials") plt.xlabel("x-axis") plt.ylabel("y-axis") plt.scatter(a, a**2) plt.scatter(a, a**3, marker="+", color="green"); plt.scatter(a, a**4, 200, marker="o"); # 200 is size in pixels plt.show()
import matplotlib.pyplot as plt import numpy as np a = np.array([1,1,2,2,2,2,3,4,4,4,4, 5,5,5,5,6,6,7,8,8,9,9,9,10,10,10]) plt.hist(a, bins=5) plt.show();
import matplotlib.pyplot as plt import numpy as np plt.title("Product Comparison") plt.bar(["Product A", "Product B"], [50, 100]) plt.show();
import matplotlib.pyplot as plt import numpy as np plt.title("Marketshare Comparison") companies = ["Company A", "Company B", "Company C"] shares = [20, 50, 30] colors = ['red', 'green', 'blue'] plt.pie(shares, labels=companies, colors=colors, startangle=100) plt.show();
import matplotlib.pyplot as plt plt.title("Product Comparison") plt.bar(["Product A", "Product B"], [50, 100]) plt.draw(); plt.savefig("product_comparison.png");