Sunday, October 28, 2018

Importing local files to Colaboratory

Firstly ,executing below cell should create an inline "Choose Files" button

from google.colab import files
uploaded = files.upload()

After selecting your file(s), uploaded will be a dictionary of keys (the file names) and values (the encoded file objects). To decode the files for a library such as Pandas, try

import pandas as pd
df_filename = pd.read_csv('filename.csv')


Or Upload from below navigation

Find indices of the max element of the array in a particular axis.

import numpy as np
  
# Working on 2D array
array = np.arange(12).reshape(3, 4)
print("INPUT ARRAY Values: \n", array)
  
# If No axis mentioned, so it works on entire array
print("\nMax element : ", np.argmax(array))
  
# returning Indices of the max element
# as per the indices
print("\nIndices of Max element on column: ", np.argmax(array, axis=0))
print("\nIndices of Max element on row: ", np.argmax(array, axis=1))

Python Code to convert or reshape 2D to 3D matrix array

from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data.shape)
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data.shape)

Output:

(3, 2)
(3, 2, 1)

Saturday, October 27, 2018

python code to convert data frame to multi dimensional array(matrix) and access its columns

import pandas as pd
import os

os.chdir("D:\\DigitRecog\\Kaggle\\all")

Images = pd.read_csv("train.csv")
type(Images)

Images = Images.values
y=Images[:,0]
y.shape[0]

Thursday, October 25, 2018

Save an Image to a folder

import cv2
import os
os.chdir("D:\\NeuralNetwork")
cv2.imwrite('test.png',img)

Wednesday, October 24, 2018

Viewing the Intensity matrix of an image

  1. from skimage import io, viewer
  2. img = io.imread('test.jpg', as_grey=True) # load the image as grayscale
  3. print 'image matrix size: ', img.shape # print the size of image
  4. print '\n First 5 columns and rows of the image matrix: \n', img[:5,:5]*255
  5. viewer.ImageViewer(img).show() # plot the image

Tuesday, October 23, 2018

Creating Matrix with random values


numpy.random.rand(d0, d1, ..., dn)

d0,d1,d2...dn : The dimensions of the returned array, should all be positive. 
If no argument is given a single Python float is returned.

Example:
>>> np.random.rand(3,2)

array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random



Monday, October 22, 2018

Max function in Python


np.max is just an alias for np.amax. This function only works on a single input array and finds the value of maximum element in that entire array (returning a scalar). Alternatively, it takes an axisargument and will find the maximum value along an axis of the input array (returning a new array).

>>> a = np.array([[1, 2, 6],
                  [2, 4, 1]])
>>> np.max(a)
6
>>> np.max(a, axis=0) # max of each column
array([2, 4, 6])


np.maximum will take two arrays and compute their element-wise maximum. Arrays should be compatible. Below is an example:

>>> b = np.array([3, 6, 1])
>>> c = np.array([4, 2, 9])
>>> np.maximum(b, c)
array([4, 6, 9])

Sunday, October 21, 2018

For Loop Example

# Program to iterate through a list using indexing

genre = ['popper', 'rocker', 'jazz built']

# iterate over the list using index
for i in range(len(genre)):
print("I like", genre[i])