Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 29 July 2020

Longest word of a given String in Python

def longest_word(str):
words = str.split()
max_len = len(max(words, key=len))
return [word for word in words if len(word) == max_len]

print(longest_word("I have an interview Scheduled today in Bangalore"))
['interview', 'Scheduled', 'Bangalore']

print(longest_word("Do you want me to do something?"))
['something?']

Tuesday, 26 May 2020

NamedTuple Example in Python

import collections 
Movie = collections.namedtuple('Movie',['MovieID','Title','Genres'])  #NamedTuple Example


M = Movie('1','Toy Story (1995)',"Animation|Children's|Comedy") 

print(M[0])
print(M[1])
print(M.MovieID)
print(M.Title)
print(M.Genres)

1
Toy Story (1995)
1
Toy Story (1995)
Animation|Children's|Comedy


#NamedTuple Example : #1

import collections 
Movie = collections.namedtuple('Movie',['MovieID','Title','Genres']) 

def parseMovie(_row):
fields = _row.split("::")
movieid = (int) (fields[0])
title = fields[1]
genres = fields[2]
M = Movie(movieid,title,genres)
return M

print(parseMovie("1::Toy Story (1995)::Animation|Children's|Comedy"))

Answer:
Movie(MovieID=1, Title='Toy Story (1995)', Genres="Animation|Children's|Comedy")


#NamedTuple Example : #2
import collections 
from datetime import datetime

Rating = collections.namedtuple('Rating',['UserID','MovieID','Rating','Timestamp']) 

def parseRatingRecord(_row):
fields = _row.split("::")
userid = (int) (fields[0])
movieid = (int) (fields[1])
rating = (int) (fields[2])
_timestamp = datetime.fromtimestamp((int) (fields[3]))
_rating = Rating(userid,movieid,rating,_timestamp)
return _rating

print(parseRatingRecord("1::1193::5::978300760"))

Answer:
Rating(UserID=1, MovieID=1193, Rating=5, Timestamp=datetime.datetime(2001, 1, 1, 3, 42, 40))


#Example 3

import collections
User = collections.namedtuple("Users",["UserID","Gender","Age","Occupation","ZipCode"])

def parseUserRecord(_row):
fields = _row.split("::")
userid = (int)(fields[0])
gender= fields[1]
age = (int) (fields[2])
occupation = (int) (fields[3])
zipcode = (int) (fields[4])
_user = User(userid,gender,age,occupation,zipcode) 
return _user

print(parseUserRecord("3::M::25::15::55117"))            
                               
                               

Tuesday, 19 May 2020

StructField, StructType Example in PySpark. Create Dataframe using in memory object in PySpark

#Create in memory object in Pyspark and create dataframe using it and export into MySQL.

import pyspark.sql.types as st

data = [
    (101,'Sankaranarayanan','1976-04-20','M','Bangalore','Married'),
    (102,'Anbusudha','1979-07-22','F','Bangalore','Married'),
(103,'Rahul','1989-07-15','M','Manachai','Bachelor')
]

print(data)

[(101, 'Sankaranarayanan', '1976-04-20', 'M', 'Bangalore', 'Married'), (102, 'Anbusudha', '1979-07-22', 'F', 'Bangalore', 'Married'), (103, 'Rahul', '1989-07-15', 'M', 'Manachai', 'Bachelor')]

for i in data:
    print(i)
(101, 'Sankaranarayanan', '1976-04-20', 'M', 'Bangalore', 'Married')
(102, 'Anbusudha', '1979-07-22', 'F', 'Bangalore', 'Married')
(103, 'Rahul', '1989-07-15', 'M', 'Manachai', 'Bachelor')

person_schema = st.StructType([
st.StructField('SNo', st.IntegerType(), True),
    st.StructField('Name', st.StringType(), True),
    st.StructField('DOB', st.StringType(), True),
    st.StructField('Gender', st.StringType(), True),
    st.StructField('City', st.StringType(), True),
    st.StructField('MarritalStatus', st.StringType(), True)
])

print(person_schema)

StructType(List(StructField(SNo,IntegerType,true),StructField(Name,StringType,true),StructField(DOB,StringType,true),StructField(Gender,StringType,true),StructField(City,StringType,true),StructField(MarritalStatus,StringType,true)))

user_df = spark.createDataFrame(data, person_schema)
 
user_df.printSchema()

root
 |-- SNo: integer (nullable = true)
 |-- Name: string (nullable = true)
 |-- DOB: string (nullable = true)
 |-- Gender: string (nullable = true)
 |-- City: string (nullable = true)
 |-- MarritalStatus: string (nullable = true)

user_df.show(5)

+---+----------------+----------+------+---------+--------------+
|SNo|            Name|       DOB|Gender|     City|MarritalStatus|
+---+----------------+----------+------+---------+--------------+
|101|Sankaranarayanan|1976-04-20|     M|Bangalore|       Married|
|102|       Anbusudha|1979-07-22|     F|Bangalore|       Married|
|103|           Rahul|1989-07-15|     M| Manachai|      Bachelor|
+---+----------------+----------+------+---------+--------------+ 

#Write the dataframe content into MySQL table
user_df.write.format("jdbc").\
option("url", "jdbc:mysql://localhost:3306/school").\
option("driver", "com.mysql.jdbc.Driver").\
option("dbtable", "user_table").\
option("user", "root").\
option("password", "Studi0Plus").save()
Goto Mysql :


use school;
show tables;
select * from user_table;

# SNo, Name, DOB, Gender, City, MarritalStatus
'103', 'Rahul', '1989-07-15', 'M', 'Manachai', 'Bachelor'
'102', 'Anbusudha', '1979-07-22', 'F', 'Bangalore', 'Married'
'101', 'Sankaranarayanan', '1976-04-20', 'M', 'Bangalore', 'Married'

Friday, 15 May 2020

MatPlotLib - Chart Examples

import numpy as np
from matplotlib import pyplot as plt

x = np.arange(1,11)
x

array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])


y=2*x
y

array([ 2,  4,  6,  8, 10, 12, 14, 16, 18, 20])


plt.plot(x,y)
plt.show()



plt.plot(x,y)
plt.title("Line Plot Example")
plt.xlabel("x-label")
plt.ylabel("y-label")
plt.show()




plt.plot(x,y,color="g",linestyle=":",linewidth=2)
plt.title("Line Plot Example")
plt.xlabel("Sales")
plt.ylabel("Transactions")
plt.show()



x = np.arange(1,11)
y1 = 2 * x
y2 = 3 * x

plt.plot(x,y1,color="g",linestyle=":",linewidth=2)
plt.plot(x,y2,color="r",linestyle="-.",linewidth=3)
plt.title("Line Plot Example")
plt.xlabel("Sales")
plt.ylabel("Transactions")
plt.grid(True)
plt.show()



plt.subplot(1,2,1)
plt.plot(x,y1,color="g")
plt.subplot(1,2,2)
plt.plot(x,y2,color="r")
plt.show()


#Bar chart example

transDict = {"Jan":33000,"Feb":23800,"March":40000,"April":57900}
months = list(transDict.keys())
sales = list(transDict.values())

plt.bar(months,sales)
plt.show()




transDict = {"Jan":33000,"Feb":23800,"March":40000,"April":57900}
months = list(transDict.keys())
sales = list(transDict.values())

plt.barh(months,sales,color="g")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()



transDict = {"Jan":33000,"Feb":23800,"March":40000,"April":57900}
months = list(transDict.keys())
sales = list(transDict.values())

plt.bar(months,sales,color="r")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.grid(True)
plt.show()


#Scatter plot

x = [10,20,30,40,50,60,70,80,90]
y = [8,1,7,2,0,3,7,3,2]
plt.scatter(x,y)
plt.show()


x = [10,20,30,40,50,60,70,80,90]
y = [8,1,7,2,0,3,7,3,2]
plt.scatter(x,y,marker="*",c="orange",s=200)
plt.show()


x = [10,20,30,40,50,60,70,80,90]
y1 = [8,3,7,1,4,9,3,6,0]
y2 = [1,2,3,8,6,5,3,9,6]
plt.scatter(x,y1,marker="*",c="orange",s=200)
plt.scatter(x,y2,marker=".",c="red",s=300)
plt.show()



x = [10,20,30,40,50,60,70,80,90]
y1 = [8,3,7,1,4,9,3,6,0]
y2 = [1,2,3,8,6,5,3,9,6]

plt.subplot(1,2,1) #single row and 2 columns and left side
plt.scatter(x,y1,marker="*",c="orange",s=200)

plt.subplot(1,2,2) #single row and 2 columns and right side
plt.scatter(x,y2,marker=".",c="red",s=300)
plt.show()



x = [10,20,30,40,50,60,70,80,90]
y1 = [8,3,7,1,4,9,3,6,0]
y2 = [1,2,3,8,6,5,3,9,6]

plt.subplot(2,1,1) #2 rows single column top side
plt.scatter(x,y1,marker="*",c="orange",s=200)

plt.subplot(2,1,2) #2 rows single column bottom side
plt.scatter(x,y2,marker=".",c="red",s=300)
plt.show()


#histogram example

data = [1,3,3,3,3,9,9,5,4,4,8,8,8,6,7]
plt.hist(data)
plt.show()



#load data from csv and display histogram chart
import pandas as pd
df = pd.read_csv("E:\\PyExa\\iris.csv")

plt.hist(df["sepal.length"],bins=30,color="r")
plt.show()



import pandas as pd
df = pd.read_csv("E:\\PyExa\\iris.csv")

plt.hist(df["sepal.length"],bins=30,color="r")
plt.hist(df["petal.width"],color="green")
plt.show()

Introduction to Pandas - Sample Programs


#Series Example with 0,1,2,3,. are indexes
import pandas as pd
s1 = pd.Series([1,2,3,4,5,6,7,8,9,10])
print(s1)

0     1
1     2
2     3
3     4
4     5
5     6
6     7
7     8
8     9
9    10
dtype: int64


type(s1)

pandas.core.series.Series


#Series example with Custom indexes
import pandas as pd
s1 = pd.Series([86,63,85,81,90],index=["Tamil","English","Maths","Science","Social"])
print(s1)

Tamil      86
English    63
Maths      85
Science    81
Social     90
dtype: int64


#Passing dictionary object to the Series
#Keys of a given Dictionary will become Indexes
import pandas as pd
subjectDict = {"Tamil":85, "English":63, "Maths":85, "Science":81, "Social":90}
s1 = pd.Series(subjectDict)
print(s1)

Tamil      85
English    63
Maths      85
Science    81
Social     90
dtype: int64




# 'b' and 'd' are not there in the given indexes, so, NaN as the value assigned to them
import pandas as pd
s1 = pd.Series({"a":10,"c":30,"e":40},index=["b","c","d","a"])
print(s1)

b     NaN
c    30.0
d     NaN
a    10.0
dtype: float64



s1 = pd.Series([5,7,3,2,88,22,-1,0,33])
print(s1[3])

2

print(s1[:2])

0    5
1    7
dtype: int64

print(s1[-1:])

8    33
dtype: int64


print(s1[:6])

0     5
1     7
2     3
3     2
4    88
5    22
dtype: int64

#Arithmetic operations
s1 = pd.Series([10,20,30,40])
s2 = pd.Series([11,22,33,44])
s3 = s1 + s2
print(s3)

0    21
1    42
2    63
3    84
dtype: int64


s1 = pd.Series([11,66,77,55])
s2 = pd.Series([5,22,22,44])
s3 = s1 - s2
print(s3)


0     6
1    44
2    55
3    11
dtype: int64

print(s1+15)

0    26
1    81
2    92
3    70
dtype: int64



print(s2 ** 1.3)

0      8.103283
1     55.609563
2     55.609563
3    136.926807
dtype: float64


s1 = pd.Series([1,2,3])
s2 = pd.Series([6,7,8])
print(s1,s2)

0    1
1    2
2    3
dtype: int64 0    6
1    7
2    8
dtype: int64


print(s1+s2)

0     7
1     9
2    11
dtype: int64


print(s1*s2)

0     6
1    14
2    24
dtype: int64

print(s1-s2, s2-s1)

0   -5
1   -5
2   -5
dtype: int64 0    5
1    5
2    5
dtype: int64




#DataFrame Example

import pandas as pd
subjectDict = {"Subjects":["Tamil","English","Maths","Science","Social"],"Marks":[86,63,85,81,90]}
df = pd.DataFrame(subjectDict)
print(df)


  Subjects  Marks
0    Tamil     86
1  English     63
2    Maths     85
3  Science     81
4   Social     90




import pandas as pd
subjectDict = {"Names":["Arjun","Ram","Biswa","Kalai","Nila"],"Age":[78,37,88,43,93]}
df = pd.DataFrame(subjectDict)
print(df)


   Names  Age
0  Arjun   78
1    Ram   37
2  Biswa   88
3  Kalai   43
4   Nila   93


df = pd.read_csv("https://gist.githubusercontent.com/netj/8836201/raw/6f9306ad21398ea43cba4f7d537619d0e07d5ae3/iris.csv")
df.head()

df = pd.read_csv("E:\\PyExa\\iris.csv")
df.head()

sepal.length sepal.width petal.length petal.width variety
0 5.1 3.5 1.4 0.2 Setosa
1 4.9 3.0 1.4 0.2 Setosa
2 4.7 3.2 1.3 0.2 Setosa
3 4.6 3.1 1.5 0.2 Setosa
4 5.0 3.6 1.4 0.2 Setosa


df.tail()

sepal.length sepal.width petal.length petal.width variety
145 6.7 3.0 5.2 2.3 Virginica
146 6.3 2.5 5.0 1.9 Virginica
147 6.5 3.0 5.2 2.0 Virginica
148 6.2 3.4 5.4 2.3 Virginica
149 5.9 3.0 5.1 1.8 Virginica


print(df.shape)
(150, 5)  # 150 X 5 ==> 150 Rows X 5 columns


df.describe()

sepal.length sepal.width petal.length petal.width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000




df.iloc[0:3,0:2]
#1st 3 Rows and 1st 2 Columns

sepal.length sepal.width
0 5.1 3.5
1 4.9 3.0
2 4.7 3.2


df.iloc[0:4,0:4]
#1st 4 Rows and 1st 4 Columns


  sepal.length sepal.width petal.length petal.width
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2



df.loc[0:7,("petal.length","petal.width","variety")]
#1st 7 rows and specified columns

petal.length petal.width variety
0 1.4 0.2 Setosa
1 1.4 0.2 Setosa
2 1.3 0.2 Setosa
3 1.5 0.2 Setosa
4 1.4 0.2 Setosa
5 1.7 0.4 Setosa
6 1.4 0.3 Setosa
7 1.5 0.2 Setosa

#Drop variety column in the dataframe
s1 = df.drop("variety",axis=1)
print (s1.head())

sepal.length  sepal.width  petal.length  petal.width
0           5.1          3.5           1.4          0.2
1           4.9          3.0           1.4          0.2
2           4.7          3.2           1.3          0.2
3           4.6          3.1           1.5          0.2
4           5.0          3.6           1.4          0.2


#Drop  3 rows

s1 = df.drop([1,2,3],axis=0)
print(s1.head())


sepal.length  sepal.width  petal.length  petal.width variety
0           5.1          3.5           1.4          0.2  Setosa
4           5.0          3.6           1.4          0.2  Setosa
5           5.4          3.9           1.7          0.4  Setosa
6           4.6          3.4           1.4          0.3  Setosa
7           5.0          3.4           1.5          0.2  Setosa



df.mean()

sepal.length    5.843333
sepal.width     3.057333
petal.length    3.758000
petal.width     1.199333
dtype: float64


df.median()

sepal.length    5.80
sepal.width     3.00
petal.length    4.35
petal.width     1.30
dtype: float64


df.min()

sepal.length       4.3
sepal.width          2
petal.length         1
petal.width        0.1
variety         Setosa
dtype: object


df.max()

sepal.length          7.9
sepal.width           4.4
petal.length          6.9
petal.width           2.5
variety         Virginica
dtype: object


#applying user defined function

def half(s):
    return s*0.5

s1 = df[["sepal.length","petal.length"]].apply(half) #half is the udf
print(df[["sepal.length","petal.length"]].head())
print(s1.head())


   sepal.length  petal.length
0           5.1           1.4
1           4.9           1.4
2           4.7           1.3
3           4.6           1.5
4           5.0           1.4

   sepal.length  petal.length
0          2.55          0.70
1          2.45          0.70
2          2.35          0.65
3          2.30          0.75
4          2.50          0.70

#user defined function to double the dataframe values
def double_make(s):
    return s*2

print(df[["sepal.width","petal.width"]].head(5))
s1 = df[["sepal.width","petal.width"]].apply(double_make)
print(s1.head())


  sepal.width  petal.width
0          3.5          0.2
1          3.0          0.2
2          3.2          0.2
3          3.1          0.2
4          3.6          0.2
   sepal.width  petal.width
0          7.0          0.4
1          6.0          0.4
2          6.4          0.4
3          6.2          0.4
4          7.2          0.4


#grouping and counting of particular column

s1 = df["variety"].value_counts()
print(s1)

Virginica     50
Setosa        50
Versicolor    50
Name: variety, dtype: int64

#Sort order
s
df.sort_values(by="sepal.length").head()

sepal.length sepal.width petal.length petal.width variety
13 4.3 3.0 1.1 0.1 Setosa
42 4.4 3.2 1.3 0.2 Setosa
38 4.4 3.0 1.3 0.2 Setosa
8 4.4 2.9 1.4 0.2 Setosa
41 4.5 2.3 1.3 0.3 Setosa

Numpy Basics

#1 Dimensional Array

import numpy as np
n1 = np.array([1,2,3,4,5,6])
print(n1)
print(type(n1))

[1 2 3 4 5 6]
<class 'numpy.ndarray'>


#2 Dimensional Array

import numpy as np
n2 = np.array([[5,6,7,8,9,10],[50,51,52,53,54,55]])
print(n2)
print(type(n2))

[[ 5  6  7  8  9 10]
 [50 51 52 53 54 55]]
<class 'numpy.ndarray'>

#Initialize with Zeros - fill the array with zeros

import numpy as np
n3 = np.zeros(6)
print(n3)

[0. 0. 0. 0. 0. 0.]


import numpy as np
n4 = np.zeros((4,4))
print(n4)

[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
 
 
import numpy as np
n5 = np.zeros((5,3,2))
print(n5)

[[[0. 0.]
  [0. 0.]
  [0. 0.]]

 [[0. 0.]
  [0. 0.]
  [0. 0.]]

 [[0. 0.]
  [0. 0.]
  [0. 0.]]

 [[0. 0.]
  [0. 0.]
  [0. 0.]]

 [[0. 0.]
  [0. 0.]
  [0. 0.]]]
  
  
#Fill the array with a constant value

n5 = np.full((2,3),5)
print(n5)

[[5 5 5]
 [5 5 5]]
 
n6 = np.full((2,6),3)
print(n6)

[[3 3 3 3 3 3]
 [3 3 3 3 3 3]]
 
 
n7 = np.full((5),6)
print(n7)

[6 6 6 6 6]



#fill with a range (arange) 

n1 = np.arange(10,50,2.5)
print(n1)

[10 11 12 13 14 15 16 17 18 19]


n1 = np.arange(10,50,2.5)
print(n1)

[10.  12.5 15.  17.5 20.  22.5 25.  27.5 30.  32.5 35.  37.5 40.  42.5
 45.  47.5]
 
 
n1 = np.arange(10,50,7)
print(n1)

[10 17 24 31 38 45]


#fill with random integer values

n1 = np.random.randint(1,100,10)
print(n1)

[55 65 10 38 86 97 35 55 11 99]


n1 = np.random.randint(5,55,5)
print(n1)

[37 27 28 11 53]

#shape of the array

n1 = np.array([ [1,2,3],[4,3,2]  ])
print(n1)

(2,3)


n1 = np.array([ [1,2,3],[4,3,2] ,[53,2,3] ])
print(n1.shape)

(3, 3)



#stacking examples - vstack, hstack,column_stack
#vstack
n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
np.vstack((n1,n2))

array([[2, 6, 3],
       [6, 3, 8]])
   
n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
n3 = np.array([10,20,33])
np.vstack((n1,n2,n3))

array([[ 2,  6,  3],
       [ 6,  3,  8],
       [10, 20, 33]])


#hstack

n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
np.hstack((n1,n2))

array([2, 6, 3, 6, 3, 8])



n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
n3 = np.array([10,20,33])
np.vstack((n1,n2,n3))

array([[ 2,  6,  3],
       [ 6,  3,  8],
       [10, 20, 33]])
   
#column_stack

n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
np.column_stack((n1,n2))

array([[2, 6],
       [6, 3],
       [3, 8]])
   
n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
n3 = np.array([10,20,33])
np.column_stack((n1,n2,n3))


array([[ 2,  6, 10],
       [ 6,  3, 20],
       [ 3,  8, 33]])
   


n1 = np.array([2,6,3])
n2 = np.array([6,3,8])
n3 = np.array([10,20,33])
n4 = np.array([55,33,66])
np.column_stack((n1,n2,n3,n4))


array([[ 2,  6, 10, 55],
       [ 6,  3, 20, 33],
       [ 3,  8, 33, 66]])
   
   
#intersection

import numpy as np
n1  = np.array([1,2,3,4,5,6])
n2 = np.array([5,6,7,8,9])
np.intersect1d(n1,n2)

array([5, 6])


import numpy as np
n1  = np.array([1,2,3,4,5,6])
n2 = np.array([5,6,7,8,9])
np.intersect1d(n2,n1)

array([5, 6])

#difference

import numpy as np
n1  = np.array([1,2,3,4,5,6])
n2 = np.array([5,6,7,8,9])
np.setdiff1d(n1,n2)


array([1, 2, 3, 4])



import numpy as np
n1  = np.array([1,2,3,4,5,6])
n2 = np.array([5,6,7,8,9])
np.setdiff1d(n2,n1)

array([7, 8, 9])


import numpy as np
n1  = np.array([1,2,3,4,5,6])
n2 = np.array([1,2,3,4,5,6])
np.setdiff1d(n1,n2)

array([], dtype=int32)



#Sum
import numpy as np
n1  = np.array([10,20,30])
n2 = np.array([40,50,60])
np.sum([n1,n2])

210

np.sum([n1,n2],axis=0) #axis 0 : x axis - horizontal

array([50, 70, 90])


np.sum([n1,n2],axis=1) #axis 1 : y axis - vertical

array([ 60, 150])



import numpy as np
n1 = np.array([1,2,3,4,5,6])
n2 = np.array([7,8,9,10,11,12])
n3 = np.array([10,20,30,40,50,60])
n4 = np.array([100,200,300,400,500,600])
np.sum([n1,n2,n3,n4])

2388


np.sum([n1,n2,n3,n4],axis=0)

array([118, 230, 342, 454, 566, 678])


np.sum([n1,n2,n3,n4],axis=1)

array([  21,   57,  210, 2100])


Numpy Array Mathemetics
#Addition

import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 + 1
print(n1)

[56 34 45 67 23 12 78]


import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 + 10
print(n1)

[65 43 54 76 32 21 87]


import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 - 1
print(n1)

[54 32 43 65 21 10 76]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 - 10
print(n1)

[45 23 34 56 12  1 67]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 * 5
print(n1)

[275 165 220 330 110  55 385]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 * 55
print(n1)

[3025 1815 2420 3630 1210  605 4235]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 / 2.0
print(n1)


[27.5 16.5 22.  33.  11.   5.5 38.5]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 / 5
print(n1)

[11.   6.6  8.8 13.2  4.4  2.2 15.4]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 ** 3
print(n1)


[166375  35937  85184 287496  10648   1331 456533]



import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
n1 = n1 / (3 * 3)
print(n1)

[6.11111111 3.66666667 4.88888889 7.33333333 2.44444444 1.22222222 8.55555556]


#statistics functions
import numpy as np
n1 = np.array([55,33,44,66,22,11,77])
np.mean(n1)
np.sum(n1) / len(n1)  # mean is nothing but average

44.0

#middle element 
import numpy as np
n1 = np.array([11,77,33,44,66,22])
np.median(n1)

38.5

#standard deviation
import numpy as np
n1 = np.array([11,77,33,44,66,22])
np.std(n1)

23.262392157490787




#saving and Loading

import numpy as np
n1 = np.array([1,2,3,5,4,6,7,9,8,10,22])
np.save('result',n1)  #saving
 
n2 = np.load('result.npy') #loading

print(n2*10)

[ 10  20  30  50  40  60  70  90  80 100 220]
   

Thursday, 14 May 2020

Python OOPs - Class, Parameters, Constructor, Inheritance (Multi Level, Multiple)

#Create the first class in python

class Phone:
    def make_call(self):
        print("Let's make a phone call")
    
    def play_game(self):
        print("Let's play a game")
        
p1 = Phone()
p1.make_call()
p1.play_game()



#Adding parameters to a class method
class Phone:
    def set_color(self,color):
        self.color=color
        
    def set_cost(self,cost):
        self.cost=cost
        
    def show_color(self):
        return self.color
    
    def show_cost(self):
        return self.cost
    
    def play_game(self):
        print("Playing a game")
        
    def make_call(self):
        print("Making a call")
        
p1=Phone()
p1.set_color("Blue")
p1.set_cost(500)

p1.show_cost()

500

p1.show_color()

'Blue'


p1.play_game()
p1.make_call()

Playing a game
Making a call


#creating a class with constructor

class Employee:
    def __init__(self,name,age,gender,city,salary,pin):
        self.name=name
        self.age=age
        self.gender=gender
        self.city=city
        self.salary=salary
        self.pin=pin
        
    def show_details(self):
        print("Name : {}, Age : {}, Gender: {}, City: {}, Salary: {}, Pin: {}".format(self.name,self.age,self.gender,self.city,self.salary,self.pin))
        
        
e1 = Employee("Sankara",43,'M','Bangalore',67000,560093)
e1.show_details()

Name : Sankara, Age : 43, Gender: M, City: Bangalore, Salary: 67000, Pin: 560093




#Inheritance example:
#Multiple Inheritance
class Parent1:
    def assign_str1(self,str1):
        self.str1 = str1
        
    def show_str1(self):
        print(self.str1)
        
class Parent2:
    def assign_str2(self,str2):
        self.str2 = str2
    
    def show_str2(self):
        print(self.str2)
        
class Child(Parent1, Parent2):
    def assign_str3(self,str3):
        self.str3 = str3
        
    def show_str3(self):
        print(self.str3)

c1 = Child()
c1.assign_str1("I")
c1.assign_str2("Love")
c1.assign_str3("India")

c1.show_str1()

I

c1.show_str2()

Love

c1.show_str3()

India




#Multi Level inheritance

class Parent:
    def assign_name(self,name):
        self.name = name
        
    def show_name(self):
        return self.name
    
class Child(Parent):
    def assign_age(self,age):
        self.age=age
        
    def show_age(self):
        return self.age
    
class GrandChild(Child):
    def assign_gender(self,gender):
        self.gender=gender
        
    def show_gender(self):
        return self.gender
g1 = GrandChild()
g1.assign_name("Anbu")
g1.assign_age(5)
g1.assign_gender("F")

name = g1.show_name()
age = g1.show_age()
gender = g1.show_gender()

print("Name : {}, Age : {}, Gender : {}".format(name,age,gender))

Name : Anbu, Age : 5, Gender : F



Tuesday, 12 May 2020

Python with Mongo db - Sample Programs

python -m pip install pymongo


import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")

print(myclient.list_database_names())
['admin', 'config', 'local']



dblist = myclient.list_database_names()
if "school" in dblist:
  print("The database exists.")
  
mydb = myclient["school"]

for p in dblist:
    print(p)
    
admin
config
local


 
 
#Display all the databases.
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
for p in myclient.list_database_names():
    print(p)
    
admin
config
local
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")

dblist = myclient.list_database_names()
if "school" in dblist:
  print("The database : {} exists.".format("school"))
    
The database : school exists.
#Display all the collections (tables) in a database:

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
collist = mydb.list_collection_names()
for collectionName in collist:
    print(collectionName)
    
student
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
collist = mydb.list_collection_names()

if "student"  in collist:
        print("Collection student exists in school database")

Collection student exists in school database



#insert a document (Row) in a collection (table)
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

personDict = { "firstname":"Priya","lastname":"Balakumaran","city":"Kandanur" }

x = mycol.insert_one(personDict)
 
print(x.inserted_id) 


#insert multiple documents - Multi row insert
#autogenerated ids
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

mydocs = [
  { "firstname":"Ravi","lastname":"Rahul","city":"Pallathur"},
  { "firstname":"Siva","lastname":"Prasad","city":"Vadagudi"},
  { "firstname":"Arun","lastname":"Kumar","city":"Kanadukathan"},
  { "firstname":"Awesome","lastname":"Nator","city":"London"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


#insert multiple document with id specified

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

mydocs = [
  {"_id": 101,"firstname":"Kalai","lastname":"Selvi","city":"Aathalur"},
  {"_id": 102,"firstname":"Anna","lastname":"Malai","city":"Singapore"},
  {"_id": 103,"firstname":"Vanakkam","lastname":"da mappla","city":"Theni"},
  {"_id": 104 ,"firstname":"Anbu","lastname":"Sudha","city":"Bangalore"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


[101, 102, 103, 104]


#Search document in a collection

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

x = mycol.find_one()
print(x)
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}



#find : Selct all : Select * from doc



import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
docs = mycol.find()
for d in docs:
    print (d)
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}



#display specific columns

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

for x in mycol.find({},{ "_id": 0, "firstname": 1, "lastname": 1, "city":1 }):
  print(x)
  
  
  {'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}


#display only the last name:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

for x in mycol.find({},{ "_id": 0,"city":0,"firstname":0  }):
  print(x)
  
  {'lastname': 'Raman'}
{'lastname': 'Balakumaran'}
{'lastname': 'Rahul'}
{'lastname': 'Prasad'}
{'lastname': 'Kumar'}
{'lastname': 'Nator'}
{'lastname': 'Selvi'}
{'lastname': 'Malai'}
{'lastname': 'da mappla'}
{'lastname': 'Sudha'}



#Except id and City:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

for x in mycol.find({},{ "_id":0,"city":0 }):
  print(x)
  
  {'firstname': 'Raja', 'lastname': 'Raman'}
{'firstname': 'Priya', 'lastname': 'Balakumaran'}
{'firstname': 'Ravi', 'lastname': 'Rahul'}
{'firstname': 'Siva', 'lastname': 'Prasad'}
{'firstname': 'Arun', 'lastname': 'Kumar'}
{'firstname': 'Awesome', 'lastname': 'Nator'}
{'firstname': 'Kalai', 'lastname': 'Selvi'}
{'firstname': 'Anna', 'lastname': 'Malai'}
{'firstname': 'Vanakkam', 'lastname': 'da mappla'}
{'firstname': 'Anbu', 'lastname': 'Sudha'}


#Search specific city

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  {'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}





#Search city >= "S"

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

myquery = { "city": { "$gt": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}


#Regular Explression. city starts with 'S'


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
  print(x)
  
  {'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}


#Order by City Ascending

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find().sort("city")
for x in mydoc:
  print(x)
  
  
  {'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}




#Order by City descending

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$regex": "S" } }
mydoc = mycol.find().sort("city",-1)
for x in mydoc:
  print(x)



{'_id': ObjectId('5eba60012a9545fa868c9aaf'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': ObjectId('5eba60012a9545fa868c9aae'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab1'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'London'}
{'_id': ObjectId('5eba5eff2a9545fa868c9aa8'), 'firstname': 'Raja', 'lastname': 'Raman', 'city': 'Kottaiyur'}
{'_id': ObjectId('5eba5f522a9545fa868c9aac'), 'firstname': 'Priya', 'lastname': 'Balakumaran', 'city': 'Kandanur'}
{'_id': ObjectId('5eba60012a9545fa868c9ab0'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}


#Delete single doc

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
mycol.delete_one(myquery)



#Delete multiple docs

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }  # city >= "S"
x = mycol.delete_many(myquery)
print(x.deleted_count, " documents deleted.")

3  documents deleted.



#Delete all docs / rows in a collection

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }
x = mycol.delete_many({})
print(x.deleted_count, " documents deleted.")

6  documents deleted.


#Drop a collection

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
mycol.drop()




#Adding documents again

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

mydocs = [
  {"_id": 101,"firstname":"Kalai","lastname":"Selvi","city":"Aathalur"},
  {"_id": 102,"firstname":"Anna","lastname":"Malai","city":"Singapore"},
  {"_id": 103,"firstname":"Vanakkam","lastname":"da mappla","city":"Theni"},
  {"_id": 104 ,"firstname":"Anbu","lastname":"Sudha","city":"Bangalore"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

mydocs = [
  { "firstname":"Ravi","lastname":"Rahul","city":"Pallathur"},
  { "firstname":"Siva","lastname":"Prasad","city":"Vadagudi"},
  { "firstname":"Arun","lastname":"Kumar","city":"Kanadukathan"},
  { "firstname":"Awesome","lastname":"Nator","city":"London"}
]

x = mycol.insert_many(mydocs)

#print list of the _id values of the inserted documents:
print(x.inserted_ids)


#limit example.. display 3 records


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]

myresult = mycol.find().limit(3)

#print the result:
for x in myresult:
  print(x)
  
  {'_id': ObjectId('5eba65732a9545fa868c9acf'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba65732a9545fa868c9ad0'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba65732a9545fa868c9ad1'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}


#Update one record - Single record update
#London to Pudukkottai

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": "London" }
newvalues = { "$set": { "city": "Pudukkottai" } }
mycol.update_one(myquery, newvalues)

for x in mycol.find():
  print(x)


{'_id': ObjectId('5eba65732a9545fa868c9acf'), 'firstname': 'Ravi', 'lastname': 'Rahul', 'city': 'Pallathur'}
{'_id': ObjectId('5eba65732a9545fa868c9ad0'), 'firstname': 'Siva', 'lastname': 'Prasad', 'city': 'Vadagudi'}
{'_id': ObjectId('5eba65732a9545fa868c9ad1'), 'firstname': 'Arun', 'lastname': 'Kumar', 'city': 'Kanadukathan'}
{'_id': ObjectId('5eba65732a9545fa868c9ad2'), 'firstname': 'Awesome', 'lastname': 'Nator', 'city': 'Pudukkottai'}
{'_id': 101, 'firstname': 'Kalai', 'lastname': 'Selvi', 'city': 'Aathalur'}
{'_id': 102, 'firstname': 'Anna', 'lastname': 'Malai', 'city': 'Singapore'}
{'_id': 103, 'firstname': 'Vanakkam', 'lastname': 'da mappla', 'city': 'Theni'}
{'_id': 104, 'firstname': 'Anbu', 'lastname': 'Sudha', 'city': 'Bangalore'}


#Update multiple documents

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["school"]
mycol = mydb["student"]
myquery = { "city": { "$gt": "S" } }
newvalues = { "$set": { "city": "Pillaiyarpatti" } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, "documents updated.")


2 documents updated.

Monday, 11 May 2020

Python with MySQL database programs

install python connector

python -m pip install mysql-connector

#verify the mysql connectivity
import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd"
)

print(mydb)

<mysql.connector.connection_cext.CMySQLConnection object at 0x000000E2CA88AB70>
#create a database
#display the databases
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE School")
mycursor.execute("SHOW DATABASES")

for x in mycursor:
  print(x)
  
('employee',)
('information_schema',)
('mysql',)
('performance_schema',)
('sakila',)
('school',)
('sys',)
('world',)



import socket
socket.getaddrinfo('127.0.0.1', 3306)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'p@ssw0rd' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'p@ssw0rd';

#connect with School database 
#create a table
import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE Student (rollnumber int, firstname varchar(50), lastname varchar(50), city varchar(50))")




mycursor.execute("SHOW TABLES")

for x in mycursor:
  print(x)
  
('student',)

#Drop table
mycursor.execute("DROP TABLE Student")
for x in mycursor:
  print(x)
  
  
  
#create a table with auto increment column
import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)

mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE Student (rollnumber INT AUTO_INCREMENT PRIMARY KEY, firstname varchar(50), lastname varchar(50), city varchar(50))")
mycursor.execute("show tables")
for x in mycursor:
  print(x)
('student',)

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)

mycursor = mydb.cursor()

sql = "INSERT INTO student (firstname, lastname,city) VALUES (%s, %s, %s)"
val = ("Sankara","Narayanan","Pallathur")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

1 record inserted.


mycursor = mydb.cursor()

sql = "INSERT INTO student (firstname, lastname,city) VALUES (%s, %s, %s)"
val = [
  ('Prathap','Pothan','திà®°ுவனந்தப்புà®°à®®்'),
  ('Rajini','Kanth','Bangalore'),
  ('Vijay', 'Balani','Bangalore'),
  ('Anbu','Sudha','Melasivapuri')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

4 was inserted.


Get Inserted identity ids:

sql = "INSERT INTO student (firstname, lastname,city) VALUES (%s, %s, %s)"
val = ("Arun","Vijay","Chennai")
mycursor.execute(sql, val)

mydb.commit()

print("1 record inserted, ID:", mycursor.lastrowid)

1 record inserted, ID: 6
select from table:

mycursor.execute("SELECT * FROM student")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
  
(1, 'Sankara', 'Narayanan', 'Pallathur')
(2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')
(5, 'Anbu', 'Sudha', 'Melasivapuri')
(6, 'Arun', 'Vijay', 'Chennai')


#select columns
mycursor.execute("SELECT rollnumber as RollNumber, concat(firstname, ' ' ,lastname) as Name, city as City FROM student")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
(1, 'Sankara Narayanan', 'Pallathur')
(2, 'Prathap Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
(3, 'Rajini Kanth', 'Bangalore')
(4, 'Vijay Balani', 'Bangalore')
(5, 'Anbu Sudha', 'Melasivapuri')
(6, 'Arun Vijay', 'Chennai')


Fetch one record:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM Student")

myresult = mycursor.fetchone()

print(myresult)
myresult = mycursor.fetchone()

print(myresult)
myresult = mycursor.fetchone()

print(myresult)
myresult = mycursor.fetchone()

print(myresult)

(1, 'Sankara', 'Narayanan', 'Pallathur')
(2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')
#where condition
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student WHERE city ='Bangalore'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
  
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')


Like Condition:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student WHERE lastname LIKE '%an%'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
    (1, 'Sankara', 'Narayanan', 'Pallathur')
(2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')
  
  
  
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student WHERE lastname LIKE '%an'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
  (1, 'Sankara', 'Narayanan', 'Pallathur')
  (2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
  
  
 Prevent SQL Injection:
 
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student WHERE city = %s"
city = ("Bangalore", )
mycursor.execute(sql, city)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
  
  
  (3, 'Rajini', 'Kanth', 'Bangalore')
  (4, 'Vijay', 'Balani', 'Bangalore')
  
  
Order by example (ascending : default):
 
import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student order by city"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
  
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')
(6, 'Arun', 'Vijay', 'Chennai')
(5, 'Anbu', 'Sudha', 'Melasivapuri')
(1, 'Sankara', 'Narayanan', 'Pallathur')
(2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')



Order by descending example:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM student order by lastname desc"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
  
  
(6, 'Arun', 'Vijay', 'Chennai')
(5, 'Anbu', 'Sudha', 'Melasivapuri')
(2, 'Prathap', 'Pothan', 'திà®°ுவனந்தப்புà®°à®®்')
(1, 'Sankara', 'Narayanan', 'Pallathur')
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balani', 'Bangalore')



Delete a record:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()

sql = "DELETE FROM student WHERE city = %s"
adr = ("திà®°ுவனந்தப்புà®°à®®்", )
mycursor.execute(sql, adr)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")

1 record(s) deleted


#drop a table

mycursor = mydb.cursor()
sql = "DROP TABLE student"
mycursor.execute(sql)


#Drop Only if Exist

mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS student"
mycursor.execute(sql)



#update record with condition
#avoid sql injection

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)
mycursor = mydb.cursor()

sql = "UPDATE Student SET lastname = %s WHERE lastname = %s"
val = ("Balaji", "Balani")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

1 record(s) affected
#limit example

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)


mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Student LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
(1, 'Sankara', 'Narayanan', 'Pallathur')
(3, 'Rajini', 'Kanth', 'Bangalore')
(4, 'Vijay', 'Balaji', 'Bangalore')
(5, 'Anbu', 'Sudha', 'Melasivapuri')
(6, 'Arun', 'Vijay', 'Chennai')
  
  
#limit with offset example

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="p@ssw0rd",
  database="school"
)

mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Student LIMIT 3 offset 2")
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
  
  
(4, 'Vijay', 'Balaji', 'Bangalore')
(5, 'Anbu', 'Sudha', 'Melasivapuri')
(6, 'Arun', 'Vijay', 'Chennai')

Sunday, 10 May 2020

Numpy Sample Programs

import numpy
import numpy as np

print(np.__version__)
1.15.4


import numpy as np
arr = np.array([1,2,3,4,5,6,7,8,9,10])
print(type(arr))
print(arr)

for a in arr:
    print("a : {}".format(a))
<class 'numpy.ndarray'>
[ 1  2  3  4  5  6  7  8  9 10]
a : 1
a : 2
a : 3
a : 4
a : 5
a : 6
a : 7
a : 8
a : 9
a : 10



import numpy as np
arr = np.array([range(1,10,2)])

print(type(arr))

<class 'numpy.ndarray'>


print(arr)

[[1 3 5 7 9]]



for a in arr:
    print(a)
[1 3 5 7 9]


#Tuple to ndarray
myTuple = tuple((1,6,3,2,6,7,2,2))
import numpy as np
arr = np.array(myTuple)
print(arr)

[1 6 3 2 6 7 2 2]



1Dimension Arrray:

import numpy as np
arr1D = np.array(7)
print(arr1D.shape)

print(arr1D)
7

print(arr1D.ndim)
0

2D array:
arr = np.array([1,2,3,4,5,6])
print(arr.shape)

(6,)

print(arr2D.ndim)
2


arr2D = np.array([[1,2,3], [10,11,12]])
print(arr2D)



[[ 1  2  3]
 [10 11 12]]
(2, 3)

 


import numpy as np
arr3D=np.array([[  [1,2,3],[6,7,8]], [[5,3,2],[6,2,1] ] ])
print(arr3D)
print(arr3D.shape)
              
  
[[[1 2 3]
  [6 7 8]]

 [[5 3 2]
  [6 2 1]]]
(2, 2, 3)

print(arr3D.ndim)
3


import numpy as np

arr = np.array([5,7,3,2,6,33,2,1,2,3,4,5,6,7,8,9,2,3,5,6,2,12,1,3,5,3])
print(len(arr))
print(arr[0])
print(arr[1])
print(arr[-1])
print(arr[1:2])
print(arr[3:20])
print(arr[3:20:2])
print(arr[3:-1])
print(arr[3:-1:3])



26
5
7
3
[7]
[ 2  6 33  2  1  2  3  4  5  6  7  8  9  2  3  5  6]
[ 2 33  1  3  5  7  9  3  6]
[ 2  6 33  2  1  2  3  4  5  6  7  8  9  2  3  5  6  2 12  1  3  5]
[ 2  2  3  6  9  5 12  5]




import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0] + arr[-1])


5


import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('3rd element on 2st dim: ', arr[1, 2])


8


import numpy as np
arr = np.array([[1,2,3,4,5,3,2,3,4], [6,7,8,9,10,3,2,5,3]])
print('5th element on 1st dim: ', arr[0, 4])


5th element on 1st dim:  5



import numpy as np
arr = np.array([[[3,2,1], [6,-5,-4]], [[9,8,7], [5,4,3]]])
print(arr[0, 1, 2])
print(arr[1,0,1])


-4
8


import numpy as np
arr = np.array([[1,2,22,453,5], [3,7,8,32,3233]])
print('Last element from 2nd dim: ', arr[1, -1])
print('Last element from 1st dim: ', arr[0, -1])

Last element from 2nd dim:  3233
Last element from 1st dim:  5


import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
print(arr[:3])
print(arr[:7:2])
print(arr[:-1:2])
print(arr[::2])


[2 3 4 5]
[1 2 3]
[1 3 5 7]
[1 3 5]
[1 3 5 7]



import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
print(arr[0,2:-1])
print(arr[0,:4])
print(arr[1,-1])
print(arr[1,1:-1:2])


[7 8 9]
[3 4]
[1 2 3 4]
10
[7 9]



import numpy as np

arr = np.array([6,34,3,2])
print(arr.dtype)  #data type 

int64


arr = np.array(['silva', 'kalai', 'Raj'])
print(arr.dtype)

<U5


arr = np.array([1, 2, 3, 4], dtype='S') #defined data type
print(arr)
print(arr.dtype)

[b'1' b'2' b'3' b'4']
|S1

arr = np.array([1.3, 2.31, 3.12])
print(arr.dtype)  #float

float64


newarr = arr.astype('i') #float into integer
print(newarr)
print(newarr.dtype)

[1 2 3]
int32

arr = np.array([12, 0, 34])
print(arr)

[12  0 34]


newarr = arr.astype(bool) #integer into bool
print(newarr)
print(newarr.dtype)

[ True False  True]
bool




Copy Example:

import numpy as np

arr = np.array([6, 2, 7, 4, 5])
x = arr.copy()
arr[0] = 3333 #making changes in the source

print(arr)

[6, 2, 7, 4, 5]


print(x) #source changes not reflected here


[333, 2, 7, 4, 5]





import numpy as np

arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42 #source element got changed

print(arr)
print(x)  #target will reflect the source 


[42  2  3  4  5]
[42  2  3  4  5]



import numpy as np

arr = np.array(['a','e','i','o','u'])

x = arr.copy()
y = arr.view()

print(x.base)
print(y.base)

#The copy returns None.
#The view returns the original array.




import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)  # 2 D with 4 elements

(2, 4)


import numpy as np
arr = np.array([['a','b','c','d','e'], ['f','g','h','i','j'], ['k','l','m','n','o']  ])
print(arr.shape) # 3 D with 5 elements

(3, 5)




Reshape exa:
import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13,14,15,16])

newarr = arr.reshape(8, 2) #8 arrays 2 elements
print(newarr)

[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]
 [13 14]
 [15 16]]
 
 
newarr = arr.reshape(2, 8) #2 arrays 8 elements
print(newarr)

[[ 1  2  3  4  5  6  7  8]
 [ 9 10 11 12 13 14 15 16]]

newarr = arr.reshape(4, 4) #4 arrays 4 elements
print(newarr)

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]
 
 
1 D to 3 D:

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2) #2 arrays that contains 3 arrays, each with 2 elements:
print(newarr)

[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]
  
  
#flattening array
  
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

[[1 2 3]
 [4 5 6]]
 
 
newarr = arr.reshape(-1) #flattening
print(newarr)
[1 2 3 4 5 6]



Iteration - Looping:
--------------------

import numpy as np
arr = np.array(['a','b','c','d','e','f'])
for x in arr:
  print(x)


a
b
c
d
e
f

#iteration loop through 
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7,8,9]])
for x in arr:
  print(x)

[1 2 3]
[4 5 6]
[7 8 9]

#flattening
for x in arr:
for y in x:
    print(y)
            
1
2
3
4
5
6
7
8
9



import numpy as np

arr = np.array([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13,14, 15], [16, 17, 18]]  ])

for x in arr:
  print("x represents the 2-D array:")
  print(x)


x represents the 2-D array:
[[1 2 3]
 [4 5 6]]
 
x represents the 2-D array:
[[ 7  8  9]
 [10 11 12]]
 
x represents the 2-D array:
[[13 14 15]
 [16 17 18]]
 
 
 
import numpy as np

arr = np.array([ [[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13,14, 15], [16, 17, 18]]  ])

for x in arr:
  for y in x:
    for z in y:
      print(z)
  
#flattening
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


#nd iteration : nditer

import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

for x in arr:
print(x)

[[1 2]
 [3 4]]
[[5 6]
 [7 8]]
 
for x in np.nditer(arr):
  print(x)

1
2
3
4
5
6
7
8



#join / Concatenate


import numpy as np

arr1 = np.array([1, 2, 3,7,8,9])
arr2 = np.array([4, 5, 6,10,11,12])
arr = np.concatenate((arr1, arr2))
print(arr)

[ 1  2  3  7  8  9  4  5  6 10 11 12]



import numpy as np

arr1 = np.array([ [1, 2], [3,7],[8,9]])
arr2 = np.array([[4, 5], [6,10],[11,12]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)


[[ 1  2  4  5]
 [ 3  7  6 10]
 [ 8  9 11 12]]
 
 
 
import numpy as np
arr1 = np.array([1, 2, 3,4,5,6,7,8])
arr2 = np.array([9,10,11,12,13,14,15,16])
arr = np.stack((arr1, arr2), axis=1)
print(arr)


[[ 1  9]
 [ 2 10]
 [ 3 11]
 [ 4 12]
 [ 5 13]
 [ 6 14]
 [ 7 15]
 [ 8 16]]
 
 
import numpy as np
arr1 = np.array([1, 2, 3,4,5,6,7,8])
arr2 = np.array([9,10,11,12,13,14,15,16])
arr3 = np.array([17,18,19,20,21,22,23,24])
arr = np.stack((arr1, arr2,arr3), axis=1)
print(arr)

[[ 1  9 17]
 [ 2 10 18]
 [ 3 11 19]
 [ 4 12 20]
 [ 5 13 21]
 [ 6 14 22]
 [ 7 15 23]
 [ 8 16 24]]


split example:


import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16])
newarr = np.array_split(arr, 3)
print(newarr)


[array([1, 2, 3, 4, 5, 6]), array([ 7,  8,  9, 10, 11]), array([12, 13, 14, 15, 16])]



import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16])
newarr = np.array_split(arr, 4)
print(newarr)


[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([ 9, 10, 11, 12]), array([13, 14, 15, 16])]



import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16])
newarr = np.array_split(arr,8)
print(newarr)

[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8]), array([ 9, 10]), array([11, 12]), array([13, 14]), array([15, 16])] 


import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16])
newarr = np.array_split(arr,2)
print(newarr)
print(newarr[0])
print(newarr[1])

[array([1, 2, 3, 4, 5, 6, 7, 8]), array([ 9, 10, 11, 12, 13, 14, 15, 16])]


#split and slice 
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6,7,8,9,10,11,12,13,14,15,16])
newarr = np.array_split(arr,2)
print(newarr)

print(newarr[0])
print(newarr[1]) 

print(newarr[0][3:5])
print(newarr[1][3:5])


[array([1, 2, 3, 4, 5, 6, 7, 8]), array([ 9, 10, 11, 12, 13, 14, 15, 16])]
[1 2 3 4 5 6 7 8]
[ 9 10 11 12 13 14 15 16]
[4 5]
[12 13]



import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12], [13,14],[15,16],[17,18]])
newarr = np.array_split(arr, 3)
print(newarr)


[array([[1, 2],
       [3, 4],
       [5, 6]]), 
 array([[ 7,  8],
       [ 9, 10],
       [11, 12]]), 
 array([[13, 14],
       [15, 16],
       [17, 18]])]
   
#find the index position start with 0
   
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])  #0,1,2,3,4,5,6
x = np.where(arr == 4)
print(x)


(array([3, 5, 6]),)




#Even number's position index starts from 0

import numpy as np
arr = np.array([2222,1111,3333,4444,8888,223,224,999,1000])
x = np.where(arr%2 == 0)
print(x)

(array([0, 3, 4, 6, 8]),)


#Odd number's position index starts from 0
import numpy as np
arr = np.array([2222,1111,3333,4444,8888,223,224,999,1000])
x = np.where(arr%2 == 1)
print(x)


(array([1, 2, 5, 7]),)



#Find the indexes where the value 'n' should be inserted:


import numpy as np
arr = np.array([636, 173,285, 492])

x = np.searchsorted(arr, 177)
print(x)

2


x = np.searchsorted(arr, 77)
print(x)

0

x = np.searchsorted(arr, 377)
print(x)

3


x = np.searchsorted(arr, 500)
print(x)

4

x = np.searchsorted(arr, 1000)
print(x)

4


import numpy as np
arr = np.array([66, 17, 38, 69])
x = np.searchsorted(arr, 27, side='left')
print(x)

2

x = np.searchsorted(arr, 27, side='right')
print(x)

2



import numpy as np
arr = np.array([400, 200, 350, 100])
x = np.searchsorted(arr, [22, 444, 266,133,277,342,479])
print(x)


#sort
import numpy as np
arr = np.array([6,3,66,22,33,100,10,-1,-2,-3,0,34,1000,98])
print(np.sort(arr))

[  -3   -2   -1    0    3    6   10   22   33   34   66   98  100 1000]



import numpy as np
arr = np.array(["zeebra","elephant","lion","tiger","deer","cheetah","fox"])
print(np.sort(arr))

['cheetah' 'deer' 'elephant' 'fox' 'lion' 'tiger' 'zeebra']


import numpy as np
arr = np.array([True, False,False,True,True,True,False])
print(np.sort(arr))


[False False False  True  True  True  True]


#sort an array
import numpy as np
arr = np.array([[3, 2, 4], [5, 0, 1],[6,3,2], [5,2,1], [6,-1,-2],[0,1,-33] ])
print(np.sort(arr))

[[  2   3   4]
 [  0   1   5]
 [  2   3   6]
 [  1   2   5]
 [ -2  -1   6]
 [-33   0   1]]
 
 
Filter: 


import numpy as np
arr = np.array([41, 42, 43, 44])
x = arr[[True, True, True, True]]
print(x)

[41 42 43 44]

x = arr[[False,False,False,False]]
print(x)

[]

x = arr[[False,False,True,True]]
print(x)

[43 44]

x = arr[[True,True,False,False]]
print(x)

[41 42]



import numpy as np

arr = np.array([100,1000,800,200,700,500,600,300,200,0,900])
filter_arr = arr > 550
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

[False  True  True False  True False  True False False False  True]
[1000  800  700  600  900]



Even number filtering:

import numpy as np
arr = np.array([100,1000,800,200,700,500,600,300,200,0,900])
filter_arr = arr %2 == 0
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

[ True  True  True  True  True  True  True  True  True  True  True]
[ 100 1000  800  200  700  500  600  300  200    0  900]


Odd number filtering:

import numpy as np
arr = np.array([100,1000,800,200,700,500,600,300,200,0,900])
filter_arr = arr %2 == 1
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

[False False False False False False False False False False False]
[]

#odd number filtering
import numpy as np
arr = np.array([100,55,77,200,700,399,600,241,200,0,887])
filter_arr = arr %2 == 1
newarr = arr[filter_arr]
print(filter_arr)
print(newarr) 

[False  True  True False False  True False  True False False  True]
[ 55  77 399 241 887]


import numpy as np
arr = np.array([100,55,77,200,700,399,600,241,200,0,887])
filter_arr = arr %2 == 0
newarr = arr[filter_arr]
print(filter_arr)
print(newarr)

#even number filtering
[ True False False  True  True False  True False  True  True False]
[100 200 700 600 200   0]

Flume - Simple Demo

// create a folder in hdfs : $ hdfs dfs -mkdir /user/flumeExa // Create a shell script which generates : Hadoop in real world <n>...