提问人:dR Phil 提问时间:11/15/2023 更新时间:11/15/2023 访问量:23
如何利用降雨量和月份对作物产量进行未来预测
how to make future predictions for crop yield using rainfall and months
问:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
color_pal = sns.color_palette()
df = pd.read_csv('C:/Users/phil-/OneDrive/Documents/Crop.csv')
df = df.set_index('Dates')
df.index = pd.to_datetime(df.index)
df['month'] = df.index.month
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df[['Rainfall','TNS/HA','month']] = scaler.fit_transform(df[['Rainfall','TNS/HA','month']])
print(df.head())
X = df[['Rainfall','month']]
y = df['TNS/HA']
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import numpy as np
lr = LinearRegression()
lr.fit(X_train,y_train)
y_pred =lr.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
print(f"actuall test : {y_test[:5]} -- predicted{y_pred[:5]}")
print(mse)
print(rmse)
new_data = {'Rainfall': [0.0,34.6,163.6,82.1,34.7,279.9,108.9,166.9,350.4,219.9,144.1,57.9],
'month': [1,2,3,4,5,6,7,8,9,10,11,12]}
new_df = pd.DataFrame(new_data)
# Scale the new data using the same scaler
new_df_scaled = scaler.transform(new_df)
# Make predictions for the year 2023
predictions_2023 = lr.predict(new_df_scaled)
# Print the predictions for 2023
print("Predictions for 2023:", predictions_2023)
我希望它打印 2023 年的预测 但它给出了一个值错误
ValueError:特征名称应与拟合期间传递的特征名称匹配。 在适合时看到的功能名称,但现在缺少:
- TNS/HA型
答: 暂无答案
评论