---
# System prepended metadata

title: "關於我那~~搞錯的~~AI課\nCh.1 hello world+bstn homework"

---

關於我那~~搞錯的~~AI課
Ch.1 hello world+bstn homework 
===

原文:https://hackmd.io/@NeoDoggy/BJnrzvUQu

[ToC]

##### 21/3/6 UPD
更新了浮點數問題也解決了NOX(nan,nan)的問題
```python=20
y=y.astype(np.float64)
```
```python=
from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
from scipy import stats
import seaborn as sns 
import matplotlib.pyplot as plt

def dopear(x,y):
    return(stats.pearsonr(x,y))

def test(y):
    for x in y:
        print(x)

name=["CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT"]
bstn=load_boston()
bsdata=bstn.data
#test(bsdata)
y=np.arange(506)
y=y.astype(np.float64)
strpos=0
for i in range(0,13):
    pos=0
    for x in bsdata:
        y[pos]=x[i]
        pos+=1
    ans=dopear(y,bstn.target)
    print(name[strpos],":",dopear(y,bstn.target))
    plt.scatter(y,bstn.target)
    plt.title(name[strpos])
    plt.show()
    strpos+=1
```
重新輸出結果：
```
CRIM : (-0.3883046085868116, 1.1739870821941207e-19)
ZN : (0.3604453424505427, 5.713584153079929e-17)
INDUS : (-0.4837251600283726, 4.90025998175163e-31)
CHAS : (0.17526017719029835, 7.39062317051802e-05)
NOX : (-0.4273207723732826, 7.065041586251507e-24)
RM : (0.695359947071539, 2.487228871007725e-74)
AGE : (-0.3769545650045961, 1.569982209187648e-18)
DIS : (0.24992873408590394, 1.2066117273368359e-08)
RAD : (-0.3816262306397779, 5.465932569646693e-19)
TAX : (-0.4685359335677675, 5.637733627688442e-29)
PTRATIO : (-0.5077866855375621, 1.6095094784724266e-34)
B : (0.3334608196570665, 1.3181127340751667e-14)
LSTAT : (-0.7376627261740143, 5.08110339438928e-88)
```
前五項仍然沒變
順便學習了matplotlib.pyplot中的圖表語法
並將其結合XLaunch輸出
解決了要上網輸出圖形的困擾
1. LSTAT -0.7376627261740143
![](https://i.imgur.com/WKKy9kB.png)


2. RM 0.695359947071539
![](https://i.imgur.com/mjGp0BB.png)


3. PTRATIO -0.5077866855375621
![](https://i.imgur.com/78414l1.png)


4. INDUS -0.4837251600283726
![](https://i.imgur.com/EwHConS.png)


5. TAX -0.4685359335677675
![](https://i.imgur.com/WH1xfL8.png)

---
##### 21/3/5
做法：
將波士頓房價相關係數導入python後將其一個個篩出來做pearson
完整程式碼如下
```python=1
from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
from scipy import stats

def dopear(x,y):
    return(stats.pearsonr(x,y))

def test(y):
    for x in y:
        print(x)

name=["CRIM","ZN","INDUS","CHAS","NOX","RM","AGE","DIS","RAD","TAX","PTRATIO","B","LSTAT"]
bstn=load_boston()
bsdata=bstn.data
#test(bsdata)
y=np.arange(506)
strpos=0
for i in range(0,13):
    pos=0
    for x in bsdata:
        y[pos]=x[i]
        pos+=1
    print(name[strpos],":",dopear(y,bstn.target))
    strpos+=1

```
之後在wsl運行將其輸出成檔案bstn.out
```
python3 bstn.py > bstn.out
```
輸出結果如下
```
CRIM : (-0.3848416033917936, 2.6179514699122047e-19)
ZN : (0.3605803600433967, 5.552706329816316e-17)
INDUS : (-0.48921724930603355, 8.303855716927617e-32)
CHAS : (0.17526017719029835, 7.39062317051802e-05)
NOX : (nan, nan)
RM : (0.6216919448193313, 1.92618132755546e-55)
AGE : (-0.37721717375340386, 1.4802394780025958e-18)
DIS : (0.2615333692067058, 2.332662582373424e-09)
RAD : (-0.3816262306397779, 5.465932569646693e-19)
TAX : (-0.4685359335677675, 5.637733627688442e-29)
PTRATIO : (-0.5063983052129798, 2.6020225735178517e-34)
B : (0.33379122141773765, 1.2370491438230797e-14)
LSTAT : (-0.7395048644619826, 1.1210549265969188e-88)
```
由相關係數推得前五項較為關係者為
1. LSTAT -0.7395048644619826
![](https://i.imgur.com/94ZM0M3.png)

2. RM 0.6216919448193313
![](https://i.imgur.com/A2sFQCS.png)

3. PTRATIO -0.5063983052129798
![](https://i.imgur.com/t1dV543.png)

4. INDUS -0.48921724930603355
![](https://i.imgur.com/vjlyZwd.png)

5. TAX -0.4685359335677675
![](https://i.imgur.com/Zt7U2Ju.png)

關於如何輸出圖片：
```python=
if i==x:
    qwq=0
    for w in y:
        print("(",w,",",bstn.target[qwq],")",sep = '')
        qwq+=1
```
然後使用
https://www.mathsisfun.com/data/correlation-calculator.html

###### tags: ai