Download YouTube videos in Python

Downloading videos from youtube is easier with python

Requirements: Python, youtube_dl library

Here’s the code,save it as .py and run it.

11.png

Enter URL, with in ” __ ”

78

Enter, and get link to download video

789.png

Posted in Programming, Uncategorized | Tagged | Leave a comment

Automate browsing for stock values with Python

Lets automate browsing for stock values from yahoo finance

Requirements: Python, urllib2 library,

1)The Yahoo finance stock value of apple

10.png

2) URL, hints that searching various stock values in yahoo finance website can be automated.

URL of apple stock: http://finance.yahoo.com/q?s=AAPL
URL of google stock: http://finance.yahoo.com/q?s=GOOGL

Therefore url search can be automated by changing code (AAPL, GOOGL) to a string that has list of company code.

3) Now in web-designing, each value or each text displayed in website has an unique id and code. If that can be fished out, and given to python to fish out stock value with respect to URL, automation of searching website and getting list of various company stock values is possible.

2.png

4) Armed with unique id of stock value, and unique URL id, lets write a program that give list of stock values. Get company list from NASDAQ website

3.png

and save only the company codes in the downloaded excel file, to a text file and move it to working directory.

5.png

5) Back-end work is done, and only coding is left

Here is the code

6.png

We wrote a code for generating list of first 5o companies stock values,

Like wise, to get list of all companies change

code as

i=0

while i <len(newstocklist)

6)  list of 5o companies stock values

7.png

 

Like wise, Google stock, weather list, can be automated.

 

 

Posted in Programming | Tagged , | Leave a comment

Getting List of pdfs in a folder with Python

Sometimes, in research, one might rename pdf name as per their comfort, and when trying to review bulky amount of pdfs, it becomes difficult to identify the pdf, copy the name of pdf, copy multiple file names together,

Here is a short code which should make things easy

  1. Suppose i have tons of file in googledrive, I need to get a list of pdf i have, to share with my friend or prof. I might also need to copy all the file names together. Traditionally i should have to go and copy each and every file name individually and paste it on note/word and save it.
  2. With python things get easy.

9

3. Run this code and you get list of pdfs you have in directed folder

10

 

Posted in Programming, Uncategorized | Tagged , , | Leave a comment

Export numpy array to excel in Python

It is always easy to manipulate data on numpy array rather than dataframe. If the data you generated in array has to be exported to excel??? How to do this?

7.png

Fig1: Array of data

Python offers lot of excel libraries, but xlxswriter is good among them

Step 1)

Import xlsxwriter, Create new workbook, in this case my workbook name is “Expenses01”.

Initiate row and column

Worksheet.write function, writes or copying data cell by cell, column by column and then row by row

5.png

Check your directory for the new excel file, open it and check whether its copied or not.

6

Posted in Programming, Uncategorized | Tagged , , | Leave a comment

Pdf_Merge in Python

Merging pdf can be done easily online, but online websites have a total file size limit of 25MB. That might undermine our needs.

IN python pdf merging can be done much much easier,

All one needs is

  • Python
  • PyPDF2 library

Step 1) Save the following code as .py file

2

Step 2)

Copy all the pdfs to merged in C:\folder (in case you want to run .py file)

Copy in any folder and change directory in .py code

1

Step 3) Run the .py file in either python or Anaconda Prompt

3.png

Step 4) With a fraction of seconds you should see the code run, and message that “pdfs are merged”. The new merged pdf file will be saved at the same directory.

4

 

 

Posted in Programming, Uncategorized | Tagged , , | Leave a comment

Machine learning with KNN classifier

KNN is important classifier used in machine learning t for classifying the data.

  • Language used: Python,
  • Library used: Scikit, numpy, pandas, matplot
  • Interface: Anaconda Jupyter
  1. Save the data file in either excel or csv format. Re-direct python directory to file location.

1

2.  Import file and import required libraries, Identify the key features and classes in your data-set, and scatter plot to check their co-variance.

2.png

123.png

3. The output of print (df)

3

4. Initiate X array for the feature data-set and Y array for class data-set.

5

6

_________________________________________________________________

5. CASE I: In case I we shall use the entire data in data-set for training and testing again with the same data-set. Import KNN classifier and metrics. Calculate accuracy of prediction.

7.png

6.The net accuracy of training and testing on the same data.

8

______________________________________________________________

7. CASE II: In case II we shall use the divide data-set into training and testing data-sets. Import KNN classifier,confusion matrix and metrics. Give test-size and random state as per required. Calculate accuracy of prediction.

9.png

8. Confusion matrix and net accuracy calculated where the training and testing is done on split data.

10.png

11.png

figure_1-1.png

________________________________________________________

9. CASE III: In case III we shall use the cross-validation  training and testing. Import KNN classifier and metrics. Give test-size and random state as per required. Apart from accuracy score, cross-validation accuracy score, precision score, recall score, F1 score is also important parameters to assess the machine learned classifier.

16.png

_________________________________________________________________

10. Many times it a question as to use which K value. Use the below code to figure out optimum value of k-value that gives maximum accuracy in predicting.

14.png

optimum k value.png

______________________________________________________

 

 

 

 

 

 

 

 

 

Posted in Machine Learning, Uncategorized | Tagged , , | 2 Comments

KNN classifiers – Python @scikit learn

file-page1file-page2file-page3

Posted in Programming, Uncategorized | Leave a comment