Data Prog with Python
  • Introduction
  • Week2
  • Week3
  • Week4
  • Week5
  • Week6
  • Week7
  • Week8
  • Week9
  • Week10
  • project1
  • project2
  • project3
  • Useful codes for exam
Powered by GitBook
On this page
  • Linear regression
  • Polynomial regression
  • Clustering: kmeans/kmeans2
  • Exercise
  • 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.
  • 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.

Was this helpful?

Week4

PreviousWeek3NextWeek5

Last updated 5 years ago

Was this helpful?

Linear regression

gradient,intercept,r_value, p_value, 
stderr=scipy.stats.linregress(x,y)

Polynomial regression

np.polynomial.polynomial.polyfit
c,stats=nppp.polyfit(x,y,degree,full=True, w=None)
nppp.polyval(datasample,c)

Clustering: kmeans/kmeans2

centroids,var=scipy.cluster.vq.kmeans(Dataset, Number_of_Clusters)
id,dis=scipy.cluster.vq.vq(Dataset, centroids)
np.random.multivariate_normal([10,0],[[3,1],[1,4]]),size=[100,1])

Exercise

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()

url:

https://www.math.muni.cz/~kolacek/docs/frvs/M7222/data/AutoInsurSweden.txt
https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data