• Register
Welcome to Kodlogs Q&A, programming questions and answer website.

Most popular tags

java program python php javascript android c# r mysql spring c spring-boot eclipse arrays ggplot2 python-3 exception tomcat sql-server numpy jdbc windows string html hibernate ssl sockets x keytool python-2 pandas sql 7 asp net java-8 intellij-idea ssl-certificate entity-framework macos minecraft ios csv facebook security json list class spring-mvc heap-memory reactjs for-loop scikit-learn winforms glmnet webpack illegalstateexception matlab redirect code program junit pip django maven docker ubuntu typeerror linux loops node math datetime js jquery cryptography loop int import https laravel-5 tsql unity3d fatal-error struts java-applet pytorch runtime-error conda visual-studio apache-spark pyspark garbage-collection amazon-web-services sql-server-2005 quirks-mode internet-explorer-9 internet-explorer reporting-services anova machine-learning keras indentation-error laravel unit-testing android-studio jupyter-notebook orm qt virtualenv webclient prediction visual-studio-2010 compiler-errors numpy-ndarray postgresql curl vagrant aspnet reporting dataframe nullpointerexception javafx tensorflow jsp jpa wordpress servlet indentation xcode scanner sum github deployment socketexception git certificate css util oracle cmd recursion search tcp syntax connection float httpwebrequest dictionary database main function number file html5 functional-dependencies mysql-error-1064 node-sass gulp-sass archlinux sass data-science spring-batch psycopg2 backpropagation texture2d async-await net-mvc-4 google-calendar pdo ruby-on-rails-4 ruby-on-rails dbmigrate wp-mail github-for-windows facebook-android-sdk jsx xcb microsoft-metro react-router spring-data-jpa softmax elastic-beanstalk google-sheets-api google-spreadsheet-api cnf minidump xuggler acumatica gitlab elasticsearch-5 elasticsearch app-transport-security ios10 ios9 cdn cloudflare wampserver plpgsql aptitude salt-stack sysv parent-child android-edittext textview visual-studio-2012 android-workmanager unique-index unity2d electron theano maven-compiler-plugin pickle assetbundle rstudio bar-chart python-tesseract tomcat6 primefaces solr easymock jvm-crash tomcat7 apache-httpclient-4 web-services woodstox discord jersey-2 prepared-statement resultset sencha-cmd sencha-touch-2 sencha-touch powermock fips apex x509certificate ibm-bpm websphere-7 file-permissions file-io accesscontrolexception grizzly atmosphere slick2d lwjgl informetica twitter-bootstrap-3 jax-rs resteasy spring-security-kerberos spring-security kerberos long-integer mapnik git-review vpython continuation homebrew xgboost android-asynctask stack-trace user-interface jaxws-maven-plugin maven-3 maven-2 browser-notsupportedexception google-maps-api-3 google-maps visual-studio-code truststore firebase-authentication aws-organizations php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys windows-services react-redux inputstream facebook-graph-api entity-framework-4 reportingservices-2005 mips linear-regression deep-learning block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx eclipseide javafx-2 php-not-recognized command-line phpmyadmin tinyurl classnotfoundexception atom-editor android-emulator android-sdk-tools ionic2 cordova foobar2k tcplistener net-2 net-4 ole-db-provider export-to-excel openrowset windows-10 vagrantfile jvm-arguments global-variables autopostback classformatexception eof upstart ipv4 graphviz pydot atom curses unityeditor pandoc tesseract xls oledb redux spotify cxf wamp nio stacked headless typescript hyperlink outlook jackson keystore applicationcontext pipeline iis jstl encryption perfect-square objective-c carthage xcode8 indexing standard-deviation apt sudo xampp connector apple mdf kubernetes destructor gettime arithmeticexception gmail mix probability ioexception heuristics milliseconds disk angular cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jsf jtextfield awt jpanel thymeleaf inputmismatchexception glassfish deque anaconda jupyter flask lvalue gradle servlets netbeans simulation ibm factorial javax apache arduino boot opengl virtualbox jvm margins 2147483647 mongodb cloud firebase plot plugins processor automation crash card repository installer pointers editor concatenation formatting debugging devices module testing color arraylist sequence nested date caching expected build response ip directory algorithms release collections print figure casting url expression validation integer microsoft sorting sort read logging types replace rest 4 email ajax exe excel message required text variable time size dll system files table runtime static random code web map http error version 0 2 my
0 votes
10 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.5k 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 (8.9k 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 (8.9k points)  
...