我正在jupyter笔记本python上使用天气预报代码

2024-06-06 04:00:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从事一个基于机器学习线性回归的天气预报项目。我使用了下面的代码,但得到了错误only 2 arguments accepted 代码如下所示:

from sklearn.linear_model import LinearRegression
import numpy as np
X=df.drop(['PrecipitationSumInches'], axis=1)
Y=df['PrecipitationSumInches']
Y=Y.values.reshape(-1,1)
day_index=798
days=[i for i in range(Y.size)]
clf=LinearRegression()
clf.fit(X,Y)
inp=np.array([74],[60],[45],[67],[49],[43],[33],[45],[57], 
[29.68],[10],[7],[2],[0],[20],[4],[31])
inp=inp.reshape(1,-1)
print("The Precipitation in inches for the input is:",
clf.predict(inp))

Tags: 项目代码inimport机器dffornp
2条回答

你这里有一些问题。让我们后退一步,解决一个简单的回归问题,典型的波士顿住房问题。将下面的代码复制/粘贴到Python环境中,运行它,检查结果,如果仍然有问题,请发回

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline
import sklearn

from sklearn.datasets import load_boston
boston = load_boston()

# Now we will load the data into a pandas dataframe and then will print the first few rows of the data using the head() function.
bos = pd.DataFrame(boston.data)
bos.head()


bos.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']
bos.head()

bos['MEDV'] = boston.target


bos.describe()

bos.isnull().sum()



sns.distplot(bos['MEDV'])
plt.show()

enter image description here sns.pairplot(bos)

enter image description here

corr_mat = bos.corr().round(2)

enter image description here

sns.heatmap(data=corr_mat, annot=True)

enter image description here

sns.lmplot(x = 'RM', y = 'MEDV', data = bos)

enter image description here

X = bos[['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX','PTRATIO', 'B', 'LSTAT']]
y = bos['MEDV']


from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 10)

# Training the Model
# We will now train our model using the LinearRegression function from the sklearn library.

from sklearn.linear_model import LinearRegression
lm = LinearRegression()
lm.fit(X_train, y_train)

# Prediction
# We will now make prediction on the test data using the LinearRegression function and plot a scatterplot between the test data and the predicted value.

prediction = lm.predict(X_test)
plt.scatter(y_test, prediction)

enter image description here

df1 = pd.DataFrame({'Actual': y_test, 'Predicted':prediction})
df2 = df1.head(10)
df2

df2.plot(kind = 'bar')

from sklearn import metrics
from sklearn.metrics import r2_score
print('MAE', metrics.mean_absolute_error(y_test, prediction))
print('MSE', metrics.mean_squared_error(y_test, prediction))
print('RMSE', np.sqrt(metrics.mean_squared_error(y_test, prediction)))
print('R squared error', r2_score(y_test, prediction))

结果:

MAE 4.061419182954711
MSE 34.413968453138565
RMSE 5.866341999333023
R squared error 0.6709339839115628

https://acadgild.com/blog/linear-regression-on-boston-housing-data

还有,看看这个

https://github.com/chatkausik/Linear-Regression-using-Boston-Housing-data-set/blob/master/Mini_Project_Linear_Regression.ipynb

网上还有其他类似的例子。查看Iris数据集,并尝试在此基础上进行回归。有这么多的例子。复制/粘贴一些简单的代码,让它工作起来,如果您仍然有问题,请带着问题回到这里(很多问题是不言而喻的)。记住,你从中解脱出来,你投入了什么

这不是使用numpy创建数组的方法

改变

inp=np.array([74],[60],[45],[67],[49],[43],[33],[45],[57],[29.68],[10],[7],[2],[0],[20],[4],[31])

致:

inp=np.array([74,60,45,67,49,43,33,45,57,29.68,10,7,2,0,20,4,31])

请下次格式化代码并添加错误跟踪

相关问题 更多 >