รู้รึเปล่า? หุ้นตัวไหนน่าเสี่ยง น่าซื้อ… ก็สามารถใช้ Machine learning ทำนายได้นะ😉

Kulyarat L.
Sep 15 · 5 min read
Image for post
Image for post
Image for post
Image for post

Objective:

บทความนี้เป็นส่วนหนึ่งของ Data Science Essential ของ Botnoi Classroom หัวข้อ Trend forecasting

วิเคราะห์หุ้น GC-F (บริษัท โกลบอล คอนเน็คชั่นส์ จำกัด )(มหาชน)นั่นเอง!!

ทีนี้เราก็มาเริ่มกันเลยดีกว่า

Step 0 เริ่มจาก import library ก่อนเหมือนเดิมเลยค่ะ

# pandas and numpy are used for data manipulationimport pandas as pdimport numpy as np# matplotlib and seaborn are used for plotting graphsimport matplotlib.pyplot as plt%matplotlib inlineplt.style.use('seaborn-darkgrid')
# fix_yahoo_finance is used to fetch dataimport yfinance as yf

Step 1 Get data

# Read datadef getPrice(name,YearMonthDateStart,YearMonthDateEnd,auto_adjust=True):Dataframe = yf.download(name, YearMonthDateStart, YearMonthDateEnd, auto_adjust)return Dataframe
NameOfStock = 'GC=F'YearMonthDateStart ='2006-01-01'YearMonthDateEnd = '2016-12-31'GoldTest = getPrice(NameOfStock, '2006-01-01', '2016-12-31',auto_adjust=True)GoldTest
Image for post
Image for post
แล้วเราก็จะได้ตารางออกมาแบบนี้
Image for post
Image for post
จากรูปก็จะเห็เทรนด์ราคาคร่าว ๆ ของหุ้นตัวนี้

Step 2 Data Cleansing and Transformation

อันดับแรกมาเช็ค Data type กับ missing value กันก่อน

Image for post
Image for post
def Drop_DivSplit(dataframe):dataframe.drop('Dividends', axis=1, inplace=True)dataframe.drop('Stock Splits', axis=1, inplace=True)dataframe.drop('Adj Close', axis=1, inplace=True)return dataframe
Image for post
Image for post
ก็จะได้มาแบบนี้ ตอนนี้ก็ไม่มีสามคอลลัมน์ที่เราไม่ใช้แล้ว

2.1 Get Features

# Create get_X_y function to get dependent (y) and independent (X) variablesdef get_X_y(dataframe):# Extract time series variableX = dataframe.reset_index()[['Date']]X['Year'] = [X.iloc[i, 0].year for i in range(len(X))]X['Month'] = [X.iloc[i, 0].month for i in range(len(X))]X['Day'] = [X.iloc[i, 0].day for i in range(len(X))]X['EnglishDay'] = X.Date.dt.strftime('%a')# Set categorical variableX = X.astype({'Month':'category','Day':'category','EnglishDay':'category'}).set_index('Date')# Get OHLCX = X.merge(dataframe, left_index=True, right_index=True)y = dataframe[['NextDayClose']].copy()return X,y
Image for post
Image for post
ก็จะออกมาแบบนี้
# Create feature_engineer function to extract new featuresdef feature_engineer(dataframe, feat_eng=False):#Looking for 90 dayif feat_eng:# Create a feature from next day Closedataframe[f'NextDayClose'] = dataframe[['Close']].shift(periods=-1,fill_value=0)for i in range(1,91):# Create a feature from previous day HIGHdataframe[f'PreviousDayHigh{i}'] =  dataframe[['High']].shift(periods=i,fill_value=0)# Create a feature from previous day LOWdataframe[f'PreviousDayLow{i}'] = dataframe[['Low']].shift(periods=i, fill_value=0)# Create a feature from previous day Opendataframe[f'PreviousDayOpen{i}'] = dataframe[['Open']].shift(periods=i,fill_value=0)# Create a feature from previous day Closedataframe[f'PreviousDayClose{i}'] = dataframe[['Close']].shift(periods=i,fill_value=0) 
dataframe = dataframe[dataframe.PreviousDayHigh90!= 0 ]
dataframe = dataframe[:-1]return dataframe
def feature_diff(dataframe):dataframe[f'NextDayClose'] = dataframe[f'NextDayClose']-dataframe['Close']for i in range(1,91):# Create a feature from previous day HIGHdataframe[f'PreviousDayHigh{i}'] =  dataframe[f'PreviousDayHigh{i}']- dataframe['High']# Create a feature from previous day LOWdataframe[f'PreviousDayLow{i}'] =  dataframe[f'PreviousDayLow{i}']- dataframe['Low']# Create a feature from previous day Opendataframe[f'PreviousDayOpen{i}'] =  dataframe[f'PreviousDayOpen{i}']- dataframe['Open']# Create a feature from previous day Opendataframe[f'PreviousDayClose{i}'] =  dataframe[f'PreviousDayClose{i}']- dataframe['Close']return dataframe

2.2 Get one-hot encoder

def one_hot_encoder(dataframe):# Select category columnscat_df = dataframe.select_dtypes(include=['category']).columns.to_list()
# Convert to one-hot dataframeone_hot_df = pd.get_dummies(dataframe, columns=cat_df)return one_hot_df
Image for post
Image for post
หน้าตาประมาณนี้

2.3. Split train and test dataframe

# Create stock_train_test_split functiondef stock_train_test_split(dataframe, y, trainsize=0.8):import mathtrain_ratio = trainsizetrain_size = math.ceil(len(dataframe) * train_ratio)# Get train dataframeX_train = dataframe.iloc[:train_size, :]y_train = y.iloc[:train_size, :]# Get train dataframe# X_test = dataframe.iloc[train_size:, :]X_test = dataframe.copy()y_test = y.iloc[train_size:, :]# Drop label from independent variablecol_drop = ['OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME']X_train = X_train.drop(col_drop, axis=1)X_test = X_test.drop(col_drop, axis=1)return X_train, X_test, y_train, y_test

Step 3 Prediction model

3.1 Model

# Create model_fit function for setting prediction modeldef model_fit(X_train, y_train):
# import sci-kit learnfrom sklearn.linear_model import LinearRegression
# Model fitting with normalizationmodel = LinearRegression(normalize=True).fit(X_train, y_train)
return model

3.2 Error

def rmse(y_test, y_hat):# Root mean squared errorfrom sklearn.metrics import mean_squared_errorfrom math import sqrty_hat_test = y_hat[-len(y_test):]rms = sqrt(mean_squared_error(y_test, y_hat_test))return print('RMS = {}'.format(rms))
Image for post
Image for post
และนี่ก้คือค่า error ของโมเดลเรา

Step 4 Visualization

# Create visualize function to display resultdef visualize(X, y, y_hat, symbol, feat_eng=False):# Set figure sizeplt.figure(figsize=(10, 5))# Plot actual priceplt.plot(X.index, y, label='Actual Different Price')# Plot predicted priceplt.plot(X.index, y_hat, label='Predicted Different Price')# Set titleplt.title('Stock price: {} [{}]'.format(symbol, feat_eng) )# Set x-labelplt.xlabel('Period')# Set y-labelplt.ylabel('Price [THB]')# Plot gridplt.grid()# Display legendplt.legend()# Display graphplt.show()
Image for post
Image for post

botnoi-classroom

This publication consists of articles related to Data…

Kulyarat L.

Written by

botnoi-classroom

This publication consists of articles related to Data science and AI written from Botnoi’s data scientists and students.

Kulyarat L.

Written by

botnoi-classroom

This publication consists of articles related to Data science and AI written from Botnoi’s data scientists and students.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store