<jupyter_start><jupyter_text>Tuple Unpacking and zip  Tuple List와 유사하지만 수정불가능한(immutable) 객체인 Tuple<jupyter_code>scores = (10, 20, 30)

print(scores[0])
print(scores[1])

scores[2] = 300<jupyter_output>10
20<jupyter_text>Tuple Packing & Unpacking packing : 여러개의 값을 하나의 변수에 할당할 때 unpacking : 하나의 tuple/list를 여러개의 변수에 할당할 때<jupyter_code># Tuple packing
scores = 10, 20
print(scores)

# Tuple unpacking
score1, score2 = (10, 20)
print(score1, score2)<jupyter_output>(10, 20)
10 20<jupyter_text>for + zip zip을 사용하면 여러개의 list에 접근할 때 indexing을 통한 접근을 대체 가능<jupyter_code># for
m_scores = [10,20,30]
e_scores = [40,50,60]

for idx in range(len(m_scores)):
    m_score = m_scores[idx]
    e_score = e_scores[idx]
    print(m_score, e_score)
    
print("=====")
# zip 이용: unpacking
for m_score, e_score in zip(m_scores, e_scores):
    print(m_score, e_score)

print("=====")
# zip 이용: tuple packing
for scores in zip(m_scores, e_scores):
    print(scores)<jupyter_output>10 40
20 50
30 60
=====
10 40
20 50
30 60
=====
(10, 40)
(20, 50)
(30, 60)<jupyter_text>Manhatten Distance Manhatten Distance: 2점의 좌표를 (x1,y1), (x2,y2)로 했을 때 |x1－ x2| ＋ |y1－ y2|로 나타내어지는 거리for과 zip을 이용하여 Manhatten Distance를 구하는 방법 if, elif, else가 간단할 때 한 줄로 표현하는 방법<jupyter_code>v1 = [1,3,5,2,1,5,2]
v2 = [2,3,1,5,2,1,3]

# for만 사용
m_distance=0

for idx in range(len(v1)):
    v1_data = v1[idx]
    v2_data = v2[idx]
    diff = v1_data - v2_data
    
    if diff < 0 : m_distance += -diff
    else: m_distance += diff
print(m_distance)
    
    
# zip 사용
m_distance=0

for val1 , val2 in zip(v1,v2):
    diff = val1 - val2
    
    if diff < 0 : m_distance += -diff
    else: m_distance += diff

print(m_distance)<jupyter_output>14
14<jupyter_text>두 과목의 평균 구하기<jupyter_code>m_scores = [40, 60, 80]
e_scores = [30, 40, 50]
class_names = ['math', 'english']

n_class =2
n_student = len(m_scores)

score_sums = [0,0]
score_means = list()

for m_score, e_score in zip(m_scores, e_scores):
    score_sums[0] += m_score
    score_sums[1] += e_score

for class_idx in range(n_class):
    mean = score_sums[class_idx] / n_student
    score_means.append(mean)
    print(f"Mean of {class_names[class_idx]} class : {mean}")<jupyter_output>Mean of math class : 60.0
Mean of english class : 40.0<jupyter_text>Accuracy 구하기 zip을 이용하여 2개의 vector에 대한 원소별 접근<jupyter_code>predictions = [0, 1, 0, 2, 1, 2, 0]
labels = [1, 1, 0, 0, 1, 2, 1]

n_data = len(predictions)
correct = 0

for pred, label in zip(predictions, labels):
    if pred == label: correct += 1

accuracy = correct / n_data
print(f"accuracy : {accuracy}%")<jupyter_output>accuracy : 0.5714285714285714%<jupyter_text>Accuracy 구하기 (2) 2차원 data가 주어졌을 때 zip의 사용법one-hot vector로 주어진 prediction, label에 대해 accuracy 구하는 과정<jupyter_code>labels = [[1, 0, 0, 0],
          [0, 1, 0, 0],
          [0, 0, 1, 0],
          [0, 1, 0, 0],
          [1, 0, 0, 0],
          [0, 0, 0, 1]]
predictions = [[1, 0, 0, 0],
               [0, 0, 1, 0],
               [0, 0, 1, 0], 
               [1, 0, 0, 0],
               [1, 0, 0, 0],
               [0, 0, 0, 1]]
              
n_pred = len(labels)
n_class = len(labels[0])
print(f"n_pred:{n_pred}, n_class:{n_class}")

accuracy = 0
for label, pred in zip(labels, predictions):
    correct_cnt = 0
    
    for l,p in zip(label, pred):
        if l == p : correct_cnt +=1
        
        if correct_cnt == n_class:
            accuracy +=1
accuracy /= n_pred

print(f"accuracy:{accuracy}%")<jupyter_output>n_pred:6, n_class:4
accuracy:0.6666666666666666%<jupyter_text>Euclidean Distance zip을 이용해 matrix로 주어진 data에 대해 Euclidean distance 구하기<jupyter_code>vectors = [[1, 11],
           [2, 12],
           [3, 13],
           [4, 14]]

e_distance = 0

for v1, v2 in vectors:
    e_distance += (v1-v2)**2
e_distance = e_distance ** 0.5

print(f"Euclidean Distance: {e_distance}")<jupyter_output>Euclidean Distance: 20.0<jupyter_text>과목 별 평균 구하기 Matrix로 주어진 2차원 점수 data에 대한 과목별 평균을 구한다.- 각 과목별로 다른 연산을 해야하는 상황에서 zip을 사용하는 장점 파악 - list에 integer을 곱하는 연산 학습<jupyter_code># 개별 학생들의 여러 과목 점수들 데이터이므로 unpacking 방식으로 접근

scores = [[10, 15, 20],
          [20, 25, 30],
          [30, 35, 40],
          [40, 45, 50]]

n_student = len(scores)
n_class = len(scores[0])

score_means = [0]*n_class

for k_score, m_score, e_score in scores:
    score_means[0] += k_score
    score_means[1] += m_score
    score_means[2] += e_score

for class_idx in range(n_class):
    score_means[class_idx] /= n_student

print("Mean of the class:",score_means)<jupyter_output>Mean of the class: [25.0, 30.0, 35.0]
{'fraction_non_alphanumeric': 0.13682581786030062, 'fraction_numerical': 0.07139699381078692, 'mean_word_length': 3.427592954990215, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '231826', 'license': 'MIT', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2271, 'n_words': 511, 'path': 'Python Practice/2_Tuple Unpacking and Zip.ipynb', 'repo_name': 'kimbyeolhee/TIL', 'size': '4524'}