I created a multi-dimensional numpy array using this code:
pt=[[0 for j in range(intervals+1)] for i in range(users+1)]
A `print (np.shape(pt)) gives me
(1001,169)
I then proceeded to populate the array (code not shown) before trying to select everything but the first column to feed into matplotlib.
I referred to posts on how to select columns from a multi-dimensional array:
here
here
and
here
all of whom say I should do:
pt[:,1:]
to select everything but the first column. However this gives me the error message:
TypeError: list indices must be integers or slices, not tuple
Anyone else that reaches this post from having made the same mistake (see comments above), if you want to continue using lists then do pt[:][0:1] but really I recommend switching to numpy and ignoring all the results you get when you search for 'declaring python array'
Related
I'm learning how to do multiple regression. The functions I'm using to predict return results as an array. When I go to append the data to my pandas data frame it appends, but when I try to use the data I get an error:
ValueError: setting an array element with a sequence.
So I go to check the array and see that it has 2 dimensions. I have no clue what to do, I've printed it out and it looks like it has 1 dimension to me. This is what the array looks like when printed:
array([[1217964. ],
[1215076. ],
[1205507. ],
[1200445. ],
[1175359. ],
[1172445.56110176]])
I thought about trying to drop a dimension, but I wouldn't know what to drop since I only see one dimension. Any help is much appreciated, thanks for helping a beginner out.
It occured to me that my array may have had an empty dimension. It seems like an oversight on the package dev for the regression functions to create 2d arrays from single dimension results, but I don't know that much.
I used the method squeeze to drop my empty dimension and now it works!
I am currently working on machine learning, and as so I have an array in which the first column is the data and the second column is the label. Data was originally a cell array from Matlab (Not sure if that is important).
[My Array of arrays] https://i.stack.imgur.com/JFpWO.png
To make sure that everything is as it should be I would like to extract one of the arrays in index 0 and check its dimensions with the numpy.shape function. Currently, if I try that I just get the shape of the bigger array IE. (394,2)
Any ideas?
There are different ways.: Try..
arr[:,1,:]
or
arr[:,1]
or
[a[1] for a in arr]
I have a 2-d array in numpy. I wish to obtain unique values only in a particular column.
import numpy as np
data = np.genfromtxt('somecsvfile',dtype='str',delimiter=',')
#data looks like
[a,b,c,d,e,f,g],
[e,f,z,u,e,n,c],
...
[g,f,z,u,a,v,b]
Using numpy/scipy only, how do I obtain an array or list of unique values in the 5th column. (I know it can easily be done with pandas.)
The expected output would be 2 values: [e,a]
Correct answer posted. A simple referencing question in essence.
np.unique(data[:, 4])
With thanks.
After reading this I wrote a naive attempt to produce this
col1
---------
1
4
7
from this
ARRAY[[1,2,3], [4,5,6], [7,8,9]]
This works
SELECT unnest((ARRAY[[1,2,3], [4,5,6], [7,8,9]])[1:3][1:1]);
But I in my case, I don't know the length of the outer array.
So is there a way to hack together the slice "string" to take into account this variability?
Here was my attempt. I know, it's a bit funny
_ids := _ids_2D[('1:' || array_length(_ids_2D, 1)::text)::int][1:1];
As you can see, I just want to create the effect of [1:n]. Obviously '1:3' ain't going to parse nicely into what the array slice needs.
I could obviously use something like the unnest_2d_1d Erwin mentions in the answer linked above, but hoping for something more elegant.
If you are trying to get the first element of all nested (2nd dimension) arrays inside an array (1st dimension) then you may use
array_upper(anyarray, 1)
to get all elements of a specific dimension
anyarray[1:array_upper(anyarray, 1)][<dimension num>:<dimension num>]
e.g, to get all elements of the first dimension
anyarray[1:array_upper(anyarray, 1)][1:1]
as in the code above. Please refer to PostgreSQL manual section on Arrays for more information.
I'm trying to create a Multi dimensional array so I can create a waterfall plot in octave, a matlab type of program. (Please note octave does not have the waterfall plot option so I have to make a work around.
I have multiple arrays that have frequency in the first column and amplitude in the second column.
example: When looped through the arrays, which are called sort_array they each need to be placed into a multidimensional array on a separate page.
4000, .5
3002, .1234
1093, .7
I was trying to have each of these arrays (sort_array) added to a single multidimensional array by using a for loop to have everything added to one array to make it easier to plot and export as a text file. I also thought using k as the page option for the multiple dimension array. But I keep getting dimensions mismatch. Any ideas how to fix this?
Please note I left out the sort_array code and just included the example array called sort_array above as an example of what the array would look like. There will be about 9000 of them.
md=[];
for k=1:9000
md_tmp=[sort_array(:,1),sort_array(:,2)]
ma(:,:,k)=[ma;ma_tmp];
end
So when I type in ma(:,:,1) I would get
4000, .5
3002, .1234
1093, .7
and if I type in ma(:,:,2) I would get the next one.
thanks
Your error is here: ma(:,:,k)=[ma;ma_tmp];
You are mixing up two concepts.
Either concatenate the new matrix onto the current one:
ma = [ma; ma_tmp];
OR
Assign the new matrix directly to the correct index (this is the cleaner, more efficient solution):
ma(:,:,k) = ma_tmp;
But you can't do both.
thanks I found a work around
sort_array=sortrows(arraytmp,-1); %sort by freq
k_tmp=repmat(k,length(sort_array(:,2)),1); %to create page for freq and amp in multidimensional array
ma_tmp=[sort_array(:,1),sort_array(:,2),k_tmp];
ma=[ma;ma_tmp];