# -*- coding: UTF-8 -*-
from sklearn.pipeline import make_pipeline
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from sklearn.base import BaseEstimator, TransformerMixin # 估算器,转换器
class GaussianFeatures(BaseEstimator, TransformerMixin):
def __init__(self, N, width_factor=2.0):
self.N = N
self.width_factor = width_factor
@staticmethod # 调用静态方法,可以不实例化
def _gauss_basis(x, y, width, axis=None):
arg = (x - y) / width
print('x:\n', x)
print('y:\n', y)
print('width:\n', width)
print('arg = (x - y) / width:\n', ((x - y) / width))
print('np.exp(-0.5 * np.sum(arg ** 2, axis)).shape: ', np.exp(-0.5 * np.sum(arg ** 2, axis)).shape)
return np.exp(-0.5 * np.sum(arg ** 2, axis)) # 列向求和
def fit(self, X, y=None): # 学习部分
# 在数据区间内创建N个高斯分布中心
self.centers_ = np.linspace(X.min(), X.max(), self.N) # 沿x轴均分点形成的数组
self.width_ = self.width_factor * (self.centers_[1] - self.centers_[0]) # 沿x轴均分点的间距*宽度系数
# print('self.width_:', self.width_)
return self # 返回类对象自己
def transform(self, X): # 预测部分
return self._gauss_basis(X[:, :, np.newaxis], self.centers_,
self.width_, axis=1) # 列向
# 预定义模型
gauss_model = make_pipeline(GaussianFeatures(20),
LinearRegression())
rng = np.random.RandomState(1)
x = 10 * rng.rand(50) # 制作50个随机数
y = np.sin(x) + 0.1 * rng.randn(50) # 目标数组
print('=========================================================')
gauss_model.fit(x[:, np.newaxis], y) # 代入转置后的x矩阵进行学习
xfit = np.linspace(0, 10, 1000) # 用做预测的数据
print('---------------------------------------------------------')
yfit = gauss_model.predict(xfit[:, np.newaxis]) # 预测结果,得到y值
print('=========================================================')
print('yfit.shape:', yfit.shape)
plt.scatter(x, y) # 学习数据
plt.plot(xfit, yfit) # 预测效果曲线
plt.xlim(0, 10)
plt.show()
高斯基底函数参照:
https://gihyo.jp/assets/images/dev/serial/01/machine-learning/0009/002.png