国产 无码 综合区,色欲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()

在類似的錯誤中:

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

如何解決這個問題?

解決方案

當函數(shù)需要相同的值時,會添加錯誤“僅將長度為 1 的表轉換為 Python 比例”,但您會傳遞該表。

如果您查看np.int調用的簽名,您會注意到它接受表中的相同值。一般來說,如果你想實現(xiàn)一個對作用域內的所有元素接受相同元素的函數(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)

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


文章來源地址http://www.zghlxwxcb.cn/article/447.html

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

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

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

領支付寶紅包贊助服務器費用

相關文章

  • 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\\\"))))#先轉換為列表,再轉為數(shù)組 ???????????????^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: only size-1 arrays can be converted to Python scalars 這個

    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 錯誤示例 錯誤分析 解決方法 方法一:使用??flatten()?? 方法二:使用ravel() 結論 在Python中,當我們嘗試將一個數(shù)組作為標量(scalar)進行操作時,有時會遇到 ? ?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

    報錯信息: 類型錯誤,只有整型標量數(shù)組才能轉換成標量索引,但一般問題都不在于你的索引是不是整數(shù)。這個報錯一般會出現(xiàn)在你想使用一個索引列表去索引另一個列表,即諸如list[index_list]的形式,此時就會出現(xiàn)此報錯,因為 index_list 為 List列表類型,不被允許;如果是數(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\\\" 錯誤 問題源頭 解決方法 方法一:使用??item()??方法 方法二:使用索引操作 總結 語法 參數(shù) 返回值 使用場景 示例 當我們使用PyTorch進行深度學習任務時,有時會遇到以下錯誤信息:\\\"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 ?? 歡迎進入我的個人主頁,我是高斯小哥!?? ?? 博主檔案: 廣東某985本碩,SCI頂刊一作,深耕 深度學習 多年,熟練掌握PyTorch框架。 ?? 技術專長: 擅長處理各類深度學習任務,包括但不限于圖像分類、圖像

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

    在用GPU訓練模型時報如下的錯誤: TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. GPU上的tensor張量無法轉為numpy格式,那我們把它轉到CPU上即可。 方法非常簡單,只需在目標張量后面加 .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對象報錯)ValueError: All arrays must be of the same length 粉絲群里面的一個粉絲用pandas創(chuàng)建DataFrame對象,但是發(fā)生了報錯(跑來找我求助,然后順利幫助他解決了,順便記錄一下希望可以幫助到更多遇到這個bug不會解決的小伙伴),報錯信息和代碼如下: 報

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

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

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

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

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

    問題背景:最近在用vue3寫管理系統(tǒng)的登錄功能的時候,在封裝axios之后瀏覽器控制臺出現(xiàn)警告:?[Vue warn]: inject() can only be used inside setup() or functional components. 原因:因為在vue3中useRouter,useStore要放在setup中引入,我們在封裝axios文件中不能直接引入。 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 把舊的文件刪去 一般會出現(xiàn)在更改項目名稱后 感謝 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)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領取紅包

二維碼2

領紅包