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
我有一个如下图所示的LLAGAR数据集,其中也包含列“月”和“年”。我试着用线性回归模型来预测每月的总受害者人数,但我不知道如何得到总受害者数据报
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_如果您有任何问题,请随时提问,您也可以分享指向您的数据集的链接。
我是一名优秀的程序员,十分优秀!