Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Practical 1 (Write a program in python to display the execution of Lists - Demo the use of append(), insert(),extend(), remove(), pop().)
- A=[1,23,"Learning","Python"]
- print("All Elements in the list : ", A)
- #Append Function
- A.append(6)
- print("Appended List : ",A)
- #Insert Function
- A.insert(2,"Practical 1")
- print("Inserted Element : ",A)
- #Remove Function
- A.remove(23)
- print("Updated list after removal : ",A)
- #Pop Function (Delete value from specified index)
- A.pop(0)
- print("Updated list after pop : ",A)
- #Extend Function
- B=["Class","Sybsc A"]
- A.extend(B)
- print("Extended list : ",A)
- #Practical 2 (Write a program in python to display the execution of Tuples. Create a student attendance table to demo the use of tuples.)
- tup=()
- start=1
- while(start):
- name=input("Enter your Name : ")
- rollno=input("Enter your Roll No. : ")
- lec_attended=input("Enter Total Lectures Attended : ")
- student=("Name : "+name,"Roll No. : "+rollno,"Lec Attented : "+lec_attended)
- tup+=student
- req=int(input("To continue press 1,To view details press 2 : "))
- if(req==1):
- start=1
- else:
- print(tup)
- start=0
- #Practical 3.1 (Write a program in python to display the execution of Sets.)
- print("Mohit.K 1129\n")
- set1=set()
- set2=set()
- #To add values in set1 in the range of 5
- for i in range(5):
- set1.add(i)
- print("set1 : ",set1)
- #To add(insert) elements in set2 in range between 3 to 9
- for i in range(3,9):
- set2.add(i)
- print("set2 : ",set2)
- # Intersection of set1 and set2
- set3 = set1.intersection(set2)
- print("\nIntersection using intersection() function of set1 & set2")
- print("set3 : ",set3)
- # Intersection using "&" operator
- set4 = set1 & set2
- print("\nIntersection using '&' operator")
- print("set 4 : ",set4)
- #Union Function
- print("\nUnion of set1 & set2")
- set5 = set1.union(set2)
- print("set5 : ",set5)
- #Differnece between set1 & set2
- print("\nDiffernce between set1 & set2")
- set6 = set1.difference(set2)
- print("set6 : ",set6)
- #Difference using "-(minus)" operator
- print("\nDiffference using - operator")
- set7 = set1-set2
- print("set7 : ",set7)
- #Clearing the values
- print("\nClearing set1 & set2")
- set1.clear()
- print(set1)
- set2.clear()
- print(set2)
- #Practical 3.2 (Demo the use of sets to display employee records of a company.)
- start=1
- employeedetails = {""" """}
- while(start):
- empid = input("Enter Employee ID : ")
- name = input("Enter Employee Name : ")
- joiningdate = input("Enter joining date in dd/mm/yy formaat : ")
- dept = input("Enter Hiring department : ")
- annualsalary = input("Enter annual salary : ")
- employee = ("Empid "+empid,"Name "+name, "Joing Date "+joiningdate, " Department "+dept, "Annual salary " +annualsalary)
- print(employee)
- employeedetails.add(employee)
- req = int(input("Enter 1 to add more employee details else Enter 2 to display the info"))
- if(req == 1):
- start = 1
- else:
- print("All Employee details are \n")
- print(employeedetails)
- start = 0
- #Practical 4 (Write a program in python to display the execution of Dictionary. Create a dictionary set of and find the employee having highest and lowest salary.)
- dict1 = {"Name" : "Mohit","Age" : "20","Location" : "Mumbai"}
- dict2 = {"Height" : "5'11"}
- print("dict1 : ",dict1)
- print("dict2 : ",dict2)
- #To print keys
- print("\nPrinting Keys of dict1")
- print(dict1.keys())
- #To print values of key-value pair
- print("\nPrinting values of dict1")
- print(dict1.values())
- #To update data of values
- print("\n Updated Height of dict2")
- dict2["Height"]="6'0"
- print(dict2)
- #To append dict2 in dict1
- print("\nAppending dict2 in dict1")
- dict1.update(dict2)
- print(dict1)
- #To remove any data from dictonary
- print("\nRemoved Location")
- dict1.pop("Location")
- print(dict1)
- #To print min and max salary of an employee
- salary = {"Mohit" : 850000,"Hardik" : 780000,"Kaushik" : 650000}
- #To print Highest & Lowest Salary of an employee
- print("\nEmployee with Highest Salary : ",max(salary,key=salary.get))
- max_sal = max(salary,key=salary.get)
- print(salary.get(max_sal))
- print("\nEmployee with Lowest Salary : ",min(salary,key=salary.get))
- min_sal = min(salary,key=salary.get)
- print(salary.get(min_sal))
- #Practical 5 (Write a program in python to display the execution of Strings. Perform concat, iteration,membership and methods like upper(), join(), split(), find(), replace().)
- s0 = "Orange"
- s1 = "Hello this is python language"
- s2 = "Lets learn about strings in python"
- #CONCATENATING --> Adding two strings together
- print("----THIS IS CONCATENATION----")
- print(s1 + s2) #this is simply concatenating using + operator
- print(s1 + ',' + s2) #to make it look good
- #ITERAING --> Going through the string character by character
- print("----THIS IS ITERATING----")
- for i in s0:
- print(i)
- #upper() --> It will turn string to uppercase
- print("----UPPERCASE----")
- print(s0.upper())
- #lower() --> It will turn string to lowercase
- print("----LOWERCASE----")
- print(s0.lower())
- #len() --> To find length of string
- print("----LENGTH----")
- print("Length of s1 : ",len(s1))
- #join() --> It will join the other string
- str1 = '->'
- str2 = 'Hello'
- print("----JOINING----")
- print(str1.join(str2))
- string = 'Mohit'
- print( '' + '.'.join(string))
- #split() --> It will split the string word by word
- print("----SPLITING----")
- print(s1.split())
- print(s1.split(" ",2))
- #find() --> It will find the character or word and return its index
- print("----FINDING----")
- print(s1.find('python'))
- #replace() --> It will replace string with new string
- print("----REPLACING learn----")
- print(s2.replace('learn','study'))
- #Practical 6 (Compute a program in python using Matploitlib usage.)
- from google.colab import files
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- df = files.upload()
- df = pd.read_csv(r'Pokemon.csv') #usage of pandas
- df.head()
- df.drop('#', inplace=True, axis=1)
- df.head()
- df.shape #usage of numpy
- df.info()
- newdf = pd.DataFrame(df, columns=['Name','HP'])
- newdf.head()
- newdf.shape
- plt.rcParams["figure.figsize"] = (10, 4)
- plt.bar(newdf['Name'].head(), newdf['HP'].head())
- plt.show()
- # Line Plot
- import numpy as np
- import matplotlib.pyplot as plt
- x = np.arange(11)
- print(x)
- y = 2*x
- print(y)
- plt.plot(x,y)
- plt.title("Line Plot")
- plt.xlabel("X Label")
- plt.ylabel("Y Label")
- # Adding two line in same plot
- import numpy as np
- import matplotlib.pyplot as plt
- x = np.arange(11)
- y1 = 2*x
- y2 = 3*x
- print("\n","x : ",x,"\n\n","y1 : ",y1,"\n","y2 : ",y2)
- plt.plot(x,y1,color="g")
- plt.plot(x,y2,color="r")
- plt.title("Two Line Plot")
- plt.xlabel("X Label")
- plt.ylabel("Y Label")
- plt.grid("True")
- # Adding multiple plot in same screen
- import numpy as np
- import matplotlib.pyplot as plt
- x = np.arange(11)
- y1 = 2*x
- y2 = 3*x
- plt.subplot(3,3,1) #Subplot syntax (row,col,index of graph)
- plt.plot(x,y1,color='g',linestyle=':')
- plt.subplot(3,3,7)
- plt.plot(x,y2,color='b')
- #Bar Plot
- import numpy as np
- import matplotlib.pyplot as plt
- #define a dictionary for student marks
- student = {"Bob":87,"Matt":56,"Arpit":18}
- names = list(student.keys())
- marks = list(student.values())
- plt.bar(names,marks) #For Horizontal bar graph type "plt.barh(names,marks)"
- plt.title("Students Mark Graph")
- plt.xlabel("Student Names")
- plt.ylabel("Student Marks")
- #Scatter Plot
- import numpy as np
- import matplotlib.pyplot as plt
- x = np.arange(1,21)
- y = np.arange(6,26)
- y2 = np.arange(10,30)
- plt.scatter(x,y,marker="*",c="g",s=100) #marker means can add any symbol to show on graph,c=color,s=size
- plt.scatter(x,y2,marker="^",c="r",s=50)
- plt.plot()
- #hist graph
- import matplotlib.pyplot as plt
- data=[1,2,2,2,3,3,3,3,9,9,4,8,4]
- plt.hist(data,color='orange')
- #pie chart with numbers
- import matplotlib.pyplot as plt
- fruit=["Apple","Mango","Banana","Guava"]
- quantity=[64,66,22,78]
- plt.pie(quantity,labels=fruit)
- # with percentage
- import matplotlib.pyplot as plt
- quantity=[55,34,22,78,21]
- vegetable=["Potatu","Tomatu","Tindi","Lauki","Bindi 12 rupey"]
- plt.pie(quantity,labels=vegetable,autopct='%0.1f',colors=['Yellow','Green','Orange','Red','Blue'])
- plt.pie([1],colors=['black'],radius=0.4)
- import matplotlib.pyplot as plt
- x = [5,2,9,4,7]
- y = [10,6,8,4,2]
- plt.xlabel("x value")
- plt.ylabel("y value")
- # Bar Graph
- plt.bar(x,y)
- # Scatter Graph
- plt.scatter(x,y,marker="*",c="r",s=100)
- # Histogram Graph
- plt.hist(x,color="green")
- import matplotlib.pyplot as plt
- import pandas as pd
- import numpy as np
- df=pd.DataFrame(np.random.rand(10,5))
- Columns = ['A','B','C','D','E']
- df.plot.area()
- plt.legend(["A","B","C","D","E"])
- plt.show()
- # Practical 7 (Compute a program in python using Numpy usage.)
- import numpy as np
- # 1 Dimensional Array
- l = [1,9,8,5,3]
- npl = np.array(l)
- print("1 Dimensional Array :\n",npl)
- print("Matrix : ",npl.shape)
- print("Type of Datatype : ",npl.dtype)
- #Arthimetic Operations
- print("Addition : ",npl+10)
- print("Multiplication : ",npl*10)
- print("Subtraction : ",npl-10)
- print("Division : ",npl/10)
- print()
- # 2 Dimensional Array
- c = np.array([(1,2,3),(4,5,6)])
- print("2 Dimensional Array : \n",c)
- print("Matrix : ",c.shape)
- print()
- # 3 Dimensional Array
- d = np.array([(1,2,3),(4,5,6),(7,8,9),(10,11,12)])
- print("3 Dimensional Array : \n",d)
- print("Matrix : ",d.shape)
- print()
- print("Reshape into (3,4) : \n",d.reshape(3,4))
- print()
- # Horizontal Stack
- f = np.array([1,2,3])
- g = np.array([4,5,6])
- print("Horizontal Stack :\n",np.hstack(f))
- print()
- # Vertical Stack
- print("Vertical Stack :\n",np.vstack(g))
- print()
- # Random
- normal_array = np.random.normal(5,0.5,10)
- print("Random :\n",normal_array)
- print("Min : ",np.min(normal_array))
- print("Max : ",np.max(normal_array))
- print("Median : ",np.median(normal_array))
- print("std : ",np.std(normal_array))
- print()
- # Arrange
- print("Arrange :")
- print(np.arange(1,11))
- print(np.arange(1,14,4))
- print()
- # Slicing
- e = np.array([[1,2,3],[4,5,6]])
- print("Array :\n",e)
- print("\nPrinting 0th Index Element :\n",e[0])
- print("\nPrinting 1st Index Element :\n",e[1])
- #Practical 8 (Compute a program in python using Pandas usage.)
- import pandas as pd
- S = pd.Series([11, 28, 72, 3, 5, 8])
- S
- print(S.index)
- print(S.values)
- print("\n")
- #defining Series objects with individual indices:
- fruits = ['apples', 'oranges', 'cherries', 'pears']
- quantities = [20, 33, 52, 10]
- fruitdetails = pd.Series(quantities, index=fruits)
- print(fruitdetails)
- print("\n")
- #Creating Series Objects from Dictionaries
- cities = {"London": 12000,
- "Berlin": 14000,
- "Madrid": 45000,
- "Rome": 78050,
- "Paris": 46520,
- "Vienna": 78963,
- "Bucharest": 15302,
- "Hamburg": 78569,
- "Budapest": 25896,
- "Warsaw": 36584,
- "Barcelona": 45300,
- "Munich": 74500,
- "Milan": 62000}
- city_series = pd.Series(cities)
- print(city_series)
- my_cities = ["London", "Paris", "Zurich", "Berlin","Stuttgart", "Hamburg"]
- my_city_series = pd.Series(cities,index=my_cities)
- print("\n")
- print(my_city_series)
- print("\n")
- print(my_city_series.isnull())
- print("\n")
- print(my_city_series.notnull())
- print("\n")
- #Filtering out Missing Data
- print(my_city_series.dropna())
- print("\n")
- #Filling in Missing Data
- print(my_city_series.fillna(0))
- print("\n")
- #Fill some appropriate values for missng Cities
- missing_cities = {"Stuttgart":50939, "Zurich":38884}
- print(my_city_series.fillna(missing_cities))
- print("\n")
- #Since we had nan values integers are converted into float
- my_city_series = my_city_series.fillna(0).astype(float)
- print(my_city_series)
- import pandas as pd
- years = range(2014, 2018)
- shop1 = pd.Series([2409.14, 2941.01, 3496.83, 3119.55], index=years)
- shop2 = pd.Series([1203.45, 3441.62, 3007.83, 3619.53], index=years)
- shop3 = pd.Series([3412.12, 3491.16, 3457.19, 1963.10], index=years)
- shops_df = pd.concat([shop1, shop2, shop3], axis=1)
- shops_df
- #update column names
- cities = ["Mumbai", "Pune", "Nashik"]
- shops_df.columns = cities
- shops_df
- print("\n")
- # alternative way: give names to series:
- shop1.name = "Mumbai"
- shop2.name = "Pune"
- shop3.name = "Nashik"
- print(shops_df)
- print("\n")
- shops_df2 = pd.concat([shop1, shop2, shop3], axis=1)
- print(shops_df2)
- print("\n")
- print(type(shops_df))
- #DataFrames from Dictionaries
- cities2 = {"name": ["London", "Berlin", "Madrid", "Rome",
- "Paris", "Vienna", "Bucharest", "Hamburg",
- "Budapest", "Warsaw", "Barcelona",
- "Munich", "Milan"],
- "population": [8615246, 3562166, 3165235, 2874038,
- 2273305, 1805681, 1803425, 1760433,
- 1754000, 1740119, 1602386, 1493900,
- 1350680],
- "country": ["England", "Germany", "Spain", "Italy",
- "France", "Austria", "Romania",
- "Germany", "Hungary", "Poland", "Spain",
- "Germany", "Italy"]}
- city_frame = pd.DataFrame(cities2)
- city_frame
- #Retrieving the Column Names
- city_frame.columns.values
- #Providing custom index
- ordinals = ["first", "second", "third", "fourth",
- "fifth", "sixth", "seventh", "eigth",
- "ninth", "tenth", "eleventh", "twelvth",
- "thirteenth"]
- city_frame = pd.DataFrame(cities2, index=ordinals)
- city_frame
- #Rearranging the Order of Columns
- city_frame = pd.DataFrame(cities2,columns=["name","country","population"])
- city_frame
- #making Existing Column as the Index of a DataFrame
- city_frame = pd.DataFrame(cities2,columns=["name", "population"],index=cities2["country"])
- city_frame
- #Accessing Rows via Index Values
- city_frame = pd.DataFrame(cities2,columns=("name", "population"),index=cities2["country"])
- print(city_frame.loc["Germany"])
- #extracting rows by chosen more than on index labels
- print(city_frame.loc[["Germany", "France"]])
- #select pandas DataFrame rows based on conditions
- condition = city_frame.population>2000000
- condition
- print(city_frame.loc[condition])
- condition1 = (city_frame.population>1500000)
- condition2 = (city_frame['name'].str.contains("m"))
- print(city_frame.loc[condition1 & condition2])
- #Adding Rows to a DataFrame
- milan = ['Milan', 1399860]
- city_frame.iloc[-1] = milan
- city_frame.loc['Switzerland'] = ['Zurich', 415215]
- city_frame
- #Practical 9 (Implement the PCA using Python.)
- import pandas as pd
- import matplotlib.pyplot as plt
- from sklearn.datasets import load_breast_cancer
- Cancer = load_breast_cancer()
- Cancer.keys()
- #print(Cancer['DESCR'])
- df = pd.DataFrame(Cancer['data'],columns=Cancer['feature_names'])
- print(df)
- from sklearn.preprocessing import StandardScaler
- Scaler = StandardScaler()
- Scaler.fit(df)
- Scaled_data = Scaler.transform(df)
- print(Scaled_data)
- from sklearn.decomposition import PCA
- pca = PCA(n_components=3)
- pca.fit(Scaled_data)
- x_pca=pca.transform(Scaled_data)
- x_pca.shape
- print(x_pca)
- #2D
- plt.figure(figsize=(8,6))
- plt.scatter(x_pca[:,0],x_pca[:,1])
- c=Cancer['target']
- plt.xlabel("1st Principle Component")
- plt.ylabel("2nd Principle Component")
- #3D
- from mpl_toolkits.mplot3d import Axes3D
- fig = plt.figure(figsize=(10,10))
- axis = fig.add_subplot(111,projection='3d')
- axis.scatter(x_pca[:,0],x_pca[:,1],x_pca[:,2],c=Cancer['target'],cmap='plasma')
- axis.set_xlabel("1st PC",fontsize=10)
- axis.set_ylabel("2st PC",fontsize=10)
- axis.set_zlabel("3st PC",fontsize=10)
- #Check Variance Ratio
- print(pca.explained_variance_ratio_)
- #Practical 10 (Consider a set of data points and train your model in python. The model should have 80% training data and 20% of test data.)
- import pandas as pd
- from sklearn.datasets import load_iris
- from sklearn.model_selection import train_test_split
- iris_data = load_iris()
- iris_data
- iris_data.keys()
- df = pd.DataFrame(iris_data.data, columns = iris_data.feature_names)
- df
- training_data, testing_data = train_test_split(df, test_size=0.2)
- print(training_data)
- print(testing_data)
- print(f"No. of training examples : {training_data.shape[0]}")
- print(f"No. of testing examples : {testing_data.shape[0]}")
- training_data.head()
- #Practical 11 (Implement the Regression algorithm: Linear Regression in python.)
- from google.colab import files
- import pandas as pd
- import matplotlib.pyplot as plt
- from sklearn.model_selection import train_test_split
- #df = files.upload()
- df = pd.read_csv('Salary_Data.csv')
- df
- x = df.iloc[:,0].values #input
- y = df.iloc[:,1].values #output
- print(x.shape)
- print(y.shape)
- x = x.reshape(-1,1)
- y = y.reshape(-1,1)
- print(x.shape)
- print(y.shape)
- x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2,random_state = 0)
- print("x Training:",x_train)
- print(x_train.shape)
- print("\nx Testing:",x_test)
- print(x_test.shape)
- print("y Training:",y_train)
- print(y_train.shape)
- print("\ny Testing:",y_test)
- print(y_test.shape)
- plt.scatter(x_train,y_train,color = 'r',marker = '+')
- plt.xlabel("Years Experience")
- plt.ylabel('Salary')
- plt.title('Employee Details')
- plt.show()
- from sklearn.linear_model import LinearRegression
- reg = LinearRegression()
- reg.fit(x_train,y_train)
- reg.coef_
- reg.intercept_
- y_pred = reg.predict(x_test)
- print(y_pred)
- print(y_test)
- print(y_pred)
- print(y_test)
- plt.scatter(x_train,y_train,color = 'r',marker = '+')
- plt.xlabel("Years Experience")
- plt.ylabel('Salary')
- plt.plot(x_train,reg.predict(x_train))
- plt.title('Employee Details')
- plt.show()
- import matplotlib.pyplot as plt
- import numpy as np
- from sklearn import datasets, linear_model
- from sklearn.model_selection import train_test_split
- boston = datasets.load_boston()
- x = boston.data
- y = boston.target
- x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.4,random_state = 0)
- reg = linear_model.LinearRegression()
- reg.fit(x_train, y_train)
- print("Coefficient : ",reg.coef_)
- print("Variance Score :",format(reg.score(x_test,y_test)))
- plt.scatter(reg.predict(x_train),reg.predict(x_train) - y_train, color = "red", s =10, label = 'train data')
- plt.scatter(reg.predict(x_test),reg.predict(x_test) - y_test, color = "green", s =10, label = 'test data')
- plt.hlines(y = 0,xmin = 0,xmax = 50, linewidth = 2)
- plt.legend(loc = 'upper right')
- plt.title('Residual Errors')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement