关于局部加权线性回归问题

线性回归的一个问题是它往往会对数据拟合不足,解决这一问题的一种方法是使用一种称为局部加权线性回归的技术。我在Andrew Ng的CS229讲义中读到了这种技术,并且我还尝试编写了以下脚本:

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).TA_tensor = tf.constant(xMat)b_tensor = tf.constant(yMat)m = shape(xMat)[0]weights = mat(eye((m)))k = 1.0for 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 solutionwA = 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 coefficientsslope = solution_eval[0][0]y_intercept = solution_eval[1][0]print('slope: ' + str(slope))print('y_intercept: ' + str(y_intercept))# Get best fit linebest_fit = []for i in xArr:  best_fit.append(slope*i+y_intercept)# Plot the resultsplt.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 interpreted 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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注