# Week4

## Linear regression

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

## Polynomial regression

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

## Clustering: kmeans/kmeans2

```python
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: <https://www.math.muni.cz/~kolacek/docs/frvs/M7222/data/AutoInsurSweden.txt>

```python
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.

url: <https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data>

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zh-1-peng.gitbook.io/data-prog-with-python/week4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
