Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有一种方法可以在python中使用数据集中的变量计数(以日期作为预测值)来运行线性回归?_Python_Scikit Learn_Linear Regression - Fatal编程技术网

有没有一种方法可以在python中使用数据集中的变量计数(以日期作为预测值)来运行线性回归?

有没有一种方法可以在python中使用数据集中的变量计数(以日期作为预测值)来运行线性回归?,python,scikit-learn,linear-regression,Python,Scikit Learn,Linear Regression,我试图建立一个线性模型来预测某一年的犯罪数量 数据集设置如下:- 例如:- 每个数据点都指在给定日期发生的犯罪 Date Crime 12-31-15 yes 12-15-15 yes 11-14-13 yes 03-15-11 yes 我想运行一个线性回归,我的预测值是年份,预测变量是每年的犯罪数量 我所做的是计算每年的犯罪数量,基本上给了我: df['countsofarrests'] = df.groupby('year')['year'].transform(

我试图建立一个线性模型来预测某一年的犯罪数量

数据集设置如下:-

例如:-

每个数据点都指在给定日期发生的犯罪

Date      Crime
12-31-15   yes
12-15-15   yes
11-14-13   yes
03-15-11   yes
我想运行一个线性回归,我的预测值是年份,预测变量是每年的犯罪数量

我所做的是计算每年的犯罪数量,基本上给了我:

df['countsofarrests'] = df.groupby('year')['year'].transform('count')
x(2011年、2012年、2013年……2018年)

y(4123231232311…1231)
每年的犯罪数量

我的问题是,我可以这样做吗?这样我就可以预测2019年的犯罪率,并且必须按年份合并?这样做,我觉得我丢失了很多数据

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

model = regressor.fit(x, y)

model.predict(2019)

你所要做的就是:):


我的问题不是运行回归,更重要的是这样做会将我拥有的数据点从数据集中的数千个减少到仅仅几年,这在我看来严重限制了回归。我是不是想过头了?哦,好吧。只需将数据缩减到年度水平,然后预测未来。您仍然可以利用线性回归。你的数据是几年的?嘿,我已经更新了代码。你想要的是超简单的。
model.predict([[2019]])
import pandas as pd

Date;Crime
12-31-15;yes
12-15-15;yes
11-14-13;yes
03-15-11;yes


df = pd.read_clipboard(sep=';')
df['Date'] = pd.to_datetime(df['Date'])
df['year'] = df.Date.dt.year

print(df)

Date    Crime   year
0   2015-12-31  yes 2015
1   2015-12-15  yes 2015
2   2013-11-14  yes 2013
3   2011-03-15  yes 2011

df = (df.groupby('year').Crime.count()).reset_index()
df
year    Crime
0   2011    1
1   2013    1
2   2015    2

X = df['year'].values.reshape(-1,1)
y = df['Crime']


from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
model = regressor.fit(X, y)

model.predict([[2019]]) 

array([2.83333333])