简体   繁体   English

ValueError:必须通过二维输入。 形状=(1, 50, 2)

[英]ValueError: Must pass 2-d input. shape=(1, 50, 2)

I was going to make a list of 50 random numbers, one to one hundred, and take the square and square root for the numbers, and I would put them into a nice PD.Dataframe (the first list is the random numbers which are the rows and the two other lists are the columns).我打算列出 50 个随机数,从 1 到 100,然后取数字的平方和平方根,然后我会将它们放入一个不错的 PD.Dataframe(第一个列表是随机数,它们是行和另外两个列表是列)。 My Code looks like this:我的代码如下所示:

import random
import math
import numpy as np
import pandas as pd
y = random.sample(range(1, 100), 50)
y1 = [f**2 for f in y]
y2 = [round(math.sqrt(f2)) for f2 in y]
whole = y + y1 + y2
whole2 = (np.array(whole)).reshape((50,3))
df = pd.DataFrame([whole2[:,1:]], index=whole2[:,0], columns=['Number', 'Square','Square Root'])

I would appreciate if some one could tell me where and how I went wrong.如果有人能告诉我哪里以及如何出错,我将不胜感激。 Thanks!谢谢!

Your logic is a little off here.你的逻辑有点不对劲。 I think np.stack is a little more intuitive here as well.我认为 np.stack 在这里也更直观一些。 You can also remove y from the stack if you would only like the random numbers one time.如果您只想随机数一次,也可以从堆栈中删除 y。

import random
import math
import numpy as np
import pandas as pd
y = random.sample(range(1, 100), 50)
y1 = [f**2 for f in y]
y2 = [round(math.sqrt(f2)) for f2 in y]
whole = np.stack([y1, y2], axis=-1)
df = pd.DataFrame(whole, index=y, columns=['Square','Square Root'])

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:必须通过二维输入。 形状=(430, 430, 3) - ValueError: Must pass 2-d input. shape=(430, 430, 3) 无法将列表转换为 dataframe。 不断收到错误“ValueError:必须通过二维输入。 形状=(1, 4, 5)” - Unable to convert a list into a dataframe. Keep getting the error “ValueError: Must pass 2-d input. shape=(1, 4, 5)” 我收到以下错误:“ValueError: Must pass 2-d input.shape=(1, 3, 1)”但我正在传递一个 2-d 输入。 这里发生了什么? - I am getting the following error: "ValueError: Must pass 2-d input. shape=(1, 3, 1)" but I am passing a 2-d input. What is happening here? 计算余弦相似度:ValueError: Input must be 1- or 2-d - Calculating cosine similarity: ValueError: Input must be 1- or 2-d 三维熊猫DataFrame错误“必须通过二维输入” - Three Dimensional Pandas DataFrame Error “Must Pass 2-D Input” ValueError:model 的特征数量必须与输入匹配。 决策树 - ValueError: Number of features of the model must match the input. Decision Tree ValueError:无法为张量“输入/X:0”提供形状(1、50、50、3)的值,其形状为“(?、50、50、1)” - ValueError: Cannot feed value of shape (1, 50, 50, 3) for Tensor 'input/X:0', which has shape '(?, 50, 50, 1)' ValueError:无法将输入数组从形状(50,50,3)广播到形状(50,50) - ValueError: could not broadcast input array from shape (50,50,3) into shape (50,50) scipy.sparse.hstack(([1],[2])) - >“ValueError:blocks必须是2-D”。 为什么? - scipy.sparse.hstack(([1], [2])) -> “ValueError: blocks must be 2-D”. Why? scipy.sparse.hstack [ValueError:块必须为二维] - scipy.sparse.hstack [ValueError: blocks must be 2-D]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM