Scikit-learn教程给我一个折旧错误,如何更新?

我买了一本《精通scikit-learn机器学习》,并开始按照书中的内容进行学习。然而,书中相当一部分代码现在已经过时了。

书中第一个代码片段,

import matplotlib.pyplot as pltX = [[6], [8], [10], [14],   [18]]y = [[7], [9], [13], [17.5], [18]]plt.figure()plt.title('Pizza price plotted against diameter')plt.xlabel('Diameter in inches')plt.ylabel('Price in dollars')plt.plot(X, y, 'k.')plt.axis([0, 25, 0, 25])plt.grid(True)plt.show()

运行得很好。然而,当我继续第二个代码片段时:

from sklearn.linear_model import LinearRegression# Training dataX = [[6], [8], [10], [14],   [18]]y = [[7], [9], [13], [17.5], [18]]# Create and fit the modelmodel = LinearRegression()model.fit(X, y)print 'A 12" pizza should cost: $%.2f' % model.predict([12])[0]

它给我了一个错误:

 A 12-inch pizza should cost: $%.2f /home/dave/anaconda3/lib/python3.5/site-packages/sklearn/utils/validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.   DeprecationWarning) Traceback (most recent call last):   File "002----Chapter2-B.py", line 11, in <module>     print ("A 12-inch pizza should cost: $%.2f") % model.predict([12])[0] TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'

而它应该给我的结果是:

 A 12" pizza should cost: $13.68

有什么方法可以修复这个问题吗?


回答:

尝试以下方法:

print ("A 12-inch pizza should cost: $%.2f" % model.predict(np.array([12]).reshape(1, -1)[0]))

我使用了reshape(1,-1)来将二维数组传递给predict函数。

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中创建了一个多类分类项目。该项目可以对…

发表回复

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