본문 바로가기
반응형

Python14

[Python] Custom Curve fitting과 Residual 분석 비교하기(ft. plt.ginput) 데이터 분석을 하다 보면 특정 포인트(점)들을 직접 입력해서 내가 원하는 커브를 만들고, 동시에 기본적인 회귀모델과 비교하고 싶을 때가 있다. 오늘은 matplotlib, scikit-learn, scipy를 활용해 비교해보는 시간을 갖는다. 아래 순서대로 진행할 예정이다. 1. 원본 데이터 생성2. 사용자가 화면에서 직접 점 찍기(ginput)3. 커스텀 곡선(spline) 만들기4.기본 Polynomial Regression 곡선 만들기5. 두 곡선의 Residual 비교하기 1. 원본 데이터 생성먼저 sin(x) + cos(x)에 약간의 노이즈를 추가해 실제 측정된 것 같은 데이터를 만든다.np.random.seed(1)x_data = np.linspace(0, 10, 400)y_data = np... 2025. 11. 24.
python dictionary unpacking의 모든 것(딕셔너리 unpacking, merge, 병합) 1. Passing Dictionary to a Functiondef introduce(name, age, city): print(f"이름: {name}, 나이: {age}, 도시: {city}")person = {"name": "John", "age": 25, "city": "New York"}# Unpack dictionary into function argumentsintroduce(**person)"""Name: John, Age: 25, City: New York""" 2. Merging Dictionaries dict_a = {"a": 1, "b": 2}dict_b = {"c": 3, "d": 4}merged = {**dict_a, **dict_b}print(merged)"""{'a'.. 2025. 8. 22.
[numpy.ma] Numpy로 array masking 하는 방법 오늘은 numpy masking 방법에 대해 알아본다.masking은 array에서 일부 element를 무시하는 역할을 한다. numpy array maskingnumpy에서 조건을 만족하지 않는 배열의 일부를 연산에서 제외시키거나 표현을 하고 싶지 않을 때 사용한다.indexing은 완전히 미충족 조건인 요소를 filter 하는 것이라면 masking은 가져가되 연산에만 사용하지 않는 것이다.import numpy as npdata = np.array([10, 20, 30, 40, 50])# 예: 30보다 큰 값은 무시 (마스크 처리)masked = np.ma.masked_greater(data, 30)print(masked) # [10 20 30 -- --] # 마스킹된 값은 --로 표시됨# .. 2025. 8. 4.
Pandas map() vs apply(lambda): Same Result, Different Paths When working with pandas, it’s common to see both map() and apply(lambda) used to transform a column. In many cases, they produce the same result — but they’re not exactly the same. This post shows an example where both give the same output, then explains how they differ and when to use each. 1. Example: Producing the Same Result import pandas as pddf = pd.DataFrame({ 'grade': ['A', 'B', 'C'.. 2025. 7. 10.
[python] 직관적으로 cumulative count 계산하기(ft. pandas, numpy) 어떤 카테고리 내에서 시간 순으로 나열되어 있는 데이터에서, 특정 신호 전까지 누적카운트를 쭉 나열하는 방법을 생각해볼 수 있다. 망상해본 것이다.(원래 망상하면서 이것 저것 구현해봐야 한다.) 오늘은 이와 비슷한? 학년-반 내에서 학생 번호가 쭉 있을 때, 결석 전까지 누적 count를 매겨보려고 한다. 글로만 보면 이해안가니까 바로 코드로 deep dive! 1. Library오늘은 numpy, pandas 이외에 dictionary를 편하게 쓸 defaultdict 빌트인 함수를 가져온다.from collections import defaultdictimport numpy as npimport pandas as pd 2. Data대충 알겠지 않는가? 5,6학년 내에 1,2,3반이 있는데 각 8번.. 2025. 3. 19.
explode의 이해: Pandas dataframe에서 list 원소를 펼치는 방법 데이터프레임 전처리를 하다 보면, 가끔 아래처럼 grade별로 number가 list로 묶여있는 데이터를 마주하게 된다.data = {'grade': [1, 2, 3], 'number': [[10, 20], [30], [40, 50, 60]]}df = pd.DataFrame(data)### grade number0 1 [10, 20]1 2 [30]2 3 [40, 50, 60]### 이런 경험이 없으면, long form으로 쫙 펼쳐서 보고 싶은데 참 난감하다. 이 경우 pandas에서는 어떻게 쉽게 해결할 수 있는지, 그리고 그것을 구현해 보는 시간을 갖도록 한다.(직접 해보는 이유는, 단순히 method 하나 들고와서 "띡" 해결하는 것이 아닌 처리 로직을 이해하기 위함이다.) 1. .. 2025. 3. 9.
반응형