0% found this document useful (0 votes)
5 views22 pages

Fds Program

The document outlines various exercises involving the use of Python libraries such as NumPy, Pandas, and Scikit-learn for data manipulation and analysis. It includes examples of creating arrays, performing array slicing, creating dataframes, reading data from files, and conducting univariate and multivariate analyses on diabetes datasets. Additionally, it covers linear and logistic regression applications, as well as plotting functions for data visualization.

Uploaded by

anypropose7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Fds Program

The document outlines various exercises involving the use of Python libraries such as NumPy, Pandas, and Scikit-learn for data manipulation and analysis. It includes examples of creating arrays, performing array slicing, creating dataframes, reading data from files, and conducting univariate and multivariate analyses on diabetes datasets. Additionally, it covers linear and logistic regression applications, as well as plotting functions for data visualization.

Uploaded by

anypropose7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link].

:1 DOWNLOAD, INSTALL AND EXPLORE THE FEATURES OF NUMPY, SCIPY,


JUPYTER, STATSMODELS AND PANDAS PACKAGES.

PROGRAM:

import numpy as np arr=[Link]([[1,2,3],[4,2,5]])


print(“Array is of type: “ type(arr))
print(“No. of dimensions: “, [Link])
print(“Shape of array”,[Link])
print(“Size of array”,[Link])
print(“Array stores elements of type”,[Link])

Output :
Array's of type : <class 'numpy. ndarray'>
No. of dimensions:2
Shape of array(2,3)
Size of array(2,3)
Array of stores elements of type int64
[Link].2 WORKING WITH NUMPY ARRAYS

PROGRAM:

import numpy as np
# Creating array object arr = [Link]( [[ 1, 2, 3],
[ 4, 2, 5]] )
# Printing type of arr object
print("Array is of type: ", type(arr))
# Printing array dimensions (axes)
print("No. of dimensions: ", [Link])
# Printing shape of array
print("Shape of array: ", [Link])
# Printing size (total number of elements) of array
print("Size of array: ", [Link])
# Printing type of elements in array
print("Array stores elements of type: ", [Link])

OUTPUT
Array is of type: <class '[Link]'>
No. of dimensions: 2
Shape of array: (2, 3) Size of array: 6
Array stores elements of type: int32
PROGRAM TO PERFORM ARRAY SLICING

PROGRAM:
a = [Link]([[1,2,3],[3,4,5],[4,5,6]])
print(a)
print("After slicing")
print(a[1:])

OUTPUT
[[1 2 3]
[3 4 5]
[4 5 6]]
After slicing
[[3 4 5]
[4 5 6]]
PROGRAM TO PERFORM ARRAY SLICING

# array to begin with import numpy as np


a = [Link]([[1,2,3],[3,4,5],[4,5,6]])
print('Our array is:' )
print(a)
# this returns array of items in the second column
print('The items in the second column are:' )
print(a[...,1])
print('\n' )
# Now we will slice all items from the second row
print ('The items in the second row are:' )
print(a[1,...])
print('\n' )
# Now we will slice all items from column 1 onwards
print('The items column 1 onwards are:' )
print(a[...,1:])

OUTPUT
Our array is:
[[1 2 3]
[3 4 5]
[4 5 6]]
The items in the second column are:
[2 4 5]

The items in the second row are: [3 4 5]

The items column 1 onwards are: [[2 3]


[4 5]
[5 6]]
[Link].3 CREATE A DATAFRAME USING A LIST OF ELEMENTS.

PROGRAM :
import pandas as pd data = {
'name': ['Siva', 'Kumar', 'Prasath', 'Askoh', 'Robin', 'Rajan', 'Joel'],
'city': ['Pudukkottai', 'Thanjure', 'Pattukkottai', 'Kumbagonam','Karaikudi', 'Mannark udi', 'Trichy'],
'age': [41, 28, 33, 34, 38, 31, 37],'py-score': [88.0, 79.0, 81.0, 80.0, 68.0, 61.0, 84.0]}
row_labels = [101, 102, 103, 104, 105, 106, 107]
df=[Link](data=data,index=row_labels)
df
[Link](n=2)
[Link](n=2)
[Link]
[Link]
[Link]
df.memory_usage()
[Link][0]
john = [Link](data=['Jovan', 'Medavakkam', 34, 79],index=[Link], name=17)
john
df = [Link](john)
df

OUTPUT

Index 56
name 56
city 56
age 56
py-score56
dtype: int64
EX NO:4 READING DATA FROM TEXT FILES DOES EXPLORING VARIOUS
COMMANDS DOING DESCRIPTIVE ANALYTICS ON THE IRIS DATA SET.

PROGRAM:

Step1: Read Text Files with Pandas using read_csv()


Download [Link] from UCI Repository.( [Link]
# importing pandas
import pandas as pd
# read text file into pandas DataFrame df = pd.read_csv("[Link]")

# display DataFrame print([Link]())

Step2: Read Excel files with Pandas using read_excel()

Download [Link] from Kaggle Repository.( [Link] # importing


pandas
import pandas as pd

# read text file into pandas DataFrame df = pd.read_excel("[Link]")

# display DataFrame print([Link]())

Step 3: Read as web url files with Pandas using read_excel()


csv_url = '[Link]
iris = pd.read_csv(csv_url, header = None)

Step 4: Descriptive Analysis

import pandas as pd iris_filename = '[Link]'


iris = pd.read_csv(iris_filename, sep=',', decimal='.', header=None, names= ['sepal_length', 'sepal_width',
'petal_length', 'petal_width','target'])
[Link]
sepal_widths=iris[‘sepal_width’].values petal_widths=iris[‘petal_width’].values
petal_lengths=iris[‘petal_length’].values sepal_lengths=iris[‘sepal_length’].values

import numpy as np
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

[Link]()

OUTPUT:
EX NO:5a UNIVARIATE ANALYSIS USING DIABETES DATA SET

PROGRAM:
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
sns.set_style('darkgrid')
%matplotlib inline from [Link]
import FormatStrFormatter
import warnings
[Link]('ignore')
Df=pd.read_csv("[Link]")
[Link]()
[Link]
[Link]
Df['‘Outcome']=Df['Outcome'].astype('bool')
[Link]()
[Link]().T
class_counts=[Link]('Outcome').size()
print('class breakdown of the data:\n')
print(class_counts)
Correlations=[Link](method='pearson') p
rint('correlations of attributes in the data: \n')
Correlations Skew=[Link]()
print('Skew of attribute distributions in the data:\n')
Skew
df1 = [Link](Df, columns= ['Age','Glucose'])
print (df1)
[Link]()
[Link]()
[Link]()
print([Link]())
[Link]()
print([Link]())

OUTPUT:

<class '[Link]'>
RangeIndex: 768 entries, 0 to 767
Data columns (total 10 columns):
# Column Non-Null Count Dtype

0 Pregnancies 768 non-null int64


1 Glucose 768 non-null int64
2 BloodPressure 768 non-null int64
3 SkinThickness 768 non-null int64
4 Insulin 768 non-null int64
5 BMI 768 non-null float64
6 DiabetesPedigreeFunction 768 non-null float64
7 Age 768 non-null int64
8 Outcome 768 non-null int64
9 ‘Outcome 768 non-null bool

dtypes: bool(1), float64(2), int64(7)


memory usage: 54.9 KB
class breakdown of the data:

Outcome
0 500
1 268

dtype: int64
correlations of attributes in the data:
Skew of attribute distributions in the data:

Age Glucose
0 50 148
1 31 85
2 32 183
3 21 89
4 33 137
.. … …
763 63 101
764 27 122
765 30 121
766 47 126
767 23 93

[768 rows x 2 columns]


Age 138.303046
Glucose 1022.248314
dtype: float64
Age 1.129597
Glucose 0.173754
dtype: float64
EX NO:5b LINEAR REGRESSION AND LOGISTIC REGRESSION WITH THE DIABETES
DATASET USING PYTHON MACHINE LEARNING

PROGRAM:
# Import the libraries
import pandas as pd
import numpy as np
import [Link] as plt
from sklearn import datasets
diabetes = datasets.load_diabetes() diabetes
print([Link])
# columns
diabetes.feature_names
# Now we will split the data into the independent and independent variable
X = [Link]
Y = [Link] [Link], [Link]
# We will split the data into training and testing data
from sklearn.model_selection import train_test_split
train_x, test_x, train_y, test_y = train_test_split(X,Y,test_size=0.3,random_state=99)
train_x.shape,train_y.shape
# Linear Regression
from sklearn.linear_model import LinearRegression le = LinearRegression()
[Link](train_x,train_y)
y_pred = [Link](test_x)
y_pred
result = [Link]({'Actual': test_y, 'Predict' : y_pred})
result
# we will check the accuracy
print('coefficient', le.coef_)
print('intercept', le.intercept_)
from [Link] import mean_squared_error, r2_score
# mean_squared_error
mean_squared_error(test_y,y_pred)
r2_score(test_y,y_pred)

OUTPUT: 0.4545737971700594
DIABETES AND LOGISTIC REGRESSIONDATA DOWNLOADED FROM KAGGLE

import [Link] as plt


import pandas as pd
import numpy as np
from sklearn import datasets, linear_model
from [Link] import mean_squared_error, r2_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

#To calculate accuracy measures and confusion matrix


from sklearn import metrics

diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)


diabetes_X = diabetes_X[:, [Link], 2]
# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

# Split the targets into training/testing sets


diabetes_y_train = diabetes_y[:-20]
diabetes_y_test = diabetes_y[-20:]

# Create linear regression object


regr = linear_model.LinearRegression()

# Train the model using the training sets


[Link](diabetes_X_train, diabetes_y_train)

# Make predictions using the testing set


diabetes_y_pred = [Link](diabetes_X_test)

# Create Logistic regression object


Logistic_model = LogisticRegression()
Logistic_model.fit(diabetes_X_train, diabetes_y_train)

# The coefficients
print('Coefficients: \n', regr.coef_)
# The mean squared error
print('Mean square error:%.2f'% mean_squared_error(diabetes_y_test, diabetes_y_pred))
# The coefficient of determination: 1 is perfect prediction
print('Coefficient of determination:%.2f'% r2_score(diabetes_y_test, diabetes_y_pred))
y_predict = Logistic_model.predict(diabetes_X_train)
#print("Y predict/hat ", y_predict)
y_predict
OUTPUT:
Coefficients: [938.23786125]
Mean squared error: 2548.07
Coefficient of determination: 0.47
EX NO:5c USE THE DIABETES DATA SET FROM UCI AND PIMA INDIANS DIABETES
DATA SET FOR PERFORMING THE FOLLOWING: MULTIPLE REGRESSION

PROGRAM:
import pandas as pd
from sklearn import linear_model
df = pd.read_csv ('[Link]')
print (df)
X = df[['Glucose', 'BloodPressure']]
y = df['Age']
regr = linear_model.LinearRegression()
[Link](X, y)
predictedage = [Link]([[150, 13]])
print(predictedage)

OUTPUT:

Pregnancies Glucose
BloodPressure SkinThickness
Insulin BMI \

0 6 148
72 35 0 33.6
1 1 85
66 29 0 26.6
2 8 183
64 0 0 23.3
3 1 89
66 23 94 28.1
4 0 137
40 35 168 43.1
.. ... ...
... ... ... ...
763 10 101
76 48 180 32.9
764 2 122
70 27 0 36.8
765 5 121
72 23 112 26.2
766 1 126
60 0 0 30.1
767 1 93
70 31 0 30.4

DiabetesPedigreeFunction Age
Outcome

0 0.627 50
1
1 0.351 31
0
2 0.672 32
1
3 0.167 21
0
4 2.288 33
1
.. ... ...
...
763 0.171 63
0
764 0.340 27
0
765 0.245 30
0
766 0.349 47
1
767 0.315 23
0

[768 rows x 9 columns]


[28.77214401]
EX NO:5d COMPARE THE RESULTS OF THE ABOVE ANALYSIS FOR THE TWO DATA
SETS.

PROGRAM:
import pandas as pd
import numpy as np
data_1 = pd.read_csv(r'd:\[Link]')
df1 = [Link](data_1)
data_2 = pd.read_csv(r'd:\[Link]')
df2 = [Link](data_2)
df1['amount1'] = df2['amount1']
df1['prices_match'] = [Link](df1['amount'] == df2['amount1'], 'True', 'False')
df1['price_diff'] = [Link](df1['amount'] == df2['amount1'], 0, df1['amount'] –
df2['amount1'])
print(df1)

OUTPUT:
Model City Year amount amount1 prices_match price_diff
0 Maruti Chennai 2022 600000 600000
True 0
1 Hyndai Chennai 2022 700000 700000
True 0
2 Ford Chennai 2022 800000 850000
False -50000
3 Kia Chennai 2022 900000 900000
True 0
4 XL6 Chennai 2022 1000000 1000000
True 0
5 Tata Chennai 2022 1100000 1150000
False -50000
6 Audi Chennai 2022 1200000 1200000
True 0
7 Ertiga Chennai 2022 1300000 1300000
True 0
EX NO:6a APPLY AND EXPLORE VARIOUS PLOTTING FUNCTIONS ON UCI DATA
SETS.

DENSITY AND CONTOUR PLOTS

PROGRAM:
%matplotlib inline
import [Link] as plt [Link]('seaborn-white') import numpy as np
def f(x, y):
return [Link](x) ** 10 + [Link](10 + y * x) * [Link](x) x = [Link](0, 5, 50)
y = [Link](0, 5, 40) X, Y = [Link](x, y) Z = f(X, Y)
[Link](X, Y, Z, colors='black'); [Link](X, Y, Z, 20, cmap='RdGy'); [Link](X, Y, Z, 20,
cmap='RdGy') [Link]();

OUTPUT:

.
EX NO:6b APPLY AND EXPLORE VARIOUS PLOTTING FUNCTIONS
LIKE CORRELATION AND SCATTER PLOTS ON UCI DATA SETS

PROGRAM :
import pandas as pd
con = pd.read_csv('[Link]') con
list([Link])
import seaborn as sns
[Link](x="Pregnancies", y="Age", data=con);
[Link](x="Pregnancies", y="Age", data=con);
[Link](x="Pregnancies", y="Age", hue="Outcome", data=con);
from scipy import stats
[Link](con['Age'], con['Outcome'])
cormat = [Link]()
round(cormat,2)
[Link](cormat);

OUTPUT:
EX NO:6c APPLY AND EXPLORE HISTOGRAMS AND THREE DIMENSIONAL PLOTTING
FUNCTIONS ON UCI DATA SETS

PROGRAM:
import pandas as pd
import numpy as np
import [Link] as plt # To visualize
from mpl_toolkits.mplot3d import Axes3D
data = pd.read_csv('d:\\[Link]')
data
data['Glucose'].plot(kind='hist')
fig = [Link](figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
fig = [Link]()
ax = fig.add_subplot(111, projection='3d')
x = data['Age'].values
y = data['Glucose'].values
z = data['Outcome'].values
ax.set_xlabel("Age (Year)")
ax.set_ylabel("Glucose (Reading)")
ax.set_zlabel("Outcome (0 or 1)")
[Link](x, y, z, c='r', marker='o')
[Link]()

OUTPUT:
EX NO:7 VISUALIZING GEOGRAPHIC DATA WITH BASEMAP

PROGRAM:
Step 1: install conda package
conda install basemap
step 2: %matplotlib inline
import numpy as np
import [Link] as plt
from mpl_toolkits.basemap import Basemap
Step 3: [Link](figsize=(8, 8))
m=Basemap(projection='ortho', resolution=None, lat_0=50, lon_0=-100)
[Link](scale=0.5);
Step 4:
fig = [Link](figsize=(8, 8))
m = Basemap(projection='lcc', resolution=None, width=8E6, height=8E6,
lat_0=45, lon_0=-100,)
[Link](scale=0.5, alpha=0.5)
# Map (long, lat) to (x, y) for plotting
x, y = m(-122.3, 47.6)
[Link](x, y, 'ok', markersize=5)
[Link](x, y, ' Seattle', fontsize=12);
OUTPUT:

You might also like