• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
44 views
I am using 2 one-dimensional arrays in NumPy. I can't concatenate them using numpy.concatenate.I get following error message :

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

import numpy

x = numpy.array([19, 42, 8])

y = numpy.array([1, 1])

numpy.concatenate(x, y)
by (1.4k points)  
reshown by

2 Answers

0 votes

Lets see how we can concatenate numpy arrays together  now let's get started I'm going to make a new Python file I call it a numpy concat and like this  now the first thing I need I should impose my numpy as NP after that I'm going to make two variable a to erase sorry the first one I call it example one dot a range of dot array sorry dot array I give it a one two three and also like this  the simple and the second example I also make another array dot array four five six  now if you want to concatenate these two arrays basically we have several methods in numpy array concatenation and the first one that we are going to cover is concatenate you can simply write print n P dot concatenate and in here we can write the first example we want and the second example  now if I run this I'll show you how oh I forgot to add me now if I run this you can see that we have can't getin it to arrays now we have one two three four five six also you can concatenate a more than one array for example I have the two example in here two arrays in here I want to make another array and X 3 n P dot array and I give it for example nine five six  now if I print this print in P dot.

 I can get an eight Oh example one and array two and every three now if I run this sorry every time I forgot this yeah now if I run this you can see that we have one two three four five six and nine five six or another array  this was a one dimensional array if you have multi-dimensional array also you can can concatenate these by for example you can concatenate them by first access or by second axis now I'm going to make another example I'm making another array and great it is multi-dimensional array two dimensional one two three and another is just a minute you should make like this let me remove this  the first area is a 1-2-3 and the second one is four five and six  now if I simply print this great array you can see that we have a multi-dimensional array let me comment these examples because I don't need to this if I run this again you can see that I have this array  now if you want to concatenate this multi-dimensional multi-dimensional array along with the first axis you can simply write print concat sorry n P dot concatenate and in here you can write for the grit grit if I run this you can see that now as our it is the first grit and it's the second grade  this was in the first axis if you want to concatenate along with the second axis and you can let me comment this example .

I'm going to print n P dot concatenate you can give your grit and grit and define your axis in here access one now if I run this you can see that I have an the axis one I have in the first axis one two three one two three and four five six four five six like this this was our multi dimensional array now we are going to F for example if we have max dimension we have the first one for example is a multi-dimensional array and the second one is one dimensional array for this kind of arrays numpy has some different methods we have V stack and H stack for this now I'm going to make another example and I will show you let me comment this and like this now in here I'm making X I'm giving NP dot array I give one this is one dimensional array one two three and I have y oh this is multi dimensional array I give it four five six along with seven eight nine  now we have a different dimension the first one is a one dimension and the second one is two dimension for this kind of arrays you can use from V stack of H stack you can simply write print this jacket means vertical stack and P dot V stack and in here right you are to erase x and y if I run this you can see that we have now concat concatenated concat it these two arrays add one two three four five six and seven eight nine this was vertical if you want horizontally stack let me comment this and I want to show you another example for this I have X and P dot array in here I have a one two three and I have another dimension is four five and six we want another numpy array dot array this is also a multi-dimensional array I give you a hundred sorry I let me I give it a hundred and also and at 101  now I'm going to print I'm using horizontal stack in here and P dot H stack HS track and X Y now if I run this you can see that now it is we have concatenation of these one two three and return four five six 101  thank you guys this was our sixth video if you have any question please let me know and also for the further videos please subscribe the China 

by (9.8k points)  
0 votes

We are going to start by using ipython and import numpy as NP first let's go ahead and create some random arrays so a is a two by two we're going to multiply that by 10 and subtract five so that it's numbers are between five and negative five and we'll turn it into intz let's also create a random array B dot random and we'll do this one is a two by one it will also multiply it by 10 and subtract 5 and B dot as type int and lastly let's create C C is equal to NP dot random random this one's going to be a 1 by 2 times by 10 minus 5 C is equal to C dot as type int alright so here's our array a array B and our array C alright for this first example we're going to use a and B now remember B is 2 by 1 and a is 2 by 2 and n P dot concatenate starts off with access equals 0 as the default so if I plug in a and B .

 

Here it will not work because with access equals 0 they need to have the same number of columns because it works just like V stack so there's a there's B again now let's do n P dot concatenate with a and see this one does work because C does have the same number of columns so when it defaults to ax is equal 0 it will work like NP dot V stack number of columns need to line up but with NP concatenate if we give it a and B again we can specify the access to be access equals 1 that means it's going to work just like NP not H stack so it's gonna horizontally stack these together and that way the rows have to be the same dimension so we have two and two two rows and a two rows be and it's tax be right on the end so there you go that is how you can use MP dot concatenate in Python .

by (9.8k points)  
...