# Start writing code here...
from scipy import misc
img=misc.face()
import matplotlib.pyplot as plt
plt.imshow(img)
plt.show()
imred=img.copy()
imred[:,:,1:]=0
imred[55,0]
plt.imshow(imred)
plt.show()
img.shape
#define linear relation like y=kx+random noise
#save the data set using panda
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x=5*np.random.rand(100)*2
y=15-4*x+np.random.rand(100)*2
plt.plot(x,y,"r.")
plt.axis([0,5,0,20])
plt.show()
df=pd.DataFrame
from sklearn.linear_model import LinearRegression
lin_reg=LinearRegression()
lin_reg.fit(x.reshape(-1,1),y)
lin_reg.intercept_+lin_reg.coef_
plt.plot(x,y,"r.")
plt.axis([0,5,0,20])
plt.plot(x,lin_reg.intercept_+lin_reg.coef_*x,"b-")
plt.show()
from sklearn.metrics import mean_squared_error as mse
y_pred=lin_reg.intercept_+lin_reg.coef_*x
mse(y,y_pred)
from sklearn.metrics import r2_score as r2
r2(y, y_pred)
lin_reg.score(x.reshape(-1,1),y)
#exercises numpy (as yesterday when I wasn't aroud)
arr = np.array((1, 2, 3, 4, 5))
print(arr)
v = np.linspace(10,-10,100)
print(v)
#exercise 3
v1 = np.linspace(0,20)
print(v1)
len(v)
v1 = np.random.randint(-10,10,4)
print('v1 = ')
print(v1)
v2 = np.random.randint(-10,10,4)
print('v2 = ')
print(v2)
r = v1*v2
print('r = ')
print(r)
M= np.arange(1,10).reshape((3, 3))
print(M)
M1 = np.random.random((3,3,3))
print(M1)
x = np.random.randn(10)
print('x = ')
print(x)
print('sum of all elements:')
print(np.sum(x))
print("Sum of each column:")
print(np.sum(x, axis=0))
print("Sum of each row:")
print(np.sum(x, axis=1))
A = np.arange(1,10).reshape((3,3))
print(A)
#chess exrcise correction
a = np.zeros(64,dtype='int64').reshape(8,8)
a[1::2,1::2]=1
print(a)
#correction exercise 3
a=np.arange(21)
a[9:16]=-a[9:16]
print(a)
#correction 6 identity matrix
a=np.identity(3)
print(a)
#7 4*4 matrix with 0 in the middle and 1 outside
abs(np.identity(4)-1).astype(int)
#7 correction exercise with summing
a=np.arange(9).reshape(3,3)
print(a)
print(a.sum(axis=0))
print(a.sum(axis=1))
#correction 8
a=np.arange(1,10).reshape(3,3)
print(a)
a[2,0:2]
#correction 9
a=np.arange(100)
def closest(arr,scalar):
return arr[abs(arr-scalar).argmin]
arr-scalar (in this case = a-34.9)
abs(a-34.9).argmin()
closest(a,34.9)
data=np.array([[-1.26356584, 7.16376914, 6.65286967],
[-0.79056614, 5.05922807, 5.64702233],
[ 0.9660033 , 7.83589409, 5.96605105],
[-4.0160404 , 21.32977635, 21.69702124],
[-1.90358345, 9.73677094, 8.75134229],
[ 1.41069079, 8.51979406, 7.06018639],
[-4.15750907, 23.57576646, 22.89407545],
[-0.95710097, 8.88593669, 5.94832755],
[-3.72926405, 20.08672219, 19.39756749],
[-1.36434358, 6.7754983 , 6.92703833],
[-3.7457475 , 17.27308928, 19.52512407],
[ 1.92613303, 10.87157562, 8.84074446],
[-1.56563311, 6.30979793, 7.53759814],
[ 0.76638661, 9.28079606, 5.60804912],
[ 2.8330194 , 15.00493077, 13.30886972],
[-0.2603251 , 5.89893621, 5.07015763],
[-0.67384821, 8.67347897, 5.47007485],
[-2.25400635, 11.87185912, 10.25960495],
[-0.0376848 , 2.78301217, 5.0014702 ],
[ 2.64698034, 13.66193193, 12.25344436],
[ 0.14766321, 6.53968706, 5.02257291],
[ 1.76918496, 10.38462665, 8.24033067],
[ 0.67491 , 3.99177717, 5.47155742],
[ 4.26052585, 21.89589366, 23.79183816],
[-1.75961512, 6.62245674, 8.20537042],
[-0.36331332, 3.99068072, 5.13664869],
[-2.57934635, 11.06156009, 11.88750898],
[-1.02561026, 4.22509033, 6.08894907],
[-1.02165089, 5.06891893, 6.08055753],
[ 0.91152627, 9.42137172, 5.86016393],
[ 0.58752794, 7.86272759, 5.35735503],
[-1.94426636, 9.75354974, 8.91340122],
[-1.36886087, 4.8466919 , 6.93982017],
[-0.95503157, 5.14047135, 5.94423112],
[-0.51338211, 1.71524226, 5.27285023],
[ 0.53337204, 2.8593825 , 5.29451224],
[-0.36798269, 5.16786643, 5.14018374],
[-3.32224244, 15.90474381, 16.42629667],
[ 1.19857916, 5.69001364, 6.4872237 ],
[ 1.64779036, 6.62328328, 7.81090887],
[-2.12506371, 9.54200275, 9.67505541],
[-3.94864545, 22.34174753, 21.14132316],
[-0.67638817, 5.90909652, 5.47362527],
[ 1.49796204, 6.59235796, 7.32297464],
[ 0.67307103, 5.66239422, 5.46899115],
[ 1.03725155, 5.28763312, 6.1138098 ],
[-0.69795809, 1.49385788, 5.5043146 ],
[ 0.16421034, 5.28917845, 5.0279154 ],
[ 0.3590795 , 5.76577031, 5.13348242],
[-0.59050754, 7.96916922, 5.36098882]])
x=data[:,0]
print(x)
y=data[:,1]
y_pred=data[:,2]
plt.plot(x,y,"r.")
plt.show()
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score as R2
from sklearn.metrics import mean_squared_error as MSE
MSE(y,y_pred)
R2(y,y_pred)
y_pred=data[:,2]
plt.plot(x,y,'r.',x,y_pred,'g.')
plt.show()
import pandas as pd
from sklearn.datasets import load_iris
ds=load_iris()
print(ds.DESCR)
ds.feature_names
ds.data
df = pd.DataFrame
print(df)
shape = df.shape
print(shape)
ds.target
len(ds.target)
import numpy as np