English 中文(简体)
在1个层面之后,确定一个阵容元素,并按顺序列出所要求的阵列。 检测到的体质为2,+inhomogeneous
原标题:setting an array element with a sequence requested array has an inhomogeneous shape after 1 dimensions The detected shape was (2,)+inhomogeneous part
import os
import numpy as np
from scipy.signal import *
import csv
import matplotlib.pyplot as plt

from scipy import signal
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DataFilter, FilterTypes, AggOperations, WindowFunctions, DetrendOperations
from sklearn.cluster import KMeans

#Options to read:  EEG-IO ,  EEG-VV ,  EEG-VR ,  EEG-MB 
data_folder =  EEG-IO  

# Parameters and bandpass filtering
fs = 250.0

# Reading data files
file_idx = 0
list_of_files = [f for f in os.listdir(data_folder) if os.path.isfile(os.path.join(data_folder, f)) and  _data  in f] #List of all the files, Lists are randomized, its only looking for file with _data in it
print(list_of_files)
file_sig = list_of_files[file_idx] # Data File
file_stim = list_of_files[file_idx].replace( _data , _labels ) #Label File, Replacing _data with _labels
print ("Reading: ", file_sig, file_stim)

# Loading data
if data_folder ==  EEG-IO  or data_folder ==  EEG-MB :
    data_sig = np.loadtxt(open(os.path.join(data_folder,file_sig), "rb"), delimiter=";", skiprows=1, usecols=(0,1,2)) #data_sig would be a buffer
elif data_folder ==  EEG-VR  or data_folder ==  EEG-VV :
    data_sig = np.loadtxt(open(os.path.join(data_folder,file_sig), "rb"), delimiter=",", skiprows=5, usecols=(0,1,2)) 
    data_sig = data_sig[0:(int(200*fs)+1),:] # getting data ready -- not needed for previous 2 datasets
    data_sig = data_sig[:,0:3] #
    data_sig[:,0] = np.array(range(0,len(data_sig)))/fs


############ Calculating PSD ############
index, ch = data_sig.shape[0], data_sig.shape[1]
# print(index)
feature_vectors = [[], []]
feature_vectorsa = [[], []]
feature_vectorsb = [[], []]
feature_vectorsc = [[], []]
#for x in range(ch):
#for x in range(1,3):
#while x < 
#while x>0:
x=1
while x>0 and x<3:
    if x==1:
        data_sig[:,1] = lowpass(data_sig[:,1], 10, fs, 4)

    elif x==2:
        data_sig[:,2] = lowpass(data_sig[:,2], 10, fs, 4)

    for y in range(500, 19328 ,500):
        #print(ch)
        if x==1:
            DataFilter.detrend(data_sig[y-500:y, 1], DetrendOperations.LINEAR.value)

            psd = DataFilter.get_psd_welch(data_sig[y-500:y, 1], nfft, nfft//2, 250,
                                       WindowFunctions.BLACKMAN_HARRIS.value)

            band_power_delta = DataFilter.get_band_power(psd, 1.0, 4.0)
            
            # Theta 4-8
            band_power_theta = DataFilter.get_band_power(psd, 4.0, 8.0)
            
            #Alpha 8-12
            band_power_alpha = DataFilter.get_band_power(psd, 8.0, 12.0)
             
            #Beta 12-30
            band_power_beta = DataFilter.get_band_power(psd, 12.0, 30.0)
            # print(feature_vectors.shape)

            feature_vectors[x].insert(y, [band_power_delta, band_power_theta, band_power_alpha, band_power_beta])
            feature_vectorsa[x].insert(y, [band_power_delta, band_power_theta])

        elif x==2:
            DataFilter.detrend(data_sig[y-500:y, 2], DetrendOperations.LINEAR.value)

            psd = DataFilter.get_psd_welch(data_sig[y-500:y, 2], nfft, nfft//2, 250,
                                       WindowFunctions.BLACKMAN_HARRIS.value)

            band_power_delta = DataFilter.get_band_power(psd, 1.0, 4.0)
            
            # Theta 4-8
            band_power_theta = DataFilter.get_band_power(psd, 4.0, 8.0)
            
            #Alpha 8-12
            band_power_alpha = DataFilter.get_band_power(psd, 8.0, 12.0)
             
            #Beta 12-30
            band_power_beta = DataFilter.get_band_power(psd, 12.0, 30.0)
            # print(feature_vectors.shape)

            # feature_vectorsc[x].insert(y, [band_power_delta, band_power_theta, band_power_alpha, band_power_beta])
            # feature_vectorsd[x].insert(y, [band_power_delta, band_power_theta])

    x = x+1

print(feature_vectorsa)
powers = np.log10(np.asarray(feature_vectors, dtype=float))
powers1 = np.log10(np.asarray(feature_vectorsa, dtype=float))
# powers2 = np.log10(np.asarray(feature_vectorsb))
# powers3 = np.log10(np.asarray(feature_vectorsc))
print(powers.shape)
print(powers1.shape)

超级混乱。 在我执行我的法典时,我继续犯这一错误:

数值错误:确定一个有顺序的阵列要素。 所要求的阵列在1个层面之后具有不祥的形态。 检测到的体质为2,+无烟部分。

追查:

File "/Users/mikaelhaji/Downloads/EEG-EyeBlinks/read_data.py", line 170, in powers = np.log10(np.asarray(feature_vectors, dtype=float)) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/numpy/core/_asarray.py", line 102, in asarray return array(a, dtype, copy=False, order=order) 数值错误:确定一个有顺序的阵列要素。 所要求的阵列在1个层面之后具有不祥的形态。 检测到的体质为2,+无烟部分。

如果大家对可能发生这种情况的原因有任何想法/答案,请让我知道。

事先得到答复。

问题回答

你们是否试图贬低 n? 当我将其从1.24.1降为1.21.6时,这一错误就消失了,只有用户停战和未来的交战。

!pip install numpy==1.21.6

<代码>dtype=“object”对我有用。

variable2 = np.asarray(variable1, dtype="object")

这里简单地介绍你错误信息:

In [19]: np.asarray([[1,2,3],[4,5]],float)
Traceback (most recent call last):
  File "<ipython-input-19-72fd80bc7856>", line 1, in <module>
    np.asarray([[1,2,3],[4,5]],float)
  File "/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py", line 102, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

如果我略去<代码>float,那么它就是一种带有警告的物体类型阵列。

In [20]: np.asarray([[1,2,3],[4,5]])
/usr/local/lib/python3.8/dist-packages/numpy/core/_asarray.py:102: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify  dtype=object  when creating the ndarray.
  return array(a, dtype, copy=False, order=order)
Out[20]: array([list([1, 2, 3]), list([4, 5])], dtype=object)

I was getting the same error. I was opening a txt file that contains a table of values, and saving it into a NumPy array defining the dtype as float since otherwise, the numbers would be strings.

with open(dirfile) as fh:
    next(fh)
    header = next(fh)[2:]
    next(fh)
    data = np.array([line.strip().split() for line in fh], float)

For the previous files, it worked perfectly however for the last file it did not: The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (35351,) + inhomogeneous part.

然而,当Imp data = nploadtxt(fh)时,出现了一个新的错误: 第351条栏的栏号。

因此,我的问题是,档案的最后一条线遗漏了最后两栏的数值。 我更正了在txt档案中,因为我想有一个相同的 n(d)结构,而且所有东西都做了罚款。

我把上述所有方法捆绑在一起,他们都没有为我工作。

实际问题是正文。

电话pip 安装Numpy==1.21.1 为我工作。

This error occurs when we have Numpy version problem.

我正在使用Python3.9。

pip install numpy==1.21.2

并为我工作。

您可能会有这样的问题:[1, np.array[0,1,2], 3, np.array[8,9,10]

你可以做的一件简单的事情是:

  1. Put the break point where the error is arising
  2. Run the IDE in debug mode
  3. Print the particular variable or line
  4. Avoid this array within array scenario and it will work!




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签