简体   繁体   English

使用 sklearn.linearmodel 预测 x 值

[英]using sklearn.linearmodel to predict an x value

using sklearn to predict clearly gives wrong answer使用 sklearn 预测清楚地给出错误答案

this is my code:这是我的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

#read file
data=pd.read_csv(r"C:\Users\Administrator\OneDrive - Trøndelag fylkeskommune\Skrivebord\Scripts\POPULATION_NORWAY.csv")
data.head()

data=data [["YEAR","POPULATION"]]

print(data)

plt.scatter(data["YEAR"], data["POPULATION"] , color="blue")
plt.xlabel("YEAR")
plt.ylabel("POPULATION")
plt.show()

train=data[:(int((len(data))))]
test=data[(int((len(data)))):]

regr=linear_model.LinearRegression()

train_x=np.array(train[["YEAR"]])
train_y=np.array(train[["POPULATION"]])

regr.fit(train_x,train_y)

plt.scatter(train["YEAR"], train["POPULATION"], color="blue" )
plt.plot(train_x, regr.coef_*train_x+regr.intercept_, "-r")
plt.xlabel("Year")
plt.ylabel("Population")
plt.show()


def get_regression_predictions(input_features,intercept,slope):
    predicted_values=input_features*slope+intercept

    return predicted_values


future_year=float(input("what year is it?"))
future_population=float(input("what is the population?"))


estimated_population=get_regression_predictions(future_year,regr.intercept_[0],regr.coef_[0][0])
print ("estimated population :",estimated_population)


estimated_year=get_regression_predictions(future_population,regr.intercept_[0],regr.coef_[0][0])
print ("estimated year :",estimated_year)

these are the values:这些是价值观:

YEAR  POPULATION
1900       2.217
1910       2.376
1920       2.616
1930       2.799
1940       2.963
1950       3.249
1960       3.567
1970       3.863
1980       4.078
1990       4.233
2000       4.478
2010       4.858
2020       5.367

this is population number in millions in Norway这是挪威的数百万人口

this is the input and output:这是输入和 output:

what year is it?2050 

what is the population?6 

estimated population : 5.8595164835164795  

estimated year : -45.6942065934066 

as you can see, the estimated year is all wrong, i expect the estimated year to be someting closer to 2030如您所见,估计的年份都是错误的,我预计估计的年份会接近 2030 年

Well, you are approximating the population as function of time like this: Pop = year*slope + intercepet好吧,您将人口近似为 function 的时间,如下所示: Pop = year*slope + intercepet

To get the year in function of population, you need invert the expression:要获得人口 function 中的年份,您需要反转表达式:

year = (pop - intercept)/slope年 = (pop - 截距)/斜率

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM