【问题标题】:TypeError: must be str, not float - linear RegressionTypeError:必须是 str,而不是 float - 线性回归
【发布时间】:2020-10-11 02:19:57
【问题描述】:

当我从之前的图表中复制代码并刚刚更新变量时,我得到一个 TypeError: must be str, not float error for my linear regression.以下是我的依赖项以及代码。 (斜率,截距)线是错误指向的位置。任何帮助表示赞赏。我对编码还很陌生,似乎无法弄清楚这一点。

import pandas as pd
import numpy as np
import requests
import time
import json
import random
import scipy.stats as st

from sklearn import datasets
from scipy.stats import linregress
from pprint import pprint```


x_values = city_data.loc[city_data['Latitude']>=0]
y_values = city_data['Temperature']

(slope, intercept, rvalue, pvalue, stderr) = stats.linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, marker="o", facecolors="green", edgecolors="black",
            s=30, alpha=0.75)
plt.plot(x_values,regress_value,"r-")
plt.annotate(line_eq,(20,36),fontsize=15,color="red")
plt.xlim(-50, 85)
plt.ylim(10,95 )
plt.title('City Norther Hemisphere Latitude vs Temperature (10/10/2020)')
plt.xlabel('Latitude')
plt.ylabel('Tempurature (F)')
plt.show()```

【问题讨论】:

  • 哪一行给了你错误?
  • (斜率、截距、右值、pvalue、stderr)= stats.linregress(x_values, y_values)

标签: pandas matplotlib linear-regression


【解决方案1】:

将此行更改为:

city_data = city_data[city_data['Latitude']>=0]
x_values = city_data['Latitude']

【讨论】:

  • 打印了 x_values 并打印了我的整个数据框...
  • 这就是你想要的?
  • 不是。我只需要“纬度”列中大于或等于零的值
  • 我刚刚想通了,我将 .loc 设置为一个变量,然后将序列从该变量拉到 x_values 中,如果这有意义的话。
猜你喜欢
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多