gpt4 book ai didi

python - Scikit-learn 教程出现折旧错误,如何更新?

转载 作者:行者123 更新时间:2023-11-30 09:54:37 25 4
gpt4 key购买 nike

我获得了一本使用 scikit-learn 掌握机器学习,并开始研究它。然而,现在看来很多代码已经过时了。

书中的第一个代码片段,

import matplotlib.pyplot as plt
X = [[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 data
X = [[6], [8], [10], [14], [18]]
y = [[7], [9], [13], [17.5], [18]]
# Create and fit the model
model = 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函数。

关于python - Scikit-learn 教程出现折旧错误,如何更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36583123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com