Python 3: 构造回归函数时遇到"ndarray is not contiguous"

Python 3: Met "ndarray is not contiguous" when construct a regression function

此代码旨在通过定义一个我们自己编译的函数"standRegres"来计算线性回归。虽然我们可以通过sklearn或者statsmodels中的函数来做lm,但是这里我们只是尝试自己构造函数。但不幸的是,我面对错误却无法克服它。所以,我在这里请求你的帮助。

整个代码 运行 直到最后一行都没有任何问题。如果我 运行 最后一行,则会出现一条错误消息:"ValueError: ndarray is not contiguous".

import os

import pandas as pd
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt

from sklearn.datasets import load_iris
# load data
iris = load_iris()
# Define a DataFrame
df = pd.DataFrame(iris.data, columns = iris.feature_names)
# take a look
df.head()
#len(df)


# rename the column name 
df.columns = ['sepal_length','sepal_width','petal_length','petal_width']


X = df[['petal_length']]
y = df['petal_width']


from numpy import *
#########################
# Define function to do matrix calculation
def standRegres(xArr,yArr):
    xMat = mat(xArr); yMat = mat(yArr).T
    xTx = xMat.T * xMat
    if linalg.det(xTx) == 0.0:
        print ("this matrix is singular, cannot do inverse!")
        return NA
    else :
        ws = xTx.I * (xMat.T * yMat)
        return ws

# test
x0 = np.ones((150,1))
x0 = pd.DataFrame(x0)
X0 = pd.concat([x0,X],axis  = 1)

# test
standRegres(X0,y)

此代码 运行s 没有任何问题,直到最后一行。如果我 运行 最后一行,则会出现一条错误消息:"ValueError: ndarray is not contiguous".

我干解决它但不知道如何。你可以帮帮我吗?非常感谢!

您的问题源于使用 mat 函数。坚持 array.

为了使用 array,您需要使用 @ 符号进行矩阵乘法,而不是 *。最后,您有一行内容为 xTx.I,但该函数并未为一般数组定义,因此我们可以使用 numpy.linalg.inv.

def standRegres(xArr,yArr):
    xMat = array(xArr); yMat = array(yArr).T
    xTx = xMat.T @ xMat
    if linalg.det(xTx) == 0.0:
        print ("this matrix is singular, cannot do inverse!")
        return NA
    else :
        ws = linalg.inv(xTx) @ (xMat.T @ yMat)
        return ws

# test
x0 = np.ones((150,1))
x0 = pd.DataFrame(x0)
X0 = pd.concat([x0,X],axis  = 1)

# test
standRegres(X0,y)
# Output: array([-0.36651405,  0.41641913])