使用sb.lmplot()的问题

我想使用lmplot探索我的数据之间的线性关系,但它返回了一个错误。有人能帮我理解我的代码哪里出了问题吗?我已经清理了我的数据,消除了空值,并将原本为字符串格式的值转换为浮点数格式。之后我尝试使用lmplot()。这是我找到csv文件的链接; 链接

代码

#Analysis of obesity by countryimport pandas as pdimport seaborn as sbimport matplotlib.pyplot as pltimport numpy as np address = 'C:/Users/Andre/Desktop/Python/firstMN/obesity-cleaned.csv'dt = pd.read_csv(address)#eliminate superfluos datadt.drop(dt['Obesity (%)'][dt['Obesity (%)'].values == 'No data'].index, inplace=True)  for i in range(len(dt)):   dt['Obesity (%)'].values[i] = float(dt['Obesity (%)'].values[i].split()[0]) #print(dt['Country'], '\n') #print(dt['Obesity (%)'])sb.lmplot('Country', 'Obesity (%)', dt) plt.show(

数据集头部

   Unnamed: 0      Country  Year Obesity (%)         Sex0           0  Afghanistan  1975         0.5  Both sexes1           1  Afghanistan  1975         0.2        Male2           2  Afghanistan  1975         0.8      Female3           3  Afghanistan  1976         0.5  Both sexes4           4  Afghanistan  1976         0.2        Male5           5  Afghanistan  1976         0.8      Female6           6  Afghanistan  1977         0.6  Both sexes7           7  Afghanistan  1977         0.2        Male8           8  Afghanistan  1977         0.9      Female9           9  Afghanistan  1978         0.6  Both sexes

输出

Traceback (most recent call last):  File "C:\Users\Andre\Desktop\Python\firstMN\obesity.py", line 23, in <module>    sb.lmplot('Country', 'Obesity (%)', dt)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\regression.py", line 616, in lmplot    facets.map_dataframe(regplot, x, y, **regplot_kws)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\axisgrid.py", line 828, in map_dataframe    self._facet_plot(func, ax, args, kwargs)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\axisgrid.py", line 846, in _facet_plot    func(*plot_args, **plot_kwargs)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\regression.py", line 817, in regplot    plotter.plot(ax, scatter_kws, line_kws)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\regression.py", line 369, in plot    self.lineplot(ax, line_kws)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\regression.py", line 412, in lineplot    grid, yhat, err_bands = self.fit_regression(ax)  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\seaborn\regression.py", line 200, in fit_regression    grid = np.linspace(x_min, x_max, 100)  File "<__array_function__ internals>", line 5, in linspace  File "C:\Users\Andre\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\core\function_base.py", line 129, in linspace    start = asanyarray(start) * 1.0numpy.core._exceptions.UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

回答:

问题在于lmplot需要两个数值,你不能将’Country’作为x值。你需要在这里使用另一种图表。

如果你这样做(尽管这没有意义),它会工作

sb.lmplot(x='Year', y='Obesity (%)', data=dt) plt.show()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注