MLPRegressor 结果

huangapple go评论139阅读模式
英文:

MLPRegressor result

问题

这是一个我无法弄明白或使其工作的书的示例。其他情况我都能解决,但这变成了一个挑战。
当我运行它时,它显示了这条消息:
粘贴完整输出:

回溯(最近的调用最后):
文件“c:\BAULO\PYTHON\ESTRUCTURAS\P_ML\UNIDAD6\Book16.py”,第14行,在
data_y = msu_df[w]
文件“C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py”,第3810行,在__getitem__中
indexer = self.columns._get_indexer_strict(key,“columns”)1
文件“C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py”,第6111行,在_get_indexer_strict中
self._raise_if_missing(keyarr, indexer, axis_name)
文件“C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py”,第6171行,在_raise_if_missing中
raise KeyError(f“在[{axis_name}]中没有[{key}]”)
KeyError: “在[columns]中没有[Index([('N_Applications',)], dtype='object')]”


可以从以下链接下载csv文件:https://github.com/PacktPublishing/Hands-On-Data-Preprocessing-in-Python/blob/main/Chapter06/MSU%20applications.csv

我尝试过`axis=1`和`reshape`,但我无法找出错误。我知道这个主题已经讨论过了,但我找到的也对我不起作用。

```python
import  pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor

msu_df = pd.read_csv('MSU applications.csv')
msu_df.set_index('Year', drop=True, inplace=True)

X = ['P_Football_Performance','SMAn2']
y = 'N_Applications'

w = np.reshape(y, (1,-1))

data_X = msu_df[X]
data_y = msu_df[w]

mlp = MLPRegressor(hidden_layer_sizes=6, max_iter=100000)
print(mlp.predict(mlp.fit(data_X, data_y)))
英文:

This is an example of a book that I can't figure out or make it work. Other cases I could solve but it became a challenge.
When I run it, it shows me this message:
Paste complete output:

Traceback (most recent call last):
  File "c:\BAULO\PYTHON\ESTRUCTURAS\P_ML\UNIDAD6\Book16.py", line 14, in <module>
    data_y = msu_df[w]
  File "C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3810, in __getitem__
    indexer = self.columns._get_indexer_strict(key, "columns")[1]
  File "C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 6111, in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)
  File "C:\Users\Mauri\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 6171, in _raise_if_missing
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
KeyError: "None of [Index([('N_Applications',)], dtype='object')] are in the [columns]"

The csv can be downloaded from: https://github.com/PacktPublishing/Hands-On-Data-Preprocessing-in-Python/blob/main/Chapter06/MSU%20applications.csv

I tried axis=1 and reshape, but I can't figure out the error. I know this topic has already been discussed but what I found doesn't work for me either.

import  pandas as pd
import numpy as np
from sklearn.neural_network import MLPRegressor

msu_df = pd.read_csv('MSU applications.csv')
msu_df.set_index('Year', drop=True, inplace=True)

X = ['P_Football_Performance','SMAn2']
y = 'N_Applications'

w = np.reshape(y, (1,-1))

data_X = msu_df[X]
data_y = msu_df[w]

mlp = MLPRegressor(hidden_layer_sizes=6, max_iter=100000)
print(mlp.predict(mlp.fit(data_X, data_y)))

答案1

得分: 0

你在这里做了一个不必要的步骤:

w = np.reshape(y, (1,-1))

我不确定你为什么这样做,但这一步只是将 y 从一个字符串转换成一个数组。你可以直接将 y 传递给数据框 msu_df 中的 label 来获取它:

y = 'N_Applications'
data_y = msu_df[y]

额外提示:你在 fit() 方法之上调用了 predict() 方法,这 不是 做预测的正确方法fit() 方法实质上是训练阶段,此阶段使用的数据是训练数据,在这种情况下是你的 data_Xdata_y。你应该在未见过/新数据上进行预测,而不是在模型已经训练过的数据上。你应该将这行代码:

print(mlp.predict(mlp.fit(data_X, data_y)))

替换为这行代码:

mlp.fit(data_X, data_y)

从你正在遵循的教程笔记本中获取预测的示例代码:

newData = pd.DataFrame({'P_Football_Performance': 0.364, 'SMAn2': 17198}, index=[2022])
mlp.predict(newData)
英文:

You are doing an unnecessary step here:

w = np.reshape(y, (1,-1))

I'm not sure why you are doing it but that step is just converting y from a string to an array. You can directly pass y to get the label from the dataframe msu_df:

y = 'N_Applications'
data_y = msu_df[y]

Additional: You are calling the predict() method on top of fit() method which is not the correct way to make a prediction. The fit() method is essentially the training stage and the data used in this stage is the training data which in this case your data_X and data_y. You would want to make predictions on unseen/new data, not on the ones where the model is already trained. You should replace this line:

print(mlp.predict(mlp.fit(data_X, data_y)))

with this:

mlp.fit(data_X, data_y)

Sample code for prediction from the tutorial notebook you are following:

newData = pd.DataFrame({'P_Football_Performance':0.364,'SMAn2':17198},
                   index=[2022]) 
mlp.predict(newData)

huangapple
  • 本文由 发表于 2023年8月10日 23:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定