gpt4 book ai didi

How to use LinearRegression to predict the total number of victims per month(如何使用线性回归来预测每月的受害者总数)

翻译 作者:bug小助手 更新时间:2023-10-26 22:15:26 27 4
gpt4 key购买 nike



I have a largar dataset which looks like the picture below, which also contains column "Month" and "Year". I try to use Linear Regression model to precit the total number of victims per month, but I don't know how to get the total number victimsDatafram

我有一个大的数据集,看起来像下面的图片,其中还包含列“月”和“年”。我尝试使用线性回归模型来预测每月的受害者总数,但我不知道如何获得受害者总数Datafram


from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(df_pre[[]],df_pre["Year"]) #don't know how to fit the data in here.

Appreciate help!

感谢您的帮助!


I try to fit Vict Age and Month, but I got wrong answer. And I try to create a new datafram which contains only month and total victim, then the fit will have different size.

我试着去适应维克特的年龄和月份,但我得到了错误的答案。我试着创建一个只包含月份和总受害者的新数据报,那么Fit就会有不同的大小。


更多回答
优秀答案推荐

The concept behind fitting data to the model is:

将数据拟合到模型背后的概念是:


reg.fit([all_inputs], [outputs])

In Machine learning terms:
reg.fit([features], [target])

Since, I couldn't preview your dataset properly, here is a simple example on how to fit data and predict with LinearRegression.

由于我不能正确地预览您的数据集,这里有一个简单的示例,说明如何使用LinearRegress来拟合数据和预测。


Let say we have small dataset of x_1, x_2, y where x_1 and x_2 are the features (inputs to the model) while y is the target (what we want to predict).

假设我们有x_1,x_2,y的小数据集,其中x_1和x_2是特征(模型的输入),而y是目标(我们想要预测的)。


Our dataset:

我们的数据集:


x_1 = [1, 1, 2, 2]
x_2 = [1, 2, 2, 3]
y = [6, 8, 9, 11]
data = [[1, 1, 6], [1, 2, 8], [2, 2, 9], [2, 3, 11]]
The nested lists are rows (that is data has 4 rows and 3 columns)

Full code

完整代码


# Import the packages and libraries
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas as pd


# Convert our data into DataFrame
data = [[1, 1, 6], [1, 2, 8], [2, 2, 9], [2, 3, 11]]
columns = ["x_1", "x_2", "y"] # columns of the dataframe
df = pd.DataFrame(data, columns=columns) # This will turn the data into a table like your data.

# Split the data to features and label
X_train = df.copy()

y_train = X_train["y"] # This is the target/ label/ output

del X_train["y"] # delete the label from the copied dataframe, so we are left with the features only.

# To answer your question of how to fit and predict with LinearRegression
model = LinearRegression() # Instantiate the class

model.fit(X_train, y_train) # Fit the input (features i.e X_train "x_1, x_2") and the output (target "y") to the model.

result = model.predict(np.array([[3, 5]])) # Now, we want to use the model to make prediction by passing a new set of input/ features x_1 and x_2 to the model to predict

# so we should get result = [16.].

Note that we are using this simple equation
y = (1 * x_1) + (2 * x_2) + 3 and if you should pass x_1 = 3 and x_2 = 5 to the equation, y = 16 which means our model works fine.

请注意,我们使用的是这个简单的方程y=(1*x_1)+(2*x_2)+3,如果您应该将x_1=3和x_2=5传递给方程,y=16,这意味着我们的模型工作得很好。


更多回答

@Double99_ If you have any questions, feel free to ask and you can as well share link to your dataset.

@Double99_如果您有任何问题,请随时提问,您也可以分享指向您的数据集的链接。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com