Home > Back-end >  Python: whats does comma mean in in [ : ,1 ]
Python: whats does comma mean in in [ : ,1 ]

Time:02-13

guys I am new to python and learning machine learning.I have trouble understanding what does this line of code mean:-

y_scores_knn = y_probas_knn[:,1]  

I didnt get what ,1 mean inside an array box [] and what it does

CodePudding user response:

That is how you access the column with index 1 if y_scores_knn is a numpy array

CodePudding user response:

Say for example your list is somewhat like this:

a = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

if you do this print(a[:,0])
it will return [1, 2, 3]
So basically ':' means that you want to iterate through every item in the x co-ordinate

CodePudding user response:

if x is a two dimensional array (a matrix) then x[:,1] is the first column (all elements where the second index is 1). This notation is quite common for things like octave, matlab and numpy which have a concept of a two dimensional array. While it does work for numpy, it will not work for a python list of lists. A list of lists that happen to have the same number of elements is just another one dimensional list as far as python is concerned. If you want to do work with matrices and you want to use python, use numpy.

  • Related