【发布时间】: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