본문 바로가기

Python

(17)
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_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-002. random module DecriptionCode ExampleOutput (example)Generate a random float between 0 and 1random_float = np.random.rand()andom_array = np.random.rand(5)random_2d_array = np.random.rand(3, 4) 0.73206853[0.183, 0.237, ...][[0.57, 0.84], ...] Generate random integersrandom_int = np.random.randint(10)random_int_array = np.random.randint(1, 10, size=5) 7[4, 2, 6, ...] Normal distribution random numbersnormal_arra..
Python_N-001. basic commend in Numpy A summary of the basic commands for using NumPy. OperationCommandDescriptionImport NumPyimport numpy as npImport the NumPy libraryCreate Arraynp.array( [1, 2, 3] )Create an array from a listArray of Zerosnp.zeros( 5 )Create an array of zerosArray of Onesnp.ones( 5 )Create an array of onesIdentity Matrixnp.eye(3)Create a 3x3 identity matrixFull Array np.full(  (3, 3),  fill_value  ) Create an a..