dtype('
#Linear Regression Model 
@st.cache(allow_output_mutation=True)
def linearRegression(X_train, X_test, y_tr,python,machine-learning,linear-regression,streamlit"/>
	














首页 > 解决方案 > UFuncTypeError:ufunc 'matmul' 不包含具有签名匹配类型的循环(dtype('dtype('

#Linear Regression Model 
@st.cache(allow_output_mutation=True)
def linearRegression(X_train, X_test, y_tr

问题描述

#Linear Regression Model 
@st.cache(allow_output_mutation=True)
def linearRegression(X_train, X_test, y_train, y_test):
    model = LinearRegression()
    model.fit(X_train,y_train)
    score = model.score(X_test, y_test)*100

    return score , model      

#User input for the model
def user_input():
    bedrooms = st.slider("Bedrooms: ", 1,15)
    bathrooms = st.text_input("Bathrooms: ")
    sqft_living = st.text_input("Square Feet: ")
    sqft_lot = st.text_input("Lot Size: ")
    floors = st.text_input("Number Of Floors: ")
    waterfront = st.text_input("Waterfront? For Yes type '1',  For No type '0': ")
    view = st.slider("View (A higher score will mean a better view) : ", 0,4)
    condition = st.slider("House Condition (A higher score will mean a better condition): ", 1,5)
    yr_built = st.text_input("Year Built: ")
    yr_reno = st.text_input("A Renovated Property? For Yes type '1',  For No type '0': ")
    zipcode = st.text_input("Zipcode (5 digit): ")
    year_sold = st.text_input("Year Sold: ")
    month_sold = st.slider("Month Sold: ", 1,12)
   
    user_input_prediction = np.array([bedrooms,bathrooms,sqft_living, sqft_lot,floors,waterfront,view,condition,yr_built,yr_reno,zipcode,year_sold,month_sold]).reshape(1,-1)
    
    return(user_input_prediction)

#Main function


            if(st.checkbox("Start a Search")):
                user_input_prediction = user_input()
                st.write('error1')
                pred = model.predict(user_input_prediction)
                st.write('error2')
                if(st.button("Submit")):
                    st.text("success")
                    
                 

我正在使用 Streamlit 构建一个接受用户输入的 ML 模型。在我的主函数中,它返回错误UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')并回溯到pred = model.predict(user_input_prediction)主函数将打印出 error1 但不是 error2


是否可以通过广播通道 API 将 SharedArrayBuffer 广播给 Web Worker?

Broadcast Channel API似乎是postMessageChannel Messaging API(又名MessageChannel )的替代品。我在最近版本的 Google Chrome 中成功地使用了这两种 API 来发送共享数组缓冲区;但是,我无法使用广播通道 API 发送共享数组缓冲区。

Mozilla 在https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel的文档引用了https://html.spec.whatwg.org/multipage/web-messaging.html#broadcastchannel的规范,其中说:

对于目的地中的每个目的地...

  1. 让数据为 StructuredDeserialize(serialized, targetRealm)。如果这引发异常,则捕获它,使用 MessageEvent 在目的地触发名为 messageerror 的事件,并将 origin 属性初始化为 sourceOrigin 的序列化,然后中止这些步骤。

StructuredDeserialize 在https://html.spec.whatwg.org/multipage/structured-data.html#structureddeserialize中定义,似乎暗示它涵盖了 SharedArrayBuffers:

  1. 否则,如果serialized.[[Type]]是“SharedArrayBuffer”,那么:如果targetRealm对应的agent集群没有serialized.[[AgentCluster]],那么抛出一个“DataCloneError”DOMException。否则,将 value 设置为 targetRealm 中的新 SharedArrayBuffer 对象,其 [[ArrayBufferData]] 内部槽值已序列化。[[ArrayBufferData]] 并且其 [[ArrayBufferByteLength]] 内部槽值已序列化。[[ArrayBufferByteLength]]。

阅读本文,在我看来这应该可行,但我收到一个消息事件,其中数据只是null. 如果这是一个安全问题,我希望得到一个 messageerror 事件而不是 message 事件。

这是我的最小测试用例:

broadcast-test.html(必须从 http 服务器提供 - 不能通过 file:// 工作)

<!DOCTYPE html>
<html>
  <head><title></title></head>
  <body>
    <script src="broadcast-test.js"></script>
  </body>
</html>

广播-test.js

const isThisTheWorker = this.document === undefined
const broadcastChannel = new BroadcastChannel('foo')

if (!isThisTheWorker) {
  broadcastChannel.addEventListener('message', (event) => {
    console.log('main received', event.data)
    const sab = new SharedArrayBuffer(100)
    broadcastChannel.postMessage({ hello: 'from main', sab })
  })
  var myWorker = new Worker('broadcast-test.js')
}
else {
  broadcastChannel.addEventListener('message', (event) => {
    console.log('worker received', event.data)
  })
  broadcastChannel.postMessage({ hello: 'from worker' })
}

观察到的控制台输出:(Windows 10 上的 Chrome 84.0.4147.135)

主要收到{你好:“来自工人”}

工人收到空

谷歌浏览器的实现是不正确的,还是我误解了规范?

标签: pythonmachine-learninglinear-regressionstreamlit

解决方案


我陷入了模型抛出各种错误的相同情况。但特别是对于你的情况,让我告诉我我尝试了什么:

你的这条线看起来还不错。

user_input_prediction = np.array([bedrooms,bathrooms,sqft_living, sqft_lot,floors,waterfront,view,condition,yr_built,yr_reno,zipcode,year_sold,month_sold]).reshape(1,-1)

只需尝试在您的代码下方添加此行

user_input_prediction = user_input_prediction.astype(np.float64)

因为这里的模型抛出了你的数据类型不匹配的错误,因为在所有这些特征值的内部都是矩阵(数字)的形式,所以我们需要在进行任何预测之前将其转换为浮点值。

还可以尝试将 predict 方法中的 user_input_prediction 作为列表传递:

preds = model.predict([user_input_prediction])

这对我有用,希望它也对你有用


推荐阅读