X[:,0] and X[:,1] in Python

 https://blog.csdn.net/csj664103736/article/details/72828584

What does x=x[1:] mean in python

Assign the contents of the second to last bits of x to x. 
Such as x = "abcdef"
x = x[1:]
print x
The result is: "bcdef"

Python join()

http://www.runoob.com/python/att-string-join.html

describe

The Python join() method is used to join the elements in the sequence with the specified characters to generate a new string.

grammar

join() method syntax:

str.join(sequence)

parameter

  • sequence – the sequence of elements to concatenate.

return value

Returns a new string formed by concatenating the elements of the sequence by the specified character.

example

The following example shows the use of join():

Example (Python 2.0+)

#!/usr/bin/python# -*- coding: UTF-8 -*-str = "-"; seq = ("a", "b", "c"); # 字符串序列printstr.join(seq);

The output of the above example is as follows:

a-b-c

X[:,0] is a way of writing arrays in numpy, which means that for a two-dimensional array, take all the data in the first dimension of the two-dimensional array, and take the 0th data in the second dimension. Intuitively, X [:,0] is to take the 0th data of all rows, and X[:,1] is to take the first data of all rows.

for example:

 

[python]  view plain copy  
 
  1. import numpy as np  
  2.   
  3. X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]])  
  4. print X[:,0]  
  5.    

 

The output of X[:,0] is:

 

 

 

 

[python]  view plain copy  
 
  1. import numpy as np  
  2.   
  3. X = np.array([[0,1],[2,3],[4,5],[6,7],[8,9],[10,11],[12,13],[14,15],[16,17],[18,19]])  
  4. print X[:,1]  

 

The output of X[:,1] is:

 

X[n,:] is to take all the values ​​of the element with subscript n in the first dimension.

X[1,:] takes all the values ​​of the elements with the subscript 1 in the first dimension, and outputs the result:

 

X[:, m:n], that is, take the m to n-1 columns of all data, including left and right

Example: output the data of all rows, columns 1 to 2 in the X array

 

[python]  view plain copy  
 
  1. X = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17],[18,19,20]])  
  2. print X[:,1:3]  


Output result:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810160&siteId=291194637