Regression算法

用线性回归找到最佳拟合直线

from google.colab import drive
drive.mount("/content/drive")
Mounted at /content/drive
from numpy import *
def loadDataSet(fileName):
numFeat = len(open(fileName).readline().split('\t')) - 1
dataMat = []
labelMat = []
fr = open(fileName)
for line in fr.readlines():
lineArr = []
curLine = line.strip().split('\t')
for i in range(numFeat):
lineArr.append(float(curLine[i]))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMat, labelMat

def loadDataSet(fileName):
numFeat = len(open(fileName).readline().split('\t')) - 1
dataMat = []
labelMat = []
fr = open(fileName)
for line in fr.readlines():
lineArr = []
curLine = line.strip().split('\t')
for i in range(numFeat):
lineArr.append(float(curLine[i]))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMat, labelMat

1.numFeat = len(open(fileName).readline().split('\t')) - 1

2.dataMat = [] 和 labelMat = []

3.fr = open(fileName)

4.for line in fr.readlines():

5.lineArr = []

curLine = line.strip().split('\t')

6.for i in range(numFeat):

7.lineArr.append(float(curLine[i]))

8.dataMat.append(lineArr) 和 labelMat.append(float(curLine[-1]))

9.return dataMat, labelMat

标准回归函数

def standRegres(xArr, yArr):
xMat = mat(xArr)
yMat = mat(yArr).T
xTx = xMat.T * xMat
if linalg.det(xTx) == 0:
print("This matrix is singular, cannot do inverse")
return
ws = xTx.I * (xMat.T * yMat)
return ws

1.def standRegres(xArr, yArr):

2.xMat = mat(xArr)

3.yMat = mat(yArr).T

4.xTx = xMat.T * xMat

5.if linalg.det(xTx) == 0:

6.ws = xTx.I * (xMat.T * yMat)

7.return ws

xArr, yArr = loadDataSet('/content/drive/MyDrive/Colab Notebooks/MachineLearning/《机器学习实战》/06丨预测数值型数据：回归/用线性回归找到最佳拟合直线/ex0.txt')
xArr[:2]
[[1.0, 0.067732], [1.0, 0.42781]]
ws = standRegres(xArr, yArr)
ws
matrix([[3.00774324],
[1.69532264]])
xMat = mat(xArr)
yMat = mat(yArr)
yHat = xMat * ws
import matplotlib.pyplot as plt
fig = plt.figure()
ax.scatter(xMat[:,1].flatten().A[0], yMat.T[:,0].flatten().A[0])
xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy*ws
ax.plot(xCopy[:,1], yHat)
plt.show()

yHat = xMat*ws
yHat.shape
(200, 1)
yMat.shape
(1, 200)
corrcoef(yHat.T, yMat)
array([[1.        , 0.98647356],
[0.98647356, 1.        ]])

局部加权线性回归函数

def lwlr(testPoint, xArr, yArr, k=1.0):
xMat = mat(xArr)
yMat = mat(yArr).T
m = shape(xMat)[0]
weights = mat(eye(m))
for j in range(m):
diffMat = testPoint - xMat[j, :]
weights[j, j] = exp(diffMat*diffMat.T/(-2.0*k**2))
xTx = xMat.T * (weights * xMat)
if linalg.det(xTx) == 0.0:
print("This matrix is singular, cannot do inverse")
return
ws = xTx.I * (xMat.T * (weights * yMat))
return testPoint * ws

def lwlr(testPoint, xArr, yArr, k=1.0):

xMat = mat(xArr)

yMat = mat(yArr).T

m = shape(xMat)[0]

weights = mat(eye(m))

for j in range(m):

diffMat = testPoint - xMat[j, :]

weights[j, j] = exp(diffMat*diffMat.T/(-2.0*k**2))

xTx = xMat.T * (weights * xMat)

if linalg.det(xTx) == 0.0:

ws = xTx.I * (xMat.T * (weights * yMat))

return testPoint * ws

def lwlrTest(testArr, xArr, yArr, k=1.0):
m = shape(testArr)[0]
yHat = zeros(m)
for i in range(m):
yHat[i] = lwlr(testArr[i], xArr, yArr, k)
return yHat
xArr, yArr = loadDataSet('/content/drive/MyDrive/Colab Notebooks/MachineLearning/《机器学习实战》/06丨预测数值型数据：回归/用线性回归找到最佳拟合直线/ex0.txt')
yArr[0]
3.176513
lwlr(xArr[0], xArr, yArr, 1.0)
<ipython-input-27-f0eaaa458f3a>:8: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
weights[j, j] = exp(diffMat*diffMat.T/(-2.0*k**2))

matrix([[3.12204471]])
lwlr(xArr[0], xArr, yArr, 0.001)
<ipython-input-27-f0eaaa458f3a>:8: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
weights[j, j] = exp(diffMat*diffMat.T/(-2.0*k**2))

matrix([[3.20175729]])
yHat = lwlrTest(xArr, xArr, yArr, 0.01)
<ipython-input-27-f0eaaa458f3a>:8: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
weights[j, j] = exp(diffMat*diffMat.T/(-2.0*k**2))
<ipython-input-28-3481d8d2a021>:5: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
yHat[i] = lwlr(testArr[i], xArr, yArr, k)
xMat = mat(xArr)
srtInd = xMat[:,1].argsort(0)
xSort = xMat[srtInd][:,0,:]
import matplotlib.pyplot as plt
fig = plt.figure()
ax.plot(xSort[:,1], yHat[srtInd])
ax.scatter(xMat[:,1].flatten().A[0], mat(yArr).T.flatten().A[0], s=2, c='red')
plt.show()

|

266 0
|

ML之回归预测：机器学习中的各种Regression回归算法、关键步骤配图
ML之回归预测：机器学习中的各种Regression回归算法、关键步骤配图
465 0
|

6109 0
|

Logistic Regression 分类算法
<div style="margin:0px; padding:0px; border:0px; line-height:1.6; font-family:'Helvetica Neue',Arial,'Hiragino Sans GB',STHeiti,'Microsoft YaHei','WenQuanYi Micro Hei',SimSun,Song,sans-serif; font
1537 0
|
1天前
|

**摘要：** 该研究利用遗传算法（GA）对混合发电系统进行优化配置，旨在最小化风能、太阳能及电池储能的成本并提升系统性能。MATLAB 2022a用于实现这一算法。仿真结果展示了一系列图表，包括总成本随代数变化、最佳适应度随代数变化，以及不同数据的分布情况，如负荷、风速、太阳辐射、弃电、缺电和电池状态等。此外，代码示例展示了如何运用GA求解，并绘制了发电单元的功率输出和年变化。该系统原理基于GA的自然选择和遗传原理，通过染色体编码、初始种群生成、适应度函数、选择、交叉和变异操作来寻找最优容量配置，以平衡成本、效率和可靠性。
29 11
|
2天前
|

**基于WOA的KNN特征选择算法摘要** 该研究提出了一种融合鲸鱼优化算法（WOA）与K近邻（KNN）分类器的特征选择方法，旨在提升KNN的分类精度。在MATLAB2022a中实现，WOA负责优化特征子集，通过模拟鲸鱼捕食行为的螺旋式和包围策略搜索最佳特征。KNN则用于评估特征子集的性能。算法流程包括WOA参数初始化、特征二进制编码、适应度函数定义（以分类准确率为基准）、WOA迭代搜索及最优解输出。该方法有效地结合了启发式搜索与机器学习，优化特征选择，提高分类性能。
23 9
|
2天前
|

**算法预览图省略** MATLAB 2022A版中，运用BP神经网络进行64QAM解调。64QAM通过6比特映射至64复数符号，提高数据速率。BP网络作为非线性解调器，学习失真信号到比特的映射，对抗信道噪声和多径效应。网络在处理非线性失真和复杂情况时展现高适应性和鲁棒性。核心代码部分未显示。
17 7
|
17小时前
|

**算法预览展示了4幅图像，从边缘检测到最终分割，体现了在matlab2022a中应用的Chan-Vese水平集迭代过程。核心代码段用于更新水平集并显示迭代效果，最后生成分割结果及误差曲线。Chan-Vese模型（2001）是图像分割的经典方法，通过最小化能量函数自动检测平滑区域和清晰边界的图像分割，适用于复杂环境，广泛应用于医学影像和机器视觉。**
9 2