简体   繁体   English

使用Scikit-Learn学习乘法

[英]Learn to multiply using Scikit-Learn

I have written a program that learns to add, 我编写了一个学习添加的程序,

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

Please help me with this program if i want it to do multiplication, i get very low accuracy. 如果我想让它做乘法,请帮助我解决这个程序,我的精度很低。

Please make as less changes as possible to the program as i am a newbie. 由于我是新手,请对程序进行尽可能少的更改。

!!Thanks in Advance!! !!提前致谢!!

Well you have multiple solutions to your problem. 好吧,您有多种解决方案。
To achieve 0 error, you need to learn a model that can learn this kind of complexity. 要实现0错误,您需要学习一个可以学习这种复杂性的模型。 You probably want to use simple models like Linear Regression, and therefore, you should expand your data using polynomial features . 您可能希望使用线性回归之类的简单模型,因此,应使用多项式特征扩展数据。 See sklearn.processing.PolynomialFeatures here . sklearn.processing.PolynomialFeatures 这里

Alternative solutions can involve more complicated models such as Neural Networks . 替代解决方案可能涉及更复杂的模型,例如神经网络 You can simply use multi-layer network with 2-3 hidden layers, and linear output layer so that the output will be unbounded. 您可以简单地使用具有2-3个隐藏层的多层网络以及线性输出层,以使输出不受限制。 This method will be less preferred for this kind of problem since it is more complex and not guaranteed to perform the best on your problem. 这种方法将不太适合此类问题,因为它比较复杂,并且不能保证在您的问题上表现最佳。

Note : 注意事项
If you do choose to try a network for this simple problem, make sure to use a Mean loss . 如果您确实选择尝试解决此简单问题的网络,请确保使用平均损失

Example : 范例

First, lets load some tools. 首先,让我们加载一些工具。
We will use linear regression and a pre-processing toolkit by scikit-learn. 我们将使用线性回归和scikit-learn的预处理工具包。

from sklearn import linear_model
from sklearn import preprocessing
from random import randint
import numpy as np

Addition problem 加法问题

# 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]

Since x+y is a linear combination of x and y , we do not need to do any feature extraction on our data. 由于x+yxy的线性组合,因此我们不需要对数据进行任何特征提取。 Minimization objective in linear regression is np.sum((x * w -Y) ** 2) where we minimize over w. 线性回归中的最小化目标为np.sum((x * w -Y) ** 2) ,其中我们对w进行最小化。 Optimal parameters for this model are [1, 1] . 该模型的最佳参数为[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)))

Output: 输出:

Addition: X=[[56, 23]], Y_hat=[79.]

Multiplication problem 乘法问题

# 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])

Now, a simple linear regressor cannot fit accurately to the data, since x[0]*x[1] is not a linear combination of elements in the sample. 现在,由于x[0]*x[1] 不是样本中元素的线性组合,因此简单的线性回归无法准确拟合数据。 However, if we choose polynomial feature extraction, we can. 但是,如果选择多项式特征提取,则可以。 Polynomial features are all polynomial combinations of the coordinates up the a defined degree, including degree 0 . 多项式特征是指直到指定次数(包括次数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))))

Output: 输出:

Multiplication: X=[[2, 4]], Y_hat=[8.]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM