python - 关于局部加权线性回归问题
问题描述
线性回归的一个问题是它倾向于欠拟合数据,解决这个问题的一种方法是一种称为局部加权线性回归的技术。我在Andrew Ng 的 CS229 Lecture notes中读到了这项技术,并且我还尝试制作以下脚本:
trX = np.linspace(0, 1, 100)
trY= trX + np.random.normal(0,1,100)
sess = tf.Session()
xArr = []
yArr = []
for i in range(len(trX)):
xArr.append([1.0,float(trX[i])])
yArr.append(float(trY[i]))
xMat = mat(xArr);
yMat = mat(yArr).T
A_tensor = tf.constant(xMat)
b_tensor = tf.constant(yMat)
m = shape(xMat)[0]
weights = mat(eye((m)))
k = 1.0
for j in range(m):
for i in range(m):
diffMat = xMat[i]- xMat[j,:]
weights[j,j] = exp(diffMat*diffMat.T/(-2.0*k**2))
weights_tensor = tf.constant(weights)
# Matrix inverse solution
wA = tf.matmul(weights_tensor, A_tensor)
tA_A = tf.matmul(tf.transpose(A_tensor), wA)
tA_A_inv = tf.matrix_inverse(tA_A)
product = tf.matmul(tA_A_inv, tf.transpose(A_tensor))
solution = tf.matmul(product, b_tensor)
solution_eval = sess.run(solution)
# Extract coefficients
slope = solution_eval[0][0]
y_intercept = solution_eval[1][0]
print('slope: ' + str(slope))
print('y_intercept: ' + str(y_intercept))
# Get best fit line
best_fit = []
for i in xArr:
best_fit.append(slope*i+y_intercept)
# Plot the results
plt.plot(xArr, yArr, 'o', label='Data')
plt.plot(xArr, best_fit, 'r-', label='Best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.show()
当我运行上面的脚本时,发生了错误:TypeError: 'numpy.float64' object cannot be compiled as an integer。语句抛出此错误:
best_fit.append(slope*i+y_intercept)
我已尝试修复此问题,但仍未找到解决方案。请帮我。
解决方案
在循环中,i
是一个列表,例如[1.0, 1.0]
。您需要决定从列表中取什么值来相乘slope*i
。例如:
best_fit = []
for i in xArr:
best_fit.append(slope*i[0]+y_intercept)
列表中的第一个元素似乎总是等于 1。
...
[1.0, 0.24242424242424243]
[1.0, 0.25252525252525254]
[1.0, 0.26262626262626265]
[1.0, 0.27272727272727276]
[1.0, 0.2828282828282829]
[1.0, 0.29292929292929293]
[1.0, 0.30303030303030304]
[1.0, 0.31313131313131315]
[1.0, 0.32323232323232326]
[1.0, 0.33333333333333337]
[1.0, 0.3434343434343435]
[1.0, 0.3535353535353536]
...
所以我想你可能会在列表中寻找第二个元素(权重?)......
best_fit = []
for i in xArr:
best_fit.append(slope*i[1]+y_intercept)
推荐阅读
- javascript - 在 un click JQuery 中查看所有产品
- terraform - 如何使用 terraform 为 IaaSDiagnostics 启用 azure windows vm 扩展
- flutter - 为什么 dart 中的 Process 需要在每个空格之后在列表中分隔参数?
- c# - 是否可以从 CEFSHARP 控件向主窗体发送键盘事件?
- javascript - 我的对象 javascript 没有完全发送到后面
- powershell - 按类型拆分大 JSON 文件
- c - 使用预处理器指令替换函数头
- python - 运行 Django 服务器时出错,ModuleNotFoundError: No module named 'mysite.myapp'(已解决)
- swift - 为什么我会收到“从 '[String]' 转换为不相关的类型 'String' 总是失败”错误?
- python - 需要帮助计算输入中的字母