본문 바로가기

전체 글

(255)
Python_EX-004. 10 way data read from a file No.MethodCode ExampleProsCons1read()# to read the entire file with open('filename.txt', 'r') as file:     data = file.read()     print(data)- Simple - Easy to use- Not suitable for large files (memory-intensive)2readline()# to read one line at a time with open('filename.txt', 'r') as file:     line = file.readline()     while line:         print(line.strip())         line = file.readline()- Suit..
Python_EX-003. how to extract a condition dataset import numpy as np# INPUT : SCORE ClassA = [10, 50, 70, 30]ClassB = [30, 40, 50, 60]ClassC = [76, 78, 92, 30]ClassD = [32, 45, 64, 42]A = np.array(ClassA).reshape(1,4)B = np.array(ClassB).reshape(1,4)C = np.array(ClassC).reshape(1,4)D = np.array(ClassD).reshape(1,4)Score = np.concatenate( (A, B, C, D), axis=0)print("""Score :""", Score)#path = ''#data = np.loadtxt(path, delimiter=',')cond = Scor..
Python_N-006. Array Slicing >>> a[  0, 3:5  ]array(  [3, 4]  )>>> a[  4:, 4:  ]array(  [  [44, 45],                [54, 55]  ]  )>>> a[  :, 2]array(  [ 2, 12, 22, 32, 42, 52 ] )>>>> a[  1:5:3, ::2  ]array(  [  [10, 12, 14,],               [40, 42, 44]  ]  )
Python_N-005. Array Broadcasting General broadcasting rule Two dimensions are compatible when  1. they are equal, or  2. one of them is 1. No.Example Code  1import numpy as np a = np.array(  [1.0, 2.0, 3.0]  ) b = 2.0 a * b an array and a scalar value are combined in an operation: 2import numpy as np a = np.array(  [  [ 0.0,  0.0,  0.0],                          [10.0, 10.0, 10.0],                          [20.0, 20.0, 20.0],  ..
Python_EX-002. Average Score Comparison (NumPy Practice) Clase A score : 10, 30, 70, 50Class B score : 30, 40, 50, 60 Q1. calculate average score  Q2. compare avg score with "argmax"
Python_N-005. 10 basic attributes in array (2) No.CommendDescriptionExample1indexingAccessing individual elements using indices.arr[0, 1]   # in the first row, second column2slicingAccessing a range of elements using : operatorarr[0:2, 1:3]   #  from rows 0-1 and columns 1-2                     # arr[ a : n ]   from a to (n-1)    3copy()Creates a copy of the arrayarr_copy = arr.copy()4reshape()Returns an array with a new shapearr_reshaped = ..
Python_N-004. 10 basic attributes in array no.AttributeDescriptionexample code1ndimThe number of dimensions of the array.import numpy as np arr = np.array(  [ [1, 2, 3], [4, 5, 6] ]  )   print(arr.ndim)  # Output: 22shapeThe shape of the array (dimensions).print(arr.shape)  # Output: (2, 3)3sizeThe total number of elements in the array.print(arr.size)  # Output: 64dtypeThe data type of the array’s elements.print(arr.dtype)  # Output: int..
Python_N-003. Data Type in array Data TypeDescriptionbool_Boolean (True or False) stored as a byteint_Default integer type (similar to Python's int)intcIdentical to C's intintpInteger type used for indexing (similar to C's ssize_t)int8Byte (-128 to 127)int16Integer (-32768 to 32767)int32Integer (-2147483648 to 2147483647)int64Integer (-9223372036854775808 to 9223372036854775807)float_Shorthand for float64float16Half precision f..