목록Machine Learning (13)
Hyun Seup Jo
In [621]: import numpy as np import matplotlib.pyplot as plt In [622]: x = np.arange(-10,10,1) # -10부터 10까지 1step씩 f_x = x ** 2 # -10 ~ 10까지의 X값을 제곱 plt.plot(x, f_x) plt.show() # y = ax^2 형태의 일반적인 이차함수 In [623]: x_new = 10 # 임의로 한 좌표 지정 derivative = [] # 미분 y = [] learng_rate= 0.1 # Alpha value for i in range(100): old_value = x_new # old_value == 10에서 시작 x_new = old_value - learng_rate *(2* old..
import numpy as np class LinearRegression(object): def __init__(self, fit_intercept=True, copy_X=True): self.fit_intercept = fit_intercept '''fit_intercept가 True일 경우: - Matrix X의 0번째 Column에 값이 1인 column vector를추가한다.''' self.copy_X = copy_X # X복사 self._coef = None # 가중치(계수)(w1) self._intercept = None # y절편(w0) y = (w1 * x) + w0 self._new_X = None def fit(self, X, y): # 데이터를 설정하고 X를 생성하여 weight m..
In [69]: import pandas as pd import numpy as np import matplotlib.pyplot as plt In [70]: test_df = pd.read_csv("./test.csv") # 생존여부 존재 X train_df = pd.read_csv("./train.csv") # 생존여부 존재 O In [71]: test_df.head(1) Out[71]: PassengerId Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked 0 892 3 Kelly, Mr. James male 34.50 0 0 330911 7.83 NaN Q In [72]: train_df.head(1) Out[72]: PassengerId S..

# 1 import numpy as np import pandas as pd pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) def get_rating_matrix(filename, dtype=np.float32): df = pd.read_csv(filename) return df.groupby(['source', 'target'])['rating'].sum().unstack().fillna(0) print(get_rating_matrix('movie_rating.csv')) target Just My Luck Lady in the Water ..
In [65]: import numpy as np import pandas as pd Data를 날려버리는 방법¶ In [66]: # Eaxmple from - https://chrisalbon.com/python/pandas_missing_data.html raw_data = {'first_name': ['Jason', np.nan, 'Tina', 'Jake', 'Amy'], 'last_name': ['Miller', np.nan, 'Ali', 'Milner', 'Cooze'], 'age': [42, np.nan, 36, 24, 73], '..

In [74]: import matplotlib.pyplot as plt import numpy as np In [75]: x = range(100) In [76]: y = [value** 2 for value in x] In [77]: plt.plot(x, y) Out[77]: [] In [78]: x = np.arange(100) In [79]: y = list(map(lambda f: f ** 2, x)) In [80]: plt.plot(x, y) Out[80]: [] In [81]: x_1 = range(100) y_1 = [np.cos(value) for value in x_1] plt.plot(x_1, y_1) Out[81]: [] In [82]: x_2 = range(100) y_2 = [n..
In [78]: from pandas import DataFrame, Series import numpy as np In [79]: df = DataFrame({'Points': [876,789,863,673,741], 'Rank': [1,2,2,3,3], 'Team': ['Riders','Riders','Devils','Devils','Kings'], 'Year': ['2014','2015','2014','2015','2014']}, columns = ['Points','Rank..
1 - Pandas¶ In [425]: import pandas as pd import numpy as np In [426]: data_url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data' #Data URL # data_url = './housing.data' #Data URL df_data = pd.read_csv(data_url, sep='\s+', header = None) #csv 타입 데이터 로드, separate는 빈공간으로 지정하고, Column은 없음 In [427]: df_data.head() # 처음 다섯줄을 출력 Out[427]: 0 1 2 ..

# 1 n의 제곱수로 2 dimentional array를 생성하는 ndarray def n_size_ndarray_creation(n, dtype=int): return np.array(range(n ** 2), dtype=dtype).reshape(n, n) print(n_size_ndarray_creation(3)) [[0 1 2] [3 4 5] [6 7 8]] 함수의 호출에서 n의 인자는 3으로 설정하고 return코드를 살펴보면 range(9)까지 즉 0 ~ 8까지의 array를 생성하며 reshape(3, 3)을 통해 3 * 3 크기의 matrix를 생성한다. # 2 shape이 지정된 크기의 ndarray를 생성, 이때 행렬의 element는 type에 따라 0, 1 또는 empty로 생성됨..
2주차 - Numerical Python¶ 1 - Array Creation¶ In [514]: import numpy as np In [515]: test_array = np.array(['1', '4', 5, 8], float) In [516]: print(test_array) [1. 4. 5. 8.] In [517]: type(test_array[3]) # 하나의 float 변수가 차지하는 메모리 공간 64bit = 8byte Out[517]: numpy.float64 2 - Shape¶ In [518]: test_array = np.array(['1', '4', 5, '8'], float) In [519]: print(test..