如何从随机生成的值中获取四阶多项式并将数组从 (100) 重塑为 (100,4)?
How to get the 4th order of polynomial from random generated values and reshape array from (100) to (100,4)?
我正在尝试实现多项式的四阶并使用以下指令实现了以下逻辑:
- 首先,从 scikit-learn 导入 PolynomialFeatures 函数
- 并用它来生成一个新的 X_4d 数组,该数组具有最高为四阶特征的所有特征
- 加入高阶多项式特征变换后的形状应该是(100,4),前5
- 示例应如下所示
代码:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
def forth_order(m, a=-3.15, b=1.18, c=3.52, d=3.92, mu=0.0, sigma=0.33):
new_x = np.random.uniform(low=-1.0, high=1.0, size=(m,)).reshape(100,4)
new_y = a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma
return(new_x, new_y)
print(new_x)
print(new_x.shape)
print(new_y)
print(new_y.shape)
# to find the 4th order for random generated values
np.random.seed(42)
# call your function to generate the artificial cubic data set
new_x,new_y = make_cubic_dataset(100)
transformer = PolynomialFeatures(degree=4, include_bias=False)
transformer.fit(new_x).reshape(100,4)
x_ = transformer.transform(new_x)
X_4d = np.polyfit(x, y, 3) # fit a degree 4 (cubic) polynomial to the data
print(X_4d)
print(X_4d.shape)
错误:
ValueError: cannot reshape array of size 100 into shape (100,4)
预期结果:
加入高阶多项式特征变换后的形状应该是(100,4),前5个和samples应该是下面这样
print(X_4d.shape)
>>> (100, 4)
print(X_4d[:5,:])
>>> [[-0.25091976 0.06296073 -0.01579809 0.00396405]
[ 0.90142861 0.81257354 0.73247704 0.66027576]
[ 0.46398788 0.21528476 0.09988952 0.04634753]
[ 0.19731697 0.03893399 0.00768234 0.00151586]
[-0.68796272 0.4732927 -0.32560773 0.22400598]]
我遇到了解决这个问题的麻烦。
这是代码中的一个小改动,不言自明:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
def forth_order(m, a=-3.15, b=1.18, c=3.52, d=3.92, mu=0.0, sigma=0.33):
new_x = np.random.uniform(low=-1.0, high=1.0, size=(m,)).reshape(-1,1)
new_y = a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma
return(new_x, new_y)
np.random.seed(42)
# call your function to generate the artificial cubic data set for forth order
new_x,new_y = forth_order(100)
X_4d = transformer.transform(new_x)
X_4d = PolynomialFeatures(degree=4, include_bias=False).fit_transform(new_x)
transformer.fit(new_x)
print(X_4d.shape)
print(X_4d[:5,:])
输出
print(X_4d.shape)
>>> (100, 4)
print(X_4d[:5,:])
>>> [[-0.25091976 0.06296073 -0.01579809 0.00396405]
[ 0.90142861 0.81257354 0.73247704 0.66027576]
[ 0.46398788 0.21528476 0.09988952 0.04634753]
[ 0.19731697 0.03893399 0.00768234 0.00151586]
[-0.68796272 0.4732927 -0.32560773 0.22400598]]
我正在尝试实现多项式的四阶并使用以下指令实现了以下逻辑:
- 首先,从 scikit-learn 导入 PolynomialFeatures 函数
- 并用它来生成一个新的 X_4d 数组,该数组具有最高为四阶特征的所有特征
- 加入高阶多项式特征变换后的形状应该是(100,4),前5
- 示例应如下所示
代码:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
def forth_order(m, a=-3.15, b=1.18, c=3.52, d=3.92, mu=0.0, sigma=0.33):
new_x = np.random.uniform(low=-1.0, high=1.0, size=(m,)).reshape(100,4)
new_y = a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma
return(new_x, new_y)
print(new_x)
print(new_x.shape)
print(new_y)
print(new_y.shape)
# to find the 4th order for random generated values
np.random.seed(42)
# call your function to generate the artificial cubic data set
new_x,new_y = make_cubic_dataset(100)
transformer = PolynomialFeatures(degree=4, include_bias=False)
transformer.fit(new_x).reshape(100,4)
x_ = transformer.transform(new_x)
X_4d = np.polyfit(x, y, 3) # fit a degree 4 (cubic) polynomial to the data
print(X_4d)
print(X_4d.shape)
错误:
ValueError: cannot reshape array of size 100 into shape (100,4)
预期结果:
加入高阶多项式特征变换后的形状应该是(100,4),前5个和samples应该是下面这样
print(X_4d.shape)
>>> (100, 4)
print(X_4d[:5,:])
>>> [[-0.25091976 0.06296073 -0.01579809 0.00396405]
[ 0.90142861 0.81257354 0.73247704 0.66027576]
[ 0.46398788 0.21528476 0.09988952 0.04634753]
[ 0.19731697 0.03893399 0.00768234 0.00151586]
[-0.68796272 0.4732927 -0.32560773 0.22400598]]
我遇到了解决这个问题的麻烦。
这是代码中的一个小改动,不言自明:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
def forth_order(m, a=-3.15, b=1.18, c=3.52, d=3.92, mu=0.0, sigma=0.33):
new_x = np.random.uniform(low=-1.0, high=1.0, size=(m,)).reshape(-1,1)
new_y = a*x**3 + b*x**2 + c*x + d + np.random.randn(m)*sigma
return(new_x, new_y)
np.random.seed(42)
# call your function to generate the artificial cubic data set for forth order
new_x,new_y = forth_order(100)
X_4d = transformer.transform(new_x)
X_4d = PolynomialFeatures(degree=4, include_bias=False).fit_transform(new_x)
transformer.fit(new_x)
print(X_4d.shape)
print(X_4d[:5,:])
输出
print(X_4d.shape)
>>> (100, 4)
print(X_4d[:5,:])
>>> [[-0.25091976 0.06296073 -0.01579809 0.00396405]
[ 0.90142861 0.81257354 0.73247704 0.66027576]
[ 0.46398788 0.21528476 0.09988952 0.04634753]
[ 0.19731697 0.03893399 0.00768234 0.00151586]
[-0.68796272 0.4732927 -0.32560773 0.22400598]]