import os
import tarfile
import urllib
import urllib.request
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/"
HOUSING_PATH = os.path.join("datasets", "housing") # 把目录和文件名合成一个路径
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/housing.tgz"
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH):
if not os.path.isdir(housing_path): # 判断路径是否为目录
os.makedirs(housing_path) # 递归创建目录
tgz_path = os.path.join(housing_path, "housing.tgz")
urllib.request.urlretrieve(housing_url, tgz_path) # 将URL检索到磁盘上的临时位置
housing_tgz = tarfile.open(tgz_path) # 打开
housing_tgz.extractall(path=housing_path) # 解压
housing_tgz.close() # 关闭
fetch_housing_data()
import pandas as pd
def load_housing_data(housing_path=HOUSING_PATH):
csv_path = os.path.join(housing_path, "housing.csv")
return pd.read_csv(csv_path) # 加载数据
housing = load_housing_data()
housing.head()
1 方差扩大因子法
概念
r_xy = housing.corr(method='pearson')
r_xy
features = list(housing) # 提取列名
features.remove('median_house_value')
features.remove('ocean_proximity') # 非数值类型
labels = ['median_house_value']
X = housing[features]
r_xx = X.corr(method='pearson')
r_xx
import seaborn as sns
sns.set() # 设置/重设美学参数
sns.heatmap(r_xx)
import numpy as np
C = np.linalg.inv(r_xx) # 逆
VIF = np.diag(C) # 找出矩阵的对角线元素
VIF.round(2)
2 考察矩阵 XTX 的特征值和条件数
概念
scaled = (X - X.mean()) / X.std() # 每列数据进行中心化&标椎化
A = np.array(scaled) # 将数据框转换成二维数组
B = np.dot(X.T, X) # 计算 X^T * X
ev, evct = np.linalg.eig(B) # 计算特征值和特征向量
kk = ev.max()/ev.min() # 计算 XTX 的条件数
ev # 特征值
ev.min() # 最小特征值
ev.argmin()
evct # 特征向量
evct[6] # 最小特征值的特征向量
kk # XTX 的条件数