Wednesday, 4 May 2022

Random Forest

Practical 5: Random forest model

 

#First, start with importing necessary Python packages −

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

 

#Next, download the iris dataset from its weblink as follows −

path = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

 

#Next, we need to assign column names to the dataset as follows −

headernames = ['sepal-length''sepal-width''petal-length''petal-width''Class']

 

#Now, we need to read dataset to pandas dataframe as follows −

dataset = pd.read_csv(path, names = headernames)

dataset.head()

 

#Data Preprocessing will be done with the help of following script lines.

X = dataset.iloc[:, :-1].values

y = dataset.iloc[:, 4].values

X

Y

#Next, we will divide the data into train and test split. The followingcode will split the dataset into 70% training data and 30% of testing data −

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30)

 

#Next, train the model with the help of RandomForestClassifier class of sklearn as follows −

from sklearn.ensemble import RandomForestClassifier

classifier = RandomForestClassifier(n_estimators = 50)

classifier.fit(X_train, y_train)

RandomForestClassifier(n_estimators=50)

#At last, we need to make prediction. It can be done with the help of following script −

y_pred = classifier.predict(X_test)

 

#Next, print the results as follows −

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

result = confusion_matrix(y_test, y_pred)

print("Confusion Matrix:")

print(result)

result1 = classification_report(y_test, y_pred)

print("Classification Report:",)

print (result1)

result2 = accuracy_score(y_test,y_pred)

print("Accuracy:",result2)

 

 

Building a Logistic Regression

  

Building a Logistic Regression


Create a logistic regression based on the bank data provided.

The data is based on the marketing campaign efforts of a Portuguese banking institution. The classification goal is to predict if the client will subscribe a term deposit (variable y).

Note that the first column of the dataset is the index.

Import the relevant libraries


import pandas as pd

import numpy as np

import statsmodels.api as sm

import matplotlib.pyplot as plt

import seaborn as sns

sns.set()

 

# this part not be needed after the latests updates of the library

from scipy import stats

stats.chisqprob = lambda chisq, df: stats.chi2.sf(chisq, df)

 

 

 Load the ‘Example_bank_data.csv’ dataset.

from google.colab import files

uploaded = files.upload()

raw_data = pd.read_csv('Example_bank_data.csv')

raw_data

 

We want to know whether the bank marketing strategy was successful, so we need to transform the outcome variable into 0s and 1s in order to perform a logistic regression.

# We make sure to create a copy of the data before we start altering itNote that we don't change the original data we loaded.

data = raw_data.copy()

 

# Removes the index column that came with the data

data = data.drop(['Unnamed: 0'], axis = 1)

 

# We use the map function to change any 'yes' values to 1 and 'no' values to 0. 

data['y'] = data['y'].map({'yes':1'no':0})

data

 

# Check the descriptive statistics

data.describe()

Declare the dependent and independent variables

 

y = data['y']

x1 = data['duration']

 

Simple Logistic Regression

x = sm.add_constant(x1)

reg_log = sm.Logit(y,x)

results_log = reg_log.fit()

 

# Get the regression summary

results_log.summary()

 

 

# Create a scatter plot of x1 (Duration, no constant) and y (Subscribed)

plt.scatter(x1,y,color = 'C0')

 

# Don't forget to label your axes!

plt.xlabel('Duration', fontsize = 20)

plt.ylabel('Subscription', fontsize = 20)

plt.show()

 

 

np.set_printoptions(formatter={'float'lambda x: "{0:0.2f}".format(x)})

#np.set_printoptions(formatter=None)

results_log.predict()

 

np.array(data['y'])

results_log.pred_table()

 

cm_df = pd.DataFrame(results_log.pred_table())

cm_df.columns = ['Predicted 0','Predicted 1']

cm_df = cm_df.rename(index={0'Actual 0',1:'Actual 1'})

cm_df

 

cm = np.array(cm_df)

accuracy_train = (cm[0,0]+cm[1,1])/cm.sum()

accuracy_train

 

Monday, 3 December 2018

Python List : Adding element to List

Adding an element to the list

We can add elements to the list using append, insert and extend functions.

#Initializing empty list
alist = [] 

Append function

It is used to add elements at the end of the list.
Append function takes only one argument at a time i.e using append function we can add only one element at a time.

#Adding element in the list using append function
alist.append(1)
alist.append(3)
alist.append(5)
print('List after addition of 3 elements :  ', alist ) 

Output :



Insert function

It is used to add an element at the specified index position of the list.
#Adding an element at a specific index using insert function. 
alist.insert(1,2)
print('List after performing insert operation :  ' , alist)
#Here we are adding element 2 at index position 1

Output :



Extend function

It is used to add a sequence of elements to the list.
This function adds elements at the end of a list.
Extend function takes a list of elements as the argument.
#Adding multiple elements to the list
alist.extend( [ 4 , 'python' , 8.97 , (91,90,89) ] )
print('List after performing extend operation :  ' , alist)
#Here we are adding elements 4 , ' python ',8.97 and tuple (91,90,89)

Output :



Python Lists : Creating List

List


The list is  simplest data structure in python which is used to store a collection of data.
The list is a collection of items like String, Numbers, Lists, Tuples, Dictionaries.
Lists are enclosed in square brackets [] .
Each value in the list is assigned to the index value.
An index value of the list is start from 0 .
Each item in list is separated by comma(,) .
List is Mutable data structure which means we can add , remove ,  replace , append the elements of list whenever we want.

Example : alist = [ 2 , 3 , 4 , 'a' , 7.67 , 'python' ]

How to create List...?

#Creating empty list
#Creating empty list means creating object of list data stucture with no values.
alist = []
print('Initialize empty list :  ' , alist )

#Creating a list of strings
alist = ['hii','this','is','Python']
print('List with string values :  ' , alist )

#Creating a list of integers
alist = [ 1 ,3, 5 , 7]
print('List with integer values :  ' , alist )
#You can also create a list with float , long , complex type integers

#Creating list having mixed type of values(stings , numbers)
alist = [ 345 , 'python' , 6.9836 , 218748682686836753656 , 'program' , 6+8j ]
print('List with mixed type of values :  ' , alist )

#Creating list with list type data structure
#List with lst type of items also called as nested lists or multidimensional list.
alist = [[3,4,5],['a','b','c'],[3.67,9,'hello']]
print('Multi-Dimensional List :  ' , alist )

#Creating list with the use of tuples
alist = [(4,5,6),('a','v','d'),(3,7,1)]
print('List with the use of tuples :  ' , alist )

#Creating list with the use od dictionaries
alist = [{1:'Python' , 2 :'Programming'},{'a' : 97 , 'b' :98 }]
print('List with the use of dictionaries :  ' , alist )

Output :


A list may contain duplicate values with their distinct positions .
List store its element according to the index positions so we can store the same element multiple index positions, this can duplicate that element in a list.
If the list contains duplicate elements then that duplicate elements have different index positions.

#Creating a list with duplicate values
alist = [ 'a', 1 ,3 , 3 , 7 ,'a' , 5 , 7]
print('List with duplicate values :  ' , alist )

Output :

In above example ,three elements of list have duplicates
element 'a' have two index positions i.e. 0 and 5 .
element 3 have two index positions i.e. 2 and 3 .
element 7 also havetwo index positions i.e. 4 and 7 .
Because of different index positioning these elements considered as distinct elements not as same element .

Saturday, 13 May 2017

Greatest number among 3 numbers which are user entered

#Greatest number among 3 numbers which are user entered

#Accepting 3 numbers from user
a=int(input('enter number:'))
b=int(input('enter number:'))
c=int(input('enter number:'))

# find which is greatest number
if a>b:
    if a>c:
        print(a,'is greatest number')
    else:
        print(c,'is greatest number')
else:
    if b>c:
        print(b,'is greatest number')
    else:
        print(c,'is greatest number')

OUTPUT:

Fibonacci series using for loop

#Fibonacci series using for loop

#Asking user that how much terms he want in the fibonacci series
n=int(input("How much terms you want:"))

#displaying fibonacci terms
a,b=0,1
print("fib series=\n",1)
for i in range(1,n):
    s=a+b
    print(s)
    a,b=b,s

OUTPUT:

Dictionary of Days and their Temperature

#Dictionary of Days and their Temperature

#defining an empty dictionary to add days and their temperature
d={}

#defining a list of name of days
days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

#take entry of temperature from user
for i in days:      
    temp=int(input("enter temperature of "+i+" :"))
    d[i]=temp

#to display the day and its temperature in the form of dictionary
print('DAY & TEMPERATURE')
print(d)

#find maximum temperature
tmax=max(d.values())
print('maximum temperature' , tmax)

OUTPUT: