国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

TypeError: only length-1 arrays can be converted to Python scalars

我的 Python 代碼如下所示:

import numpy as np
import matplotlib.pyplot as plt
def f(x):
    return np.int(x)
x = np.arange(1, 15.1, 0.1)
plt.plot(x, f(x))
plt.show()

在類(lèi)似的錯(cuò)誤中:

TypeError: only length-1 arrays can be converted to Python scalars

如何解決這個(gè)問(wèn)題?

解決方案

當(dāng)函數(shù)需要相同的值時(shí),會(huì)添加錯(cuò)誤“僅將長(zhǎng)度為 1 的表轉(zhuǎn)換為 Python 比例”,但您會(huì)傳遞該表。

如果您查看np.int調(diào)用的簽名,您會(huì)注意到它接受表中的相同值。一般來(lái)說(shuō),如果你想實(shí)現(xiàn)一個(gè)對(duì)作用域內(nèi)的所有元素接受相同元素的函數(shù),你可以使用np vectorize

import numpy as np
import matplotlib.pyplot as plt
def f(x):
    return np.int(x)
f2 = np.vectorize(f)
x = np.arange(1, 15.1, 0.1)
plt.plot(x, f2(x))
plt.show()

您可以覆蓋 f(x) 的定義并將 np.int 傳遞給向量函數(shù):f2 = np.vectorize(np.int)

請(qǐng)注意,np.vectorize 只是循環(huán)的一個(gè)有用且通用的函數(shù)。它不如大型套件那么有效。如果可能,您應(yīng)該擁有實(shí)時(shí)向量函數(shù)或方法(按軸類(lèi)型 (int) @FFT推薦)。


文章來(lái)源地址http://www.zghlxwxcb.cn/article/447.html

到此這篇關(guān)于TypeError: only length-1 arrays can be converted to Python scalars的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

原文地址:http://www.zghlxwxcb.cn/article/447.html

如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)聯(lián)系站長(zhǎng)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • TypeError: only size-1 arrays can be converted to Python scalars

    Traceback (most recent call last): ??File \\\"/home/yjq/socket_test/server2.py\\\", line 22, in module ????msg.data = float(np.array(eval(from_client_msg.decode(\\\"gbk\\\"))))#先轉(zhuǎn)換為列表,再轉(zhuǎn)為數(shù)組 ???????????????^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: only size-1 arrays can be converted to Python scalars 這個(gè)

    2024年02月12日
    瀏覽(31)
  • 解決TypeError: only size-1 arrays can be converted to Python scalars

    目錄 解決TypeError: only size-1 arrays can be converted to Python scalars 錯(cuò)誤示例 錯(cuò)誤分析 解決方法 方法一:使用??flatten()?? 方法二:使用ravel() 結(jié)論 在Python中,當(dāng)我們嘗試將一個(gè)數(shù)組作為標(biāo)量(scalar)進(jìn)行操作時(shí),有時(shí)會(huì)遇到 ? ?TypeError: only size-1 arrays can be converted to Python sca

    2024年02月05日
    瀏覽(30)
  • TypeError: only integer scalar arrays can be converted to a scalar index

    報(bào)錯(cuò)信息: 類(lèi)型錯(cuò)誤,只有整型標(biāo)量數(shù)組才能轉(zhuǎn)換成標(biāo)量索引,但一般問(wèn)題都不在于你的索引是不是整數(shù)。這個(gè)報(bào)錯(cuò)一般會(huì)出現(xiàn)在你想使用一個(gè)索引列表去索引另一個(gè)列表,即諸如list[index_list]的形式,此時(shí)就會(huì)出現(xiàn)此報(bào)錯(cuò),因?yàn)?index_list 為 List列表類(lèi)型,不被允許;如果是數(shù)

    2024年02月11日
    瀏覽(28)
  • 解決only one element tensors can be converted to Python scalars

    目錄 解決 \\\"only one element tensors can be converted to Python scalars\\\" 錯(cuò)誤 問(wèn)題源頭 解決方法 方法一:使用??item()??方法 方法二:使用索引操作 總結(jié) 語(yǔ)法 參數(shù) 返回值 使用場(chǎng)景 示例 當(dāng)我們使用PyTorch進(jìn)行深度學(xué)習(xí)任務(wù)時(shí),有時(shí)會(huì)遇到以下錯(cuò)誤信息:\\\"only one element tensors can be conve

    2024年02月03日
    瀏覽(31)
  • 【Python】成功解決TypeError: can only concatenate str (not “int“) to str

    【Python】成功解決TypeError: can only concatenate str (not “int“) to str

    【Python】成功解決TypeError: can only concatenate str (not “int”) to str ?? 歡迎進(jìn)入我的個(gè)人主頁(yè),我是高斯小哥!?? ?? 博主檔案: 廣東某985本碩,SCI頂刊一作,深耕 深度學(xué)習(xí) 多年,熟練掌握PyTorch框架。 ?? 技術(shù)專(zhuān)長(zhǎng): 擅長(zhǎng)處理各類(lèi)深度學(xué)習(xí)任務(wù),包括但不限于圖像分類(lèi)、圖像

    2024年04月23日
    瀏覽(38)
  • TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to

    在用GPU訓(xùn)練模型時(shí)報(bào)如下的錯(cuò)誤: TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. GPU上的tensor張量無(wú)法轉(zhuǎn)為numpy格式,那我們把它轉(zhuǎn)到CPU上即可。 方法非常簡(jiǎn)單,只需在目標(biāo)張量后面加 .cpu() 即可。 Before: After:

    2024年02月12日
    瀏覽(26)
  • 已解決ValueError: All arrays must be of the same length

    已解決ValueError: All arrays must be of the same length

    已解決(pandas創(chuàng)建DataFrame對(duì)象報(bào)錯(cuò))ValueError: All arrays must be of the same length 粉絲群里面的一個(gè)粉絲用pandas創(chuàng)建DataFrame對(duì)象,但是發(fā)生了報(bào)錯(cuò)(跑來(lái)找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個(gè)bug不會(huì)解決的小伙伴),報(bào)錯(cuò)信息和代碼如下: 報(bào)

    2024年02月02日
    瀏覽(27)
  • python異步協(xié)程爬蟲(chóng)報(bào)錯(cuò):【TypeError: object int can‘t be used in ‘a(chǎn)wait‘ expression】探討

    python異步協(xié)程爬蟲(chóng)報(bào)錯(cuò):【TypeError: object int can‘t be used in ‘a(chǎn)wait‘ expression】探討

    近日,通過(guò)異步協(xié)程爬蟲(chóng)抓取rar壓縮包文件時(shí),學(xué)習(xí)運(yùn)用異步協(xié)程來(lái)提高效率。但發(fā)生如下問(wèn)題: TypeError: object int can\\\'t be used in \\\'await\\\' expression 研究了好久,發(fā)現(xiàn)是在持久化保存時(shí),不能運(yùn)用整數(shù)作為await的參數(shù)。? 這個(gè)錯(cuò)誤的原因可能是在async函數(shù)中使用了一個(gè)整數(shù)類(lèi)型的變

    2024年02月09日
    瀏覽(24)
  • [vue warn]: inject() can only be used inside setup()

    [vue warn]: inject() can only be used inside setup()

    問(wèn)題背景:最近在用vue3寫(xiě)管理系統(tǒng)的登錄功能的時(shí)候,在封裝axios之后瀏覽器控制臺(tái)出現(xiàn)警告:?[Vue warn]: inject() can only be used inside setup() or functional components. 原因:因?yàn)樵趘ue3中useRouter,useStore要放在setup中引入,我們?cè)诜庋baxios文件中不能直接引入。 1.bug提示: ?2.然后我們就

    2024年02月05日
    瀏覽(26)
  • ERROR: There can be only one Game target per project.

    ERROR: There can be only one Game target per project.

    UATHelper: Packaging (Windows (64-bit)): ERROR: There can be only one Game target per project. D:dockIntermediateSource 把舊的文件刪去 一般會(huì)出現(xiàn)在更改項(xiàng)目名稱(chēng)后 感謝 There can be only one Game target per project - Development Discussion / Content Creation - Unreal Engine ForumsThere can be only one Game target per project -?

    2024年02月08日
    瀏覽(33)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包