《封号码罗》数据分析与人工智能之sklearn模型Lasso岭回归(十二)

第一部分

# 套索回归
import time
from sklearn.linear_model import Lasso  # 导入套索回归模型

# Lasso是一个估计稀疏系数的线性模型
# 方程式 (1 / (2 * n_samples)) * ||y - Xw||^2_2 + alpha * ||w||_1
# 一阶正则项,系数变小,防止过拟合
start = time.perf_counter()

end = time.perf_counter()
print(f"程序耗时为{(end - start).__round__(20)}")

第二部分

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso

# 生成数据
# 样本量少于特征量,50个样本,200个特征
X = np.random.randn(50, 200)
# print(X)
# Xw = y
w = np.random.randn(200)
# 随机选取190个,让其为0
index = np.arange(200)  # 有顺序的
np.random.shuffle(index)  # shuffle打乱顺序
# print(index)
w[index[0:190]] = 0  # 一个以索引组成的列表
# print(w)
y = X.dot(w)  # y是目标值 长度为(50,)
# print(y)

lr = LinearRegression(fit_intercept=False)  # 数据和目标值是接近线性关系
ridge = Ridge(alpha=1, fit_intercept=False)  # 将系数缩减,防止过拟合,优化了线性回归
lasso = Lasso(alpha=0.2, fit_intercept=False)  # 稀疏矩阵时用套索合适,大部分是0,小部分有数据

lr.fit(X, y)
ridge.fit(X, y)
lasso.fit(X, y)

lr_w = lr.coef_  # 线性回归求的系数
ridge_w = ridge.coef_
lasso_w = lasso.coef_

# 四个子视图
plt.figure(figsize=(9, 6))
ax = plt.subplot(2, 2, 1)
ax.plot(w)
ax.set_title(True)

ax = plt.subplot(2, 2, 2)
ax.plot(lr_w)
ax.set_title("Lr")

ax = plt.subplot(2, 2, 3)
ax.plot(ridge_w)
ax.set_title("Ridge")

ax = plt.subplot(2, 2, 4)
ax.plot(lasso_w)
ax.set_title("Lasso")
plt.show()

第三部分

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.neighbors import KNeighborsRegressor
import sklearn.datasets as datasets
import time
from sklearn.model_selection import train_test_split

faces = datasets.fetch_olivetti_faces()  # 联网获取数据
# print(faces)
data = faces.get("images")  # data里面是整个人脸
# print(data.shape)
# index = np.random.randint(400, size=1)[0]
# plt.imshow(data[index], cmap=plt.cm.gray)
# plt.show()
X = data[:, :32].reshape(400, -1)  # 所有的上半部分人脸
y = data[:, 32:].reshape(400, -1)  # 所有的下半部分人脸

# print(y.shape)  # (400, 32, 64)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=10)
# print(X_train.shape)    # (390, 32, 64)   是三维数据
# 切分数据时加上.reshape(400, -1),变成了(390, 2048)二维数据

# index = np.random.randint(390, size=1)[0]
# print(X_train[index].shape) # (2048,)
# face_up = X_train[index].reshape(32, 64)
# face_down = y_train[index].reshape(32, 64)
#
# ax = plt.subplot(1, 3, 1)
# ax.imshow(face_up, cmap=plt.cm.gray)
#
# ax = plt.subplot(1, 3, 2)
# ax.imshow(face_down, cmap=plt.cm.gray)
#
# ax = plt.subplot(1, 3, 3)
# ax.imshow(np.concatenate([face_up, face_down], axis=0), cmap=plt.cm.gray)
# plt.show()

estimators = {}
estimators["KNN"] = KNeighborsRegressor(n_neighbors=5)
estimators["Lr"] = LinearRegression()
estimators["Ridge"] = Ridge(alpha=1)
estimators["Lasso"] = Lasso(alpha=1)

predict_ = {}  # 存放预测结果
for key, model in estimators.items():
    # print(key, model)
    model.fit(X_train, y_train)  # 训练模型
    y_ = model.predict(X_test)  # 预测结果
    predict_[key] = y_  # predict_[KNN] = KNN预测的结果
# print(predict_)
# 可视化操作
# 10行 6列
plt.figure(figsize=(6 * 2, 10 * 2))
for i in range(10):  # 10行
    # 第一列
    ax = plt.subplot(10, 6, 1 + i * 6)
    face_up = X_test[i].reshape(32, 64)
    face_down = y_test[i].reshape(32, 64)
    ax.imshow(np.concatenate([face_up, face_down], axis=0), cmap="gray")
    ax.axis("off")  # 去除刻度值
    if i == 0:
        ax.set_title("True")
    # 第二列
    ax = plt.subplot(10, 6, 2 + i * 6)
    ax.imshow(face_up, cmap="gray")
    ax.axis("off")
    if i == 0:
        ax.set_title("Face_up")
    # 第三列:3+i*6  第四列4+i*6   预测的人脸在字典predict_字典里面
    for j, key in enumerate(predict_):
        ax = plt.subplot(10, 6, 3 + j + i * 6)  # 这里是直接操作了当前行的三、四、五、六列的数据
        y_ = predict_[key]  # 例如当前y_为predict_[KNN]预测结果
        face_down_ = y_[i].reshape(32, 64)  # i为当前的索引也是行数
        ax.imshow(np.concatenate([face_up, face_down_], axis=0), cmap="gray")
        ax.axis("off")
        if i == 0:
            ax.set_title(key)
# plt.show()
start = time.perf_counter()
end = time.perf_counter()
print(f"程序耗时为{(end - start).__round__(20)}")

发布了30 篇原创文章 · 获赞 5 · 访问量 3312

猜你喜欢

转载自blog.csdn.net/Python_DJ/article/details/104588536
今日推荐