1-. Import the ‘Auto Insurance in Sweden’ dataset from the following url, and do a linear regression to fit the data. Plot the data and the regression line.
url:
import pandas as pd
import numpy as np
import numpy.polynomial.polynomial as nppp
import scipy.stats as sps
from pylab import plot,show
DataFrame = pd.read_csv('C:/Users/MA/Desktop/Insurance.csv',header=None)
DataMatrix = DataFrame.as_matrix()
InputMatrix= np.array(DataMatrix[:,0])
OutMatrix = np.array(DataMatrix[:,1])
(gradient,intercept,rvalue,pvalue,stderr) = sps.linregress(InputMatrix,OutMatrix)
Regression_line = nppp.polyval(InputMatrix,[intercept,gradient])
print ("Gradient & Intercept", gradient, intercept)
plot(InputMatrix,OutMatrix, 'vr')
plot(InputMatrix,Regression_line ,'b.-')
show()
2 - Download the ‘IRIS’ dataset from the url below, import it to Python and do a 3-mean clustering based on the inputs (4-dimesnion). Plot the members of each cluster with different colour (Red, Blue & Green ) in a 2-axis coordinate which the horizontal axis is the first input and the vertical one is second input.
import pandas as pd
import numpy as np
from scipy.cluster.vq import kmeans,vq
from pylab import
DataFrame = pd.read_csv('C:/Users/MA/Desktop/iris.csv',header=None)
DataMatrix = DataFrame.as_matrix()
InputMatrix = np.matrix(DataMatrix[:,:4])
centroids,_ = kmeans(InputMatrix,3)
id,_ = vq(InputMatrix,centroids)
print(centroids)
print(id)
plot(InputMatrix[id==0,0],InputMatrix[id==0,1],'*b',InputMatrix[id==
1,0],InputMatrix[id==1,1],'vr',InputMatrix[id==2,0],InputMatrix[id==
2,1],'og',linewidth=5.5)
show()