56210

Tuple error (numpy)

Question:

I padded my data using np.pad(x,[(0,0)], mode='constant') and got this error:

ValueError: Unable to create correctly shaped tuple from [(0, 0).

My x has shape (21, 4) and I want to pad it to get a shape of (22,4).

Does anyone know what is happening?

Answer1:

The rank of the first argument has to match the number of pairs in the second argument.

For example, observe that this gives the error that you see:

>>> x = np.ones((21, 4)) >>> np.pad(x, [(0,0)], mode='constant') Traceback (most recent call last): [...snip...] ValueError: Unable to create correctly shaped tuple from [(0, 0)]

The problem is that x has rank 2 but the second argument has only one pair, not two.

However, if we supply a second argument with two pairs, this succeeds:

>>> x2 = np.pad(x, [(0,0), (0,0)], mode='constant')

To get the final dimension that you want, we have to pad the first dimension by 1. One way to do that is:

>>> x2 = np.pad(x, [(0,1), (0,0)], mode='constant') >>> x2.shape (22, 4)

Recommend

  • Using `std::conditional_t` to define a class' `typedef` in dependence of its template parameter
  • Is it possible to customize the title popup in tag? [duplicate]
  • How does Consumer.endOffsets work in Kafka?
  • Remove every other newline
  • Clean Python multiprocess termination dependant on an exit flag
  • Spark multiple dataframe saves
  • Storing a dictionary of Int and Array in NSUserDefaults error
  • After one click set two different image in two div in javascript
  • Applescript: Am I able to save a text file using a variable for the filename and file path?
  • How do I insert a line break in an xtable caption?
  • why do serviceSubscriberCellularProviders return nil? (in iOS 12)
  • How to get internal link from latest revision of a wikipedia page?
  • Pagenation in single agent page not working wordpress
  • How to handle extraction of double type numbers from a complex input (string) [duplicate]
  • Substatemachine
  • Find nearest known location: Google Reverse Geocoding
  • cq5 accessing osgi servlet through Url
  • Creating my Custom Unique Key
  • Create a Windows driver to access network storage
  • Less than comparison for date in spark scala rdd
  • Binding json result in highcharts for asp.net mvc 4
  • What is the correct way to combine two UserControls into one with two states?
  • Using Autofac with AJAX-enabled WCF Service
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • What is the diff. between default.properties and project.properties?
  • Vue.js 2: Vue cannot find files from /assets folder (v-for)
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • python socket.sendto
  • Firestore: Version history of documents
  • Modifying native query cannot have named parameter bindings?
  • How to control xtics in gnuplot
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • 2d barcode reader Java ME sdk
  • Django, uWSGI & nginx: Process dies for “no reason”
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How to call jQuery function in HTML returned by AJAX
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to warp text around image in iOS?