FDS Program & Output-1
FDS Program & Output-1
FDS Program & Output-1
PROGRAM:
import numpy as np
np.random.seed(0) # seed for reproducibility
x1 = np.random.randint(10, size=6) # One-dimensional array
x2 = np.random.randint(10, size=(3, 4)) # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5)) # Three-dimensional array
2
Reg no: 211722118054
OUTPUT:
[5 0 3 3 7 9]
First element: 5
Fifth element: 7
Last element using negative index: 9
Second-to-last element using negative index: 7
[[3 5 2 4]
[7 6 8 8]
[1 6 7 7]]
Element at (0, 0): 3
Element at (2, 0): 1
Element at (2, -1): 7
Modified array:
[[12 5 2 4]
[ 7 6 8 8]
[ 1 6 7 7]]
3
Reg no: 211722118054
PROGRAM:
import numpy as np
#One-dimensional arrray
x = np.arange(10)
print(x)
print(x[:5]) # first five elements
print(x[5:]) # elements after index 5
print(x[4:7]) # middle subarray
print(x[::2] )# every other element
print(x[1::2])# every other element, starting at index 1
print(x[::-1]) # all elements, reverse
print(x[5::-2]) # reversed every other from index 5
# Two-dimensional array
x2 = np.random.randint(10, size=(3, 4))
print(x2)
5
Reg no: 211722118054
OUTPUT:
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4]
[5 6 7 8 9]
[4 5 6]
[0 2 4 6 8]
[1 3 5 7 9]
[9 8 7 6 5 4 3 2 1 0]
[5 3 1]
[[2 5 7 5]
[4 5 3 3]
[6 1 0 6]]
[[2 5 7]
[4 5 3]]
[[2 7]
[4 3]
[6 0]]
[[6 0 1 6]
[3 3 5 4]
[5 7 5 2]]
[2 4 6]
[2 5 7 5]
[2 5 7 5]
6
Reg no: 211722118054
PROGRAM:
import numpy as np
print(x)
# Slicing operations on the one-dimensional array
print("\n")
print(x2)
8
Reg no: 211722118054
OUTPUT:
[0 1 2 3 4 5 6 7 8 9]
[0 1 2 3 4]
[5 6 7 8 9]
[4 5 6]
[0 2 4 6 8]
[1 3 5 7 9]
[9 8 7 6 5 4 3 2 1 0]
[5 3 1]
[[7 8 2 7]
[9 1 2 9]
[8 4 4 1]]
[[7 8 2]
[9 1 2]]
[[7 2]
[9 2]
[8 4]]
[[1 4 4 8]
[9 2 1 9]
[7 2 8 7]]
[7 9 8]
[7 8 2 7]
[7 8 2 7]
9
Reg no: 211722118054
PROGRAM:
import pandas as pd
# Creating a Series
data = {'A': 10, 'B': 20, 'C': 30, 'D': 40}
s = pd.Series(data)
# 3. Slicing
print("Slicing:")
print("Subset from index 1 to 3:")
print(s[1:4])
print()
# 5. Fancy Indexing
print("Fancy Indexing:")
print("Selecting specific indices:")
print(s[['A', 'C']])
print()
# 6. Modifying Elements
print("Modifying Elements:")
s['A'] = 50
print("Modified Series:")
print(s)
11
Reg no: 211722118054
OUTPUT:
Original Series:
A 10
B 20
C 30
D 40
dtype: int64
Slicing:
Subset from index 1 to 3:
B 20
C 30
D 40
dtype: int64
Conditional Selection:
Values greater than 20:
C 30
D 40
dtype: int64
Fancy Indexing:
Selecting specific indices:
A 10
C 30
dtype: int64
Modifying Elements:
Modified Series:
A 50
B 20
C 30
D 40
dtype: int64
12
Reg no: 211722118054
PROGRAM:
population = pd.Series(population_dict)
print(population)
print(population['California'])
print(population['California':'Florida'])
# After Indexing
c = pd.Series({2: 'a', 1: 'b', 3: 'c'}, index=[3, 2])
print(c)
14
Reg no: 211722118054
OUTPUT:
0 0.25
1 0.50
2 0.75
3 1.00
dtype: float64
[0.25 0.5 0.75 1. ]
RangeIndex(start=0, stop=4, step=1)
0.5
1 0.50
2 0.75
dtype: float64
a 0.25
b 0.50
c 0.75
d 1.00
dtype: float64
0.5
2 0.25
5 0.50
3 0.75
7 1.00
dtype: float64
0.5
California 38332521
Texas 26448193
New York 19651127
Florida 19552860
Illinois 12882135
dtype: int64
38332521
California 38332521
Texas 26448193
New York 19651127
dtype: int64
0 2
1 4
2 6
dtype: int64
100 5
200 5
300 5
dtype: int64
2 a
1 b
3 c
dtype: object
3 c
2 a
dtype: obje
15
Reg no: 211722118054
PROGRAM:
import pandas as pd
18
Reg no: 211722118054
OUTPUT:
population area
California 38332521 423967
Texas 26448193 695662
New York 19651127 141297
Florida 19552860 170312
Illinois 12882135 149995
Area Column:
California 423967
Texas 695662
New York 141297
Florida 170312
Illinois 149995
Name: area, dtype: int64
DataFrame 'a':
population
California 38332521
Texas 26448193
New York 19651127
Florida 19552860
Illinois 12882135
19
Reg no: 211722118054
PROGRAM:
from csv import reader
from math import sqrt
lookup = dict()
for i, value in enumerate(unique):
lookup[value] = i
print('[%s] => %d' % (value, i))
for row in dataset:
row[column] = lookup[row[column]]
return lookup
21
Reg no: 211722118054
for i in range(len(row)):
row[i] = (row[i] - minmax[i][0]) / (minmax[i][1] - minmax[i][0])
OUTPUT:
[Setosa] => 0
[Versicolor] => 1
[Virginica] => 2
Data=[5.1, 3.7, 1.5, 0.4],
Predicted: 0
23
Reg no: 211722118054
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
from csv import DictReader
def main():
# Full path to the CSV file
file_path = "p:/diabetes.csv"
# Observations
Data = []
X, Y = [], []
for i in Data:
X.append(int(i['Glucose']))
Y.append(int(i['BloodPressure']))
x = np.array(X)
y = np.array(Y)
# 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)
25
Reg no: 211722118054
Output:
26
Reg no: 211722118054
PROGRAM:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
import seaborn as sn
import matplotlib.pyplot as plt
from csv import DictReader
Data = []
Glucose, BloodPressure, BMI, Outcome = [], [], [], []
for i in Data:
Glucose.append(int(i['Glucose']))
BloodPressure.append(int(i['Blood_Pressure']))
BMI.append(float(i["BMI"]))
Outcome.append(int(i["Outcome"]))
logistic_regression = LogisticRegression()
logistic_regression.fit(X_train, y_train)
28
Reg no: 211722118054
y_pred = logistic_regression.predict(X_test)
OUTPUT:
29
Reg no: 211722118054
PROGRAM:
# Regression coefficients
print('Coefficients:', reg.coef_)
# Plotting legend
plt.legend(loc='upper right')
31
Reg no: 211722118054
# Plot title
plt.title("Residual errors")
OUTPUT:
32
Reg no: 211722118054
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats # Corrected import statement
# Plot the standard normal distribution for different values of random variable
# falling in the range -5, 5
plt.figure(figsize=(7.5, 7.5))
plt.plot(x, snd.pdf(x))
plt.xlim(-5, 5)
plt.title('Normal Distribution', fontsize='15')
plt.xlabel('Values of Random Variable X', fontsize='15')
plt.ylabel('Probability', fontsize='15')
plt.show()
OUTPUT:
34
Reg no: 211722118054
PROGRAM:
import math
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
# Driver function
X = [15, 18, 21, 24, 27]
Y = [25, 25, 27, 31, 32]
print(X)
print(Y)
# Function call to
correlationCoefficient.z
=correlationCoefficient(X,
Y, n)
37
Reg no: 211722118054
OUTPUT:
38
Reg no: 211722118054
PROGRAM:
import numpy as np
from collections import Counter
from scipy import stats
# Given data
a = [11, 21, 34, 22, 27, 11, 23, 21]
# Finding Mean by simple Computation
mean = sum(a) / len(a)
print("Finding Mean by simple Computation")
print(mean)
OUTPUT:
Finding Mean by simple Computation
21.25
Finding Mean using numpy method
21.25
Finding Median by simple Computation
21
Finding Median by numpy method
21.5
Finding Mode by simple Computation
[11, 21]
Finding Mode using numpy method
ModeResult(mode=11, count=2)
Finding Standard Deviation by simple computation
7.1545440106270926
Finding Standard Deviation using numpy method
7.1545440106270926
41
Reg no: 211722118054
PROGRAM:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ax.set_xlabel('sepal_length')
ax.set_ylabel('sepal_width')
plt.show()
# create x data
x_data = range(0, iris.shape[0])
43
Reg no: 211722118054
plt.show()
# plot histogram
ax.hist(wine_reviews['points'])
wine_reviews['points'].value_counts().sort_index().plot.barh()
plt.show()
44
Reg no: 211722118054
# Correlation Matrix
corr = iris.corr()
fig, ax = plt.subplots()
# create heatmap
im = ax.imshow(corr.values)
# set labels
ax.set_xticks(np.arange(len(corr.columns)))
ax.set_yticks(np.arange(len(corr.columns)))
ax.set_xticklabels(corr.columns)
ax.set_yticklabels(corr.columns)
plt.show()
OUTPUT:
45
Reg no: 211722118054
Vertical bar chart for Wine Review Scores Horizontal bar chart for Wine Score Review
Bar chart for Wine Review with highest cost Correlation Matrix
46