Python numpy generation matrix, serial matrix code sharing, pythonnumpy

Source: Internet
Author: User

Python numpy generation matrix, serial matrix code sharing, pythonnumpy

Import numpy

Several functions related to generating the numpy matrix:

Numpy. array ()
Numpy. zeros ()
Numpy. ones ()
Numpy. eye ()

Several related functions used to generate the numpy matrix in tandem:

Numpy. array ()
Numpy. row_stack ()
Numpy. column_stack ()
Numpy. reshape ()

>>> import numpy >>> numpy.eye(3) array([[ 1., 0., 0.],     [ 0., 1., 0.],     [ 0., 0., 1.]]) >>> numpy.zeros(3) array([ 0., 0., 0.]) >>> numpy.ones(3) array([ 1., 1., 1.]) >>> x1 = numpy.array((1, 2, 3)) >>> x1 array([1, 2, 3]) >>> x2 = numpy.array([4, 5, 6]) >>> x2 array([4, 5, 6]) >>> x3 = numpy.array((x1, x2)) >>> x3 array([[1, 2, 3],     [4, 5, 6]]) >>> x4 = x3.reshape(2, 3) >>> x4 array([[1, 2, 3],     [4, 5, 6]]) >>> x4 = x3.reshape(3, 2) >>> x4 array([[1, 2],     [3, 4],     [5, 6]]) >>> x5 = numpy.row_stack(x1, x2) Traceback (most recent call last):  File "<stdin>", line 1, in <module> TypeError: vstack() takes exactly 1 argument (2 given) >>> x5 = numpy.row_stack((x1, x2)) >>> x5 array([[1, 2, 3],     [4, 5, 6]]) >>> x6 = numpy.row_stack([x1, x2]) >>> x6 array([[1, 2, 3],     [4, 5, 6]]) >>> x7 = numpy.row_stack((x6, x2)) >>> x7 array([[1, 2, 3],     [4, 5, 6],     [4, 5, 6]]) >>> x7[0] array([1, 2, 3]) >>> x7[1] array([4, 5, 6]) >>> x7[2] array([4, 5, 6]) >>> x8 = numpy.column_stack([x1, x2, x1, x2]) >>> x8 array([[1, 4, 1, 4],     [2, 5, 2, 5],     [3, 6, 3, 6]]) >>> x8[0] array([1, 4, 1, 4]) >>> x8[1] array([2, 5, 2, 5]) >>> x8[2] array([3, 6, 3, 6]) >>> x8[3] Traceback (most recent call last):  File "<stdin>", line 1, in <module> IndexError: index 3 is out of bounds for axis 0 with size 3 >>> x8[0][3] 4 >>> 

Python generates a full 2 matrix of one row and four columns

print np.ones((1,4))*2

Summary

The above is all the content about the Python numpy matrix generation and serial matrix code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.