Skip to main content

Machine Learning - Python Regression Training and Testing

Python Code -- Regression Training and Testing.

(https://www.youtube.com/watch?v=r4mwkS2T9aI&index=4&list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v)
In addition to this video, some implementation techniques are added.
Initially i tried with Python27, but it ended up with errors( Discussed below), so its recommended to use Python3
Open Command Window and install the following
  • C:\Python27\Scripts\pip install sklearn
  • C:\Python27\Scripts\pip install quandl
  • C:\Python27\Scripts\pip install scipy
  • C:\Python27\Scripts\pip install pyparsing
If not installed properly try this link and download right version of scipy depends on your Pyton27/34
Make sure you have installed this,
(But using python 27 is not preferable as it doesnt have timespace() function)
But it is better to install Anaconda3, (as it take care of installing all libraries no need to install manually )
https://www.continuum.io/downloads
after installing it, open cmd windows 
type: conda install quandl (other packages are already there)
Database used for our analysis is given below
This is added via quandl.get in the below program 
import pandas as pd
import quandl
import math, datetime
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
df=quandl.get('WIKI/GOOGL') #Quandl contains database for testing
df=df[['Adj. Open', 'Adj. High', 'Adj. Low', 'Adj. Close', 'Adj. Volume']]
df['HL_PCT']=(df['Adj. High'] - df['Adj. Close']) / df['Adj. Close'] * 100.0df['PCT_change']=(df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
df=df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
forecast_col='Adj. Close'df.fillna(-99999, inplace=True)
forecast_out = int(math.ceil(0.01*len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)
x=np.array(df.drop(['label'],1))
x=preprocessing.scale(x)
x=x[:-forecast_out]
x_lately=x[-forecast_out:]
df.dropna(inplace=True)
y=np.array(df['label'])
y=np.array(df['label'])
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x,y,test_size=0.2)
clf=LinearRegression()
clf.fit(x_train, y_train)
accuracy=clf.score(x_test,y_test)
forecast_set=clf.predict(x_lately)
print(forecast_set,accuracy,forecast_out)
df['Forecast']=np.nan
last_date=df.iloc[-1].name
last_unix=last_date.timestamp()
one_day=86400next_unix=last_unix+one_day
for i in forecast_set:
next_date=datetime.datetime.fromtimestamp(next_unix)
next_unix += one_day
df.loc[next_date]=[np.nan for _ in range(len(df.columns)-1)] + [i]
df['Adj. Close'].plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()







Microsoft Visual C++ Compiler for Python 2.7 








Here is the python code 





Comments

Popular posts from this blog

Digital Half Toning - Ordered Dithering - MATLAB Code Bayer/ Ulichney

Digital Half-toning is a technique to convert the gray scale / color image (in the range 0-255) to binary images (in the range 0-1)  that is useful for printing (especially black and white printers). Though at first glance dots are clearly visible, as Human Vision System is a low pass filter, when dots are printed closely and viewed from the specific distance it can be perceived as a continuous tone image (i.e. gray scale). In general, half toning techniques is widely used in newspapers, journals and magazines printing.  Halftoning algorithms aims for better #Image Quality #Low Complexity #Optimal ink usage Coming to the topic, Ordered dithering is one of the standard technique to produce halftone image from continuous (gray scale) images. Let us assume, the first block containing 'B' is an image matrix and the  'H' is the threshold matrix. And the process is simply a element wise (pixel wise) comparison; if element of B > H (g...

Image RGB to CMYK : Image Conversion - MATLAB Code (RGB to CMYK, CMYK to RGB)

MATAB use makecform and applycform functions to perform this color model conversion  Ref: makecform:  https://www.mathworks.com/help/images/ref/makecform.html Ref: applycform:  https://www.mathworks.com/help/images/ref/applycform.html RGB to CMYK: I_rgb = imread('lena_color.jpg'); %Read the color image  C = makecform('srgb2cmyk');     %srgb (standard rgb) to cmyk  I_CMYK= applycform(I_rgb,C); TO SAVE IN CMYK Format imwrite(I_CMYK, 'test.tiff'); USE IMFINFO to confirm the conversion imfinfo('test.tiff') the command gives the output ... Width: 512 Height: 512 BitDepth: 32 ColorType: 'CMYK' ... CMYK to RGB: I_CMYK = imread('lena_color.jpg'); %Read the color image  C = makecform('cmyk2srgb');     %srgb (standard rgb) to cmyk  I_rgb= applycform(I_CMYK, C);

Direct Binary Search Halftoning (DBS) - MATLAB Code (Efficient Direct Binary Search, Halftoning)

Direct binary approach is a heuristic optimization method and is proven to be very powerful to  obtain optimized binary patterns. The approach achieves the least minimum square error between the perceived halftone and original image, though swap and toggle operation. The swap operation consists of switching the current pixel with eight of its neighborhood pixels. Toggle operation is about switching the values between 0 to 1 or vice versa (as shown in Fig. below)      With each iteration, the perceived error starts to reduce and in finally a superior halftone quality is achieved. The method is computationally very expensive and difficult to implement in hardware. The code for Efficient DBS is provided in the link: (MATLAB Code) https://github.com/SankarSrin/EDBS-Halftoning