Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:10:15+00:00 2026-06-15T13:10:15+00:00

Say I have an array a : a = np.array([[1,2,3], [4,5,6]]) array([[1, 2, 3],

  • 0

Say I have an array a:

a = np.array([[1,2,3], [4,5,6]])

array([[1, 2, 3],
       [4, 5, 6]])

I would like to convert it to a 1D array (i.e. a column vector):

b = np.reshape(a, (1,np.product(a.shape)))

but this returns

array([[1, 2, 3, 4, 5, 6]])

which is not the same as:

array([1, 2, 3, 4, 5, 6])

I can take the first element of this array to manually convert it to a 1D array:

b = np.reshape(a, (1,np.product(a.shape)))[0]

but this requires me to know how many dimensions the original array has (and concatenate [0]’s when working with higher dimensions)

Is there a dimensions-independent way of getting a column/row vector from an arbitrary ndarray?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:10:16+00:00Added an answer on June 15, 2026 at 1:10 pm

    Use np.ravel (for a 1D view) or np.ndarray.flatten (for a 1D copy) or np.ndarray.flat (for an 1D iterator):

    In [12]: a = np.array([[1,2,3], [4,5,6]])
    
    In [13]: b = a.ravel()
    
    In [14]: b
    Out[14]: array([1, 2, 3, 4, 5, 6])
    

    Note that ravel() returns a view of a when possible. So modifying b also modifies a. ravel() returns a view when the 1D elements are contiguous in memory, but would return a copy if, for example, a were made from slicing another array using a non-unit step size (e.g. a = x[::2]).

    If you want a copy rather than a view, use

    In [15]: c = a.flatten()
    

    If you just want an iterator, use np.ndarray.flat:

    In [20]: d = a.flat
    
    In [21]: d
    Out[21]: <numpy.flatiter object at 0x8ec2068>
    
    In [22]: list(d)
    Out[22]: [1, 2, 3, 4, 5, 6]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: convert object to array Let's say I have an array like this:
I have an array say @array. I would like to know which values of
Let's say I have an array like this: $array = array (this,is,me,and,I,am,an,array,hello,beautiful,world); How can
Say I have an array like this: [white, red, blue, red, white, green, red,
Say I have an array like this: [auburn, http://auburn.craigslist.org/web/, http://auburn.craigslist.org/cpg/, http://auburn.craigslist.org/eng/, http://auburn.craigslist.org/sof/, http://auburn.craigslist.org/sad/] What
Let's say I have the following array: [['a'],['b'],['c']] I would like to create 3
say I have a 1D array like int[] x = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16} . I would
Say I have an array that looks like this: array(100) { [0]=> array(4) {
Let say I have an array like this: Band of Horses - Is There
Say I have an array like this: [[bham, php], [auburn, php], [bham, php], [phoenix,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.