0% found this document useful (0 votes)
16 views8 pages

Univds

Uploaded by

Abirami Jaisu
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)
16 views8 pages

Univds

Uploaded by

Abirami Jaisu
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
You are on page 1/ 8

1) a) Write a NumPy program to convert an array to a float type

b) Write a NumPy program to create a 3x3 matrix with values ranging from 2 to 10
c) Write a NumPy program to convert a list of numeric value into a one-dimensional
NumPy array
d) Write a NumPy program to convert a list and tuple into arrays

a)import numpy as np
import numpy as np
a = [1, 2, 3, 4]
print("Original array")
print(a)
x = np.asfarray(a)
print("Array converted to a float type:")
print(x)

b)import numpy as np
x = np.arange(2, 11).reshape(3,3)
print(x)

c) import numpy as np

l = [12.23, 13.32, 100, 36.32]


print("Original List:",l)
a = np.array(l)
print("One-dimensional NumPy array: ",a)

d) import numpy as np

my_list = [1, 2, 3, 4, 5, 6, 7, 8]
print("List to array: ")
print(np.asarray(my_list))
my_tuple = ([8, 4, 6], [1, 2, 3])
print("Tuple to array: ")
print(np.asarray(my_tuple))

2) a) Write a NumPy program to append values to the end of an array


b) Write a NumPy program to find the real and imaginary parts of an array of complex
numbers
c) Write a NumPy program to list the second column elements from the shape of (3,3)
array
d) Write a NumPy program to find the maximum and minimum value from the shape of
(3,3)
array
a) import numpy as np

x = [10, 20, 30]


print("Original array:")
print(x)
x = np.append(x, [[40, 50, 60], [70, 80, 90]])
print("After append values to the end of the array:")
print(x)

b) import numpy as np

x = np.sqrt([1+0j])
y = np.sqrt([0+1j])
print("Original array:x ",x)
print("Original array:y ",y)
print("Real part of the array:")
print(x.real)
print(y.real)
print("Imaginary part of the array:")
print(x.imag)
print(y.imag)

c) import numpy as np

x = np.arange(2, 11).reshape(3,3)
print(x)

d) import numpy as np

a = np.arange(4).reshape((2,2))
print("Original flattened array:")
print(a)
print("Maximum value of the above flattened array:")
print(np.amax(a))
print("Minimum value of the above flattened array:")
print(np.amin(a))

3) Write a Python Program to create data frame from array, dictionary and series of input
values

a)import pandas as pd
# Initialise data to lists.
data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
{'Geeks':10, 'For': 20, 'geeks': 30}]

df = pd.DataFrame.from_records(data,index=['1', '2'])
print(df)

b) import pandas as pd

# Initialise data to lists.


data = [{'Geeks': 'dataframe', 'For': 'using', 'geeks': 'list'},
{'Geeks':10, 'For': 20, 'geeks': 30}]

df = pd.DataFrame.from_dict(data)
print(df)

4) Write a python program to compute correlation coefficient for the given data
X = [15, 18, 21, 24, 27] and Y = [25, 25, 27, 31, 32]

# Python Program to find correlation coefficient.


import math

# function that returns correlation coefficient.


def correlationCoefficient(X, Y, n) :
sum_X = 0
sum_Y = 0
sum_XY = 0
squareSum_X = 0
squareSum_Y = 0

i=0
while i < n :
# sum of elements of array X.
sum_X = sum_X + X[i]

# sum of elements of array Y.


sum_Y = sum_Y + Y[i]

# sum of X[i] * Y[i].


sum_XY = sum_XY + X[i] * Y[i]
# sum of square of array elements.
squareSum_X = squareSum_X + X[i] * X[i]
squareSum_Y = squareSum_Y + Y[i] * Y[i]

i=i+1

# use formula for calculating correlation


# coefficient.
corr = (float)(n * sum_XY - sum_X * sum_Y)/
(float)(math.sqrt((n * squareSum_X -
sum_X * sum_X)* (n * squareSum_Y -
sum_Y * sum_Y)))
return corr

# Driver function
X = [15, 18, 21, 24, 27]
Y = [25, 25, 27, 31, 32]

# Find the size of array.


n = len(X)

# Function call to correlationCoefficient.


print ('{0:.6f}'.format(correlationCoefficient(X, Y, n)))

5) Write a python program to Plot Linear Regression for the data x = [0, 1, 2, 3, 4, 5, 6, 7,
8, 9]
y = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12]

import numpy as np

import matplotlib.pyplot as plt

def estimate_coef(x, y):


# number of observations/points
n = np.size(x)

# mean of x and y vector


m_x = np.mean(x)
m_y = np.mean(y)

# calculating cross-deviation and deviation about x


SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x

return (b_0, b_1)

def plot_regression_line(x, y, b):


# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s = 30)

# predicted response vector


y_pred = b[0] + b[1]*x

# plotting the regression line


plt.plot(x, y_pred, color = "g")

# putting labels
plt.xlabel('x')
plt.ylabel('y')

# function to show plot


plt.show()

def main():
# observations / data
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])

# estimating coefficients
b = estimate_coef(x, y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))

# plotting regression line


plot_regression_line(x, y, b)

if __name__ == "__main__":
main()
6) Write a Pandas program to create and display a DataFrame from a specified dictionary
data
which has the index labels.
Sample Python dictionary data and list labels:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael',
'Matthew',
'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

import pandas as pd
import numpy as np

exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael',


'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

df = pd.DataFrame(exam_data , index=labels)
print(df)

7) Write a NumPy program to convert a Python dictionary to a NumPy ndarray

import numpy as np

from ast import literal_eval

udict = """{"column0":{"a":1,"b":0.0,"c":0.0,"d":2.0},

"column1":{"a":3.0,"b":1,"c":0.0,"d":-1.0},

"column2":{"a":4,"b":1,"c":5.0,"d":-1.0},

"column3":{"a":3.0,"b":-1.0,"c":-1.0,"d":-1.0}

}"""
t = literal_eval(udict)

print("\nOriginal dictionary:")

print(t)

print("Type: ",type(t))

result_nparra = np.array([[v[j] for j in ['a', 'b', 'c', 'd']] for k, v in t.items()])

print("\nndarray:")

print(result_nparra)

print("Type: ",type(result_nparra))

8) Write a Pandas program to select all columns, except one given column in a
DataFrame

import pandas as pd
d = {'col1': [1, 2, 3, 4, 7], 'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}
df = pd.DataFrame(data=d)
print("Original DataFrame")
print(df)
print("\nAll columns except 'col3':")
df = df.loc[:, df.columns != 'col3']
print(df)

9) Write a Python Program to implement and Validate Time Series Analysis in order to
predict the future value with your own sample data
10) Write a Python Program to plot a graph for the frequency Distribution of the sample
data
11)Write a NumPy program to merge three given NumPy arrays of same shape

import numpy as np
arr1 = np.random.random(size=(25, 25, 1))
arr2 = np.random.random(size=(25, 25, 1))
arr3 = np.random.random(size=(25, 25, 1))
print("Original arrays:")
print(arr1)
print(arr2)
print(arr3)
result = np.concatenate((arr1, arr2, arr3), axis=-1)
print("\nAfter concatenate:")
print(result)

12) Write a Python Program to demonstrate various styles of Plotting graph using
Matplotlib Module.
13) Write a python program to calculate the variance for variety of samples are derived
from different types of data
14) Create a normal curve using python program with your own data’s
15) Write a python program to show the result of correlation with scatter plot from your
own input’s
16)Write a Python Program to implement and Validate Linear Regression Model with
your own sample data
17) Write a Python Program to demonstrate the Z-Test and T-Test result for the sample
of 20 Students +2 exam final marks.
18) Write a Python Program to show the result of ANOVA Test for the sample of three
groups of Data
19) Write a Python Program to implement and Validate Logistic Regression Model with
your own sample data
20)

You might also like