To do what you're asking, just use the phrase
labeledArray = [0, x]
This way, you will get a standard list with 0 as the first element and a Numpy array as the second element.
However, in practice, you are probably trying to label for the purpose of later recall. In that case, I'd recommend you use a dictionary, as it is less confusing to keep track of:
myArrays = {}
myArrays[0] = x
Which can be used as follows:
>>> myArrays
{0: array([[1, 2, 3, 4],
[5, 6, 7, 8]])}
>>> myArrays[0]
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
Answer from Naveen Arun on Stack OverflowThe warning you're being given is to tell you you're using a deprecated (to be removed) syntax. It suggests you use from_dict instead, for example
import numpy as np
import pandas as pd
a=pd.DataFrame.from_dict({
'A': [1, 2, 3],
'B': [4, 5, 6]
},
orient='index', columns=['one', 'two', 'three'])
print(a)
This will give your intended output
one two three
A 1 2 3
B 4 5 6
Explanation
The following block which you say you don't understand —
a = pd.DataFrame.from_dict({
'A': [1, 2, 3],
'B': [4, 5, 6]
},
orient='index', columns=['one', 'two', 'three'])
print(a)
This creates a DataFrame from a dictionary (from_dict) which we're passing in as the first parameter.
{'A': [1, 2, 3], 'B': [4, 5, 6]}
This dictionary has 2 entries, "A" and "B", each of which contain a list of numbers. If you pass this to pd.DataFrame.from_dict by itself, e.g.
a = pd.DataFrame.from_dict({
'A': [1, 2, 3],
'B': [4, 5, 6]
})
print(a)
You would get the following output.
A B
0 1 4
1 2 5
2 3 6
As you can see, the keys for the dictionary are being output as the column headers. To use the dictionary keys as (row) index headers and rotate the data you can pass in orient='index'.
a = pd.DataFrame.from_dict({
'A': [1, 2, 3],
'B': [4, 5, 6]
}, orient='index')
print(a)
This will give the following output.
0 1 2
A 1 2 3
B 4 5 6
The final step is to pass in the column headers we want to use.
a = pd.DataFrame.from_dict({
'A': [1, 2, 3],
'B': [4, 5, 6]
},
orient='index', columns=['one', 'two', 'three'])
print(a)
Which gives the intended output
one two three
A 1 2 3
B 4 5 6
data = {'A': [1,2,3], 'B': [4,5,6]}
pd.DataFrame.from_dict(data,orient='index',columns=['one', 'two', 'three'])
The correct way to create a structured array, with values, is with a list of tuples:
In [55]: X
Out[55]:
array([(3,)],
dtype=[('label1', '<i4')])
In [56]: X=np.array([(3,4)],dtype=[('label1',int),('label2',int)])
In [57]: X
Out[57]:
array([(3, 4)],
dtype=[('label1', '<i4'), ('label2', '<i4')])
But I should caution you that such array is not 2d (or matrix), it is 1d with fields:
In [58]: X.shape
Out[58]: (1,)
In [59]: X.dtype
Out[59]: dtype([('label1', '<i4'), ('label2', '<i4')])
And you can't do math across fields; X*2 and X.sum() will produce errors. Using X in an equation like y = X*b + error will be hopeless.
You are probably better off working with real 2d numeric arrays, and do the mapping between labels and column numbers in your head, or with a dictionary.
Or use Pandas.
Since with 20 variables, memory is not an issue, you could keep on using dictionaries:
from collections import OrderedDict # Dictionary that remembers insertion order
import numpy as np
dd = OrderedDict()
dd["Var1"] = 10
dd["Var2"] = 20
dd["Var3"] = 30
# make numpy array from dict:
xx = np.array([v for v in dd.values()])
# make dict() from array:
xx2 = 2*xx
dd2 = OrderedDict((k, v) for (k,v) in zip(dd.keys(), xx2))
You can try this:
>>> import numpy as np
>>> a = np.array([[11, 12, 13],
[14, 16, 13],
[17, 15, 11],
[12, 14, 15]])
>>> np.argmax(a, axis=1) + 1
array([3, 2, 1, 3], dtype=int64)
np.argmax gives the indices of the max values in the specified axis.
So,
>>> np.argmax(a, axis=1)
array([2, 1, 0, 2], dtype=int64)
Then all you need to do is add 1 to it.
You can do it this way.
d=[]
for lst in a:
d.append(lst.index(max(lst))+1)
print(d)
output
[3, 2, 1, 3]
d=[]
for lst in a:
d.append([lst.index(max(lst))+1])
print(d)
output
[[3], [2], [1], [3]]
What you may be looking for is xarray.
From its documentation:
xarray: N-D labeled arrays and datasets in Python
xarray (formerly xray) is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!
Xarray introduces labels in the form of dimensions, coordinates and attributes on top of raw NumPy-like arrays, which allows for a more intuitive, more concise, and less error-prone developer experience. The package includes a large and growing library of domain-agnostic functions for advanced analytics and visualization with these data structures.
Xarray was inspired by and borrows heavily from pandas, the popular data analysis package focused on labelled tabular data. It is particularly tailored to working with netCDF files, which were the source of xarray’s data model, and integrates tightly with dask for parallel computing.
numpy arrays are the abstract objects that you can use to build labeled tables and plots. pandas pushes the table and data series angle, matplotlib the plotting angle. And for large scale data storage, such as generated by supercomputer models, there are systems like NETCDF and HDF5.
You might want to look at how HDF5 handles dimension scales, and how h5py gives you access to them in numpy.
http://docs.h5py.org/en/latest/high/dims.html
Datasets are multidimensional arrays. HDF5 provides support for labeling the dimensions and associating one or “dimension scales” with each dimension. A dimension scale is simply another HDF5 dataset.
Creating an array from axes is a common numpy task. np.arange and np.linspace create 1d arrays, np.meshgrid, mgrid and ogrid create 2d (or larger) arrays, which in turn are used to calculate values on a grid. Note that meshgrid allows you to specify ij or xy styles, reflecting two conventions, rows/columns v plot horizontal/vertical axes.
X, Y = np.meshgrid(x,y)
z = my_function(X,Y)
but plotting functions can take various forms of input:
plot(x, y, z) # 2 1d arrays and a 2d
scatter(X,Y,Z) # 3 2d arrays
scatter(XYZ) # 1 Nx3 array
So while this is a connection between the generating arrays and dependent one, this is a higher level of organization, one that your code has to maintain, not something that numpy does for you.
A comment mentioned structured arrays. That can replace the columns of a 2d array with named fields (and by extension to higher dimensions), but it is most useful when working with diverse data loaded from CSV files. They are more like the fields of SQL tables than the y coordinate of a plot.
NumPy arrays are homogeneous. You have to set type for label array
import numpy as np
arr=np.linspace(0,1,11)
lbl=np.empty((arr.shape), dtype=object)
lbl[arr<.25]='a'
lbl[(arr>=.25) & (arr <=.75)] = 'b'
lbl[arr>.75]='c'
print arr
print lbl
Output:
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
['a' 'a' 'a' 'b' 'b' 'b' 'b' 'b' 'c' 'c' 'c']
For creating an array of three such groups, you could do something like this -
ID = (arr>0.75)*1 + (arr>=0.25)
select_arr = np.array(['a','b','c'])
out = select_arr[ID]
Sample run -
In [64]: arr # Modified from sample posted in question to include 0.75
Out[64]:
array([ 0. , 0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7 , 0.75,
0.9 , 1. ])
In [65]: ID = (arr>0.75)*1 + (arr>=0.25)
...: select_arr = np.array(['a','b','c'])
...: out = select_arr[ID]
...:
In [66]: out
Out[66]:
array(['a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'c', 'c'],
dtype='|S1')
With pandas.DataFrame.to_csv you can write the columns and the index to a file:
import numpy as np
import pandas as pd
A = np.random.randint(0, 10, size=36).reshape(6, 6)
names = [_ for _ in 'abcdef']
df = pd.DataFrame(A, index=names, columns=names)
df.to_csv('df.csv', index=True, header=True, sep=' ')
will give you the following df.csv file:
a b c d e f
a 1 5 5 0 4 4
b 2 7 5 4 0 9
c 6 5 6 9 7 0
d 4 3 7 9 9 3
e 8 1 5 1 9 0
f 2 8 0 0 5 1
Numpy will handle n-dimensional arrays fine, but many of the facilities are limited to 2-dimensional arrays. Not even sure how you want the output file to look.
Many people who would wish for named columns overlook the recarray() capabilities of numpy. Good stuff to know, but that only "names" one dimension.
For two dimensions, Pandas is very cool.
In [275]: DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])],
.....: orient='index', columns=['one', 'two', 'three'])
Out[275]:
one two three
A 1 2 3
B 4 5 6
If output is the only problem you are trying to solve here, I'd probably just stick with a few lines of hand coded magic as it will be less weighty than installing another package for one feature.
There are many ways of doing this. Here are a few options:
In [1]: import numpy
In [2]: x = numpy.array([5,6,7,8,10,11,12,14])
In [3]: x
Out[3]: array([ 5, 6, 7, 8, 10, 11, 12, 14])
In [4]: x > 10
Out[4]: array([False, False, False, False, False, True, True, True], dtype=bool)
In [5]: ['Y' if y > 10 else 'N' for y in x]
Out[5]: ['N', 'N', 'N', 'N', 'N', 'Y', 'Y', 'Y']
In [6]: [{True: 'Y', False: 'N'}[y] for y in x > 10]
Out[6]: ['N', 'N', 'N', 'N', 'N', 'Y', 'Y', 'Y']
You could also use map or something of course :)
- for loop iter the tmp list;
- each element looped in the tmp list, will be judged by if ... first, match if ... then output 'Y' in new list which created by list comprehensive.
- does not match if ... then output 'N' in new list which created by list comprehensive.

Note: OP's intervals are [-3,-1], [0,3] & (3,..), so I am only assuming integral values. The conditions can be altered accordingly, but the design remains.
Using list comprehensions for if-elif-else:
my_list = [-3,-2,-1,0,1,2,3,4,5,6]
my_list_mapping = ['F' if ((i >= -3) & (i <= -1)) else 'M' if ((i >= 0) & (i <= 3)) else 'S' for i in my_list]
print(my_list_mapping)
['F', 'F', 'F', 'M', 'M', 'M', 'M', 'S', 'S', 'S']
You can do this:
arr = np.array([-3-2-1,0,1,2,3,4,5,6])
new_arr = np.zeros(shape = arr.shape, dtype=np.str)
new_arr[(arr>3)] = 'S'
new_arr[((arr>=-3) & (arr<=-1))] = 'F'
new_arr[((arr>=0)&(arr<=3))] = 'M'
new_arr
array(['', 'M', 'M', 'M', 'M', 'S', 'S', 'S'], dtype='<U1')
The values that don't match your condition will remain empty strings.
Also you can use numpy.empty to initialize an empty array:
new_arr = np.empty(shape = arr.shape, dtype=np.str)
Use np.stack instead of np.concatenate and everything should work:
train_array = np.stack((array_a,array_b,array_c,array_d,array_e,array_f,array_g,array_h), axis=0)
print(train_array.shape)
# (8, 300, 300)
from tensorflow.keras.utils import to_categoritcal
...
model.fit(train_data,
to_categorical(labels),
epochs=5,
verbose=1)