101

我正在尝试在我生成的散点图上生成线性回归,但是我的数据是列表格式,并且我可以找到使用的所有示例polyfitrequire using arangearange虽然不接受列表。我已经搜索了有关如何将列表转换为数组的高低搜索,但似乎没有什么清楚的。我错过了什么吗?

接下来,我怎样才能最好地使用我的整数列表作为输入polyfit

这是我正在关注的 polyfit 示例:

from pylab import * 

x = arange(data) 
y = arange(data) 

m,b = polyfit(x, y, 1) 

plot(x, y, 'yo', x, m*x+b, '--k') 
show() 
4

7 回答 7

210

arange 生成列表(嗯,numpy 数组);输入help(np.arange)详细信息。您无需在现有列表中调用它。

>>> x = [1,2,3,4]
>>> y = [3,5,7,9] 
>>> 
>>> m,b = np.polyfit(x, y, 1)
>>> m
2.0000000000000009
>>> b
0.99999999999999833

我应该补充一点,我倾向于在poly1d这里使用而不是写出“m * x + b”和更高阶的等价物,所以我的代码版本看起来像这样:

import numpy as np
import matplotlib.pyplot as plt

x = [1,2,3,4]
y = [3,5,7,10] # 10, not 9, so the fit isn't perfect

coef = np.polyfit(x,y,1)
poly1d_fn = np.poly1d(coef) 
# poly1d_fn is now a function which takes in x and returns an estimate for y

plt.plot(x,y, 'yo', x, poly1d_fn(x), '--k') #'--k'=black dashed line, 'yo' = yellow circle marker

plt.xlim(0, 5)
plt.ylim(0, 12)

在此处输入图像描述

于 2011-05-27T05:47:26.343 回答
43

这段代码:

from scipy.stats import linregress

linregress(x,y) #x and y are arrays or lists.

给出一个清单,内容如下:

斜率:
回归线的浮动斜率
截距:回归线
的浮动截距
r-value:浮动
相关系数
p-value:
对于零假设是斜率为零的假设检验的浮动双边 p 值
stderr:float
估计的标准误

来源

于 2014-12-08T17:37:02.020 回答
5
import numpy as np
import matplotlib.pyplot as plt 
from scipy import stats

x = np.array([1.5,2,2.5,3,3.5,4,4.5,5,5.5,6])
y = np.array([10.35,12.3,13,14.0,16,17,18.2,20,20.7,22.5])
gradient, intercept, r_value, p_value, std_err = stats.linregress(x,y)
mn=np.min(x)
mx=np.max(x)
x1=np.linspace(mn,mx,500)
y1=gradient*x1+intercept
plt.plot(x,y,'ob')
plt.plot(x1,y1,'-r')
plt.show()

用这个 ..

于 2018-05-06T11:20:48.007 回答
2
from pylab import * 

import numpy as np
x1 = arange(data) #for example this is a list
y1 = arange(data) #for example this is a list 
x=np.array(x) #this will convert a list in to an array
y=np.array(y)
m,b = polyfit(x, y, 1) 

plot(x, y, 'yo', x, m*x+b, '--k') 
show()
于 2018-05-06T12:17:17.047 回答
1

另一个快速而肮脏的答案是,您可以使用以下方法将列表转换为数组:

import numpy as np
arr = np.asarray(listname)
于 2014-09-15T20:26:10.117 回答
0

线性回归是开始人工智能的一个很好的例子

这是使用 Python 进行多元线性回归的机器学习算法的一个很好的示例:

##### Predicting House Prices Using Multiple Linear Regression - @Y_T_Akademi
    
#### In this project we are gonna see how machine learning algorithms help us predict house prices. Linear Regression is a model of predicting new future data by using the existing correlation between the old data. Here, machine learning helps us identify this relationship between feature data and output, so we can predict future values.

import pandas as pd

##### we use sklearn library in many machine learning calculations..

from sklearn import linear_model

##### we import out dataset: housepricesdataset.csv

df = pd.read_csv("housepricesdataset.csv",sep = ";")

##### The following is our feature set:
##### The following is the output(result) data:
##### we define a linear regression model here: 

reg = linear_model.LinearRegression()
reg.fit(df[['area', 'roomcount', 'buildingage']], df['price'])

# Since our model is ready, we can make predictions now:
# lets predict a house with 230 square meters, 4 rooms and 10 years old building..

reg.predict([[230,4,10]])

# Now lets predict a house with 230 square meters, 6 rooms and 0 years old building - its new building..
reg.predict([[230,6,0]])

# Now lets predict a house with 355 square meters, 3 rooms and 20 years old building 
reg.predict([[355,3,20]])

# You can make as many prediction as you want.. 
reg.predict([[230,4,10], [230,6,0], [355,3,20], [275, 5, 17]])

我的数据集如下:

在此处输入图像描述

于 2021-11-05T08:21:25.947 回答
0

George 的回答与 matplotlib 的axline非常吻合,它绘制了一条无限线。

from scipy.stats import linregress
import matplotlib.pyplot as plt

reg = linregress(x, y)
plt.axline(xy1=(0, reg.intercept), slope=reg.slope, linestyle="--", color="k")
于 2021-11-15T11:48:03.333 回答