Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从随机生成的值中得到多项式的四阶,并将数组从(100)重塑为(100,4)?_Python_Python 3.x_Scikit Learn_Linear Regression_Sklearn Pandas - Fatal编程技术网

Python 如何从随机生成的值中得到多项式的四阶,并将数组从(100)重塑为(100,4)?

Python 如何从随机生成的值中得到多项式的四阶,并将数组从(100)重塑为(100,4)?,python,python-3.x,scikit-learn,linear-regression,sklearn-pandas,Python,Python 3.x,Scikit Learn,Linear Regression,Sklearn Pandas,我尝试实现多项式的四阶,并使用以下指令实现以下逻辑: 首先,从scikit学习中导入多项式特征函数 并使用它生成一个新的X_4d阵列,该阵列具有最多四阶的所有功能 变换后的形状应为(100,4),以添加高阶多项式特征,且前5个 示例应该如下所示 代码: import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures

我尝试实现多项式的四阶,并使用以下指令实现以下逻辑:

  • 首先,从scikit学习中导入多项式特征函数
  • 并使用它生成一个新的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)
    
    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,:])
    
    错误:

    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)
    
    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,:])
    
    预期结果:

    变换后的形状应为(100,4),以添加高阶多项式特征,前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]]
    

    我在解决这个问题上遇到了麻烦

    这是代码中的一个小改动,不言自明:

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

    您不能将包含100个元素(100x1)的阵列重塑为包含400个元素(100x4)的矩阵。不要使用
    np.random.uniform(大小=(100,))。重塑(100,4)
    只需使用
    np.random.uniform(大小=(100,4))
    创建随机矩阵。@Aiven感谢您的帮助,但我能够找到它来转换结果。生成输出。如果你喜欢我的答案,请接受这个解决方案。