Python 使用Scikit学习乘法
我写了一个程序,可以学习加法Python 使用Scikit学习乘法,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,我写了一个程序,可以学习加法 from sklearn import linear_model from random import randint reg=linear_model.LinearRegression() x=[[randint(0,100),randint(0,100)] for i in range(1000)] Y=[i[0]+i[1] for i in x] reg.fit(x, Y) print(reg.pred([[56, 23]]) # OUTPUT : 79 如
from sklearn import linear_model
from random import randint
reg=linear_model.LinearRegression()
x=[[randint(0,100),randint(0,100)] for i in range(1000)]
Y=[i[0]+i[1] for i in x]
reg.fit(x, Y)
print(reg.pred([[56, 23]])
# OUTPUT : 79
如果我想让它做乘法,请帮我用这个程序,我的精确度很低
请尽可能少地修改程序,因为我是新手
!!提前谢谢 你的问题有多种解决方案。
要实现0错误,您需要学习一个能够学习这种复杂性的模型。您可能希望使用简单的模型,如线性回归,因此,您应该使用多项式特征扩展数据。请参见
sklearn.processing.PolynomialFeatures
替代解决方案可能涉及更复杂的模型,如神经网络。您可以简单地使用具有2-3个隐藏层的多层网络,以及线性输出层,这样输出将是无界的。对于这类问题,这种方法不太受欢迎,因为它更复杂,而且不能保证在您的问题上表现最好
注意:如果您选择尝试网络解决这个简单的问题,请确保使用平均损失 示例: 首先,让我们加载一些工具。
我们将使用scikit learn提供的线性回归和预处理工具包
from sklearn import linear_model
from sklearn import preprocessing
from random import randint
import numpy as np
加法问题
# Lets generate some random data
x=[[randint(0,100),randint(0,100)] for i in range(1000)]
# and compute the deterministic addition function
Y=[i[0]+i[1] for i in x]
# Again, lets generate some random data
x=[[randint(0,100),randint(0,100)] for i in range(1000)]
# And compute the multiplication of all coordinates for each sample
Y=np.array([i[0]*i[1] for i in x])
由于x+y
是x
和y
的线性组合,
我们不需要对数据进行任何特征提取。
线性回归中的最小化目标是np.sum((x*w-Y)**2)
在这里我们最小化w。
此模型的最佳参数为[1,1]
# First, we create an instance of the regressor
reg_add=linear_model.LinearRegression()
# then, fit it to the data
reg_add.fit(x, Y)
# and finally, test it on some sample
sample = [[56, 23]]
print('Addition: X={}, Y_hat={}'.format(sample,reg_add.predict(sample)))
输出:
Addition: X=[[56, 23]], Y_hat=[79.]
乘法问题
# Lets generate some random data
x=[[randint(0,100),randint(0,100)] for i in range(1000)]
# and compute the deterministic addition function
Y=[i[0]+i[1] for i in x]
# Again, lets generate some random data
x=[[randint(0,100),randint(0,100)] for i in range(1000)]
# And compute the multiplication of all coordinates for each sample
Y=np.array([i[0]*i[1] for i in x])
现在,一个简单的线性回归不能精确地拟合数据,
因为x[0]*x[1]
不是样本中元素的线性组合。
然而,如果我们选择多项式特征提取,我们可以。
多项式特征是定义度上坐标的所有多项式组合,包括度0
# Lets create an instance of the processor, using polynomial features of degree=2
pp = preprocessing.PolynomialFeatures(2)
# transform the original data
x2 = pp.fit_transform(x)
# Then, create a linear regressor,
reg_mult=linear_model.LinearRegression()
# Fit it to the processed data and the results
reg_mult.fit(x2, Y)
# and test it on a new example.
sample = [[2, 4]]
print('Multiplication: X={}, Y_hat={}'.format(sample,reg_mult.predict(pp.transform(sample))))
输出:
Multiplication: X=[[2, 4]], Y_hat=[8.]
我想说加法的准确度也不是很高。你是对的,我只是漏打了它(事实上是相反的)。实际上你应该确保你没有测试训练数据。因此,定义一个数据集X并将其拆分为培训和测试数据,并确保它们是正确的disjoint@pythonic833绩效评估的方法与问题无关。Mamamiya你太棒了,我该为部门做些什么!与SRC一起解释会很有帮助。为方便起见,添加了更多注释:)谢谢你的指导,但你能在除法问题上帮助我吗。对于除法,你需要更复杂的模型或提取更有意义的特征。例如,您可以先提取特征
1/x
,然后使用多项式特征提取,然后使用线性回归。注意,模型的先验知识是有用的,模型的构建不是机器学习!。在探索数据并试图了解目标的情况下,我们无法做到上述几点。这仅仅是一个猜测,如果你从探索开始,并且因为你可视化了数据和猜测,这是可以的。编辑:忘了再指出一次,还有许多其他的模型你可以使用