【问题标题】:I'm using a weather forecasting code on jupyter notebook python我在 jupyter notebook python 上使用天气预报代码
【发布时间】:2020-08-16 18:02:57
【问题描述】:

我正在研究一个基于机器学习线性回归的天气预报项目。我使用了以下代码,但收到错误 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))

【问题讨论】:

  • 除非我们知道你的 X 是如何构成的,否则很难说你的 inp 应该是怎样的。
  • 先生,现在请检查代码,我已经编辑了它并提供了 X。 X=df.drop(['PrecipitationSumInches'], axis=1) Y=df['PrecipitationSumInches']

标签: python numpy scikit-learn linear-regression


【解决方案1】:

这不是用 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])

请在下次格式化您的代码并添加错误跟踪。

【讨论】:

  • 您好,感谢您的快速回复。我进行了更改,现在出现以下错误: valueerror:matmul:Input operand 1 has a mismatch in its core dimension 0,with gufunc signature (n?, k), (k, m?) - >(n ?, m?)(16 号与 17 号不同)
  • 请提供 X 和 Y 的形状。可能您的训练数据的形状是 16,现在您使用的是 17,但除非您提供数据的形状、错误跟踪、代码等我们不知道
  • 这是我的 X 和 YX=df.drop(['PrecipitationSumInches'], axis=1) Y=df['PrecipitationSumInches'] Y=Y.values.reshape(-1,1)
  • 如果您不提供 df 尺寸,这意味着什么
  • 先生,我使用了“df”命令,它打印:1319 行 * 17 列。这有帮助吗?
【解决方案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()

sns.pairplot(bos)

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

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

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

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)

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 数据集并尝试对其进行回归。有很多例子。复制/粘贴一些简单的代码,让它工作,如果你仍然有问题(很多这些事情是不言而喻的),请回到这里。记住,你摆脱它,你投入了什么。

【讨论】:

  • 太好了,希望对您有帮助
猜你喜欢
  • 2011-05-16
  • 2021-05-28
  • 2013-06-22
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 2012-11-25
相关资源
最近更新 更多