Data Science Practical
Data Science Practical
Download, install and explore the features of NumPy, SciPy, Jupyter, Statsmodels and
Pandas packages.
# Importing necessary libraries
import numpy as np
import pandas as pd
import statsmodels.api as sm
def explore_numpy():
print("NumPy Operations:")
print("\n")
def explore_scipy():
plt.figure(figsize=(10, 6))
plt.xlabel('Value')
plt.ylabel('Density')
plt.grid()
plt.show()
print("\n")
# Function to explore Statsmodels features
def explore_statsmodels():
# Sample data
x = np.arange(100)
print(model.summary())
print("\n")
def explore_pandas():
df = pd.DataFrame(data)
print("DataFrame:\n", df)
print("\n")
explore_numpy()
explore_scipy()
explore_statsmodels()
explore_pandas()
Output -
NumPy Operations:
NumPy Array: [1 2 3 4 5]
import numpy as np
# Creating a 1D array
# Creating a 2D array
linspace_array = np.linspace(0, 1, 5)
print("\n")
# 2. Array Operations
# Subtraction
# Multiplication
# Division
# Element-wise square
print("\n")
# Accessing elements
# Slicing
print("\n")
# 4. Reshaping Arrays
reshaped_array = np.arange(12).reshape(3, 4)
# Flattening an array
flattened_array = reshaped_array.flatten()
print("\n")
# 5. Statistical Operations
print("\n")
# 6. Boolean Indexing
Ouput –
1D Array: [1 2 3 4 5]
2D Array:
[[1 2 3]
[4 5 6]]
Array of Zeros:
[[0. 0. 0.]
[0. 0. 0.]]
Array of Ones:
[[1. 1.]
[1. 1.]
[1. 1.]]
2. Array Operations:
Array B: [1 2 3]
4. Reshaping Arrays:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
5. Statistical Operations:
6. Boolean Indexing:
Boolean Index (elements > 2): [False False True True True]
import numpy as np
array_1d = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print("\nSlicing 1D Array:")
print("Elements from the start to index 4:", array_1d[:5]) # Slices from start to index 4
print("Elements from index 5 to the end:", array_1d[5:]) # Slices from index 5 to the end
print("\n")
[5, 6, 7, 8],
print("\nSlicing 2D Array:")
print("Elements from row 1 to 2 and column 1 to 3:\n", array_2d[1:3, 1:3]) # Slices a sub-array
print("Last two rows:\n", array_2d[-2:, :]) # Slices the last two rows
print("Every second row and every second column:\n", array_2d[::2, ::2]) # Slices every second row
and column
Output –
1D Array: [ 10 20 30 40 50 60 70 80 90 100]
Slicing 1D Array:
2D Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
Slicing 2D Array:
First row: [1 2 3 4]
[[ 6 7]
[10 11]]
[[ 9 10 11 12]
[13 14 15 16]]
[[ 1 3]
[ 9 11]]
Q4. Program for pandas Data Frames.
import pandas as pd
data = {
df = pd.DataFrame(data)
print(df)
Output –
2 Charlie 35 Chicago
Q5. Program to compute weighted averages in python either defining your own function or using
numby.
import numpy as np
values = np.array(values)
weights = np.array(weights)
if len(values) != len(weights):
if np.sum(weights) == 0:
# Example usage
Output –
def calculate_variance(data):
if len(data) == 0:
return 0
return variance
# Example usage
variance = calculate_variance(data)
Output –
import numpy as np
# Generate x values
return x, y
mean = 0
std_dev = 1
x, y = normal_curve(mean, std_dev)
plt.figure(figsize=(10, 6))
plt.xlabel('X-axis')
plt.ylabel('Probability Density')
plt.legend()
plt.grid()
plt.show()
Output –
| *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
|* *
|* *
+-------------------------------------------+
-4 -3 -2 -1 0 1 2 3 4
Q8. Program for correlation with scatter plot.
import numpy as np
correlation_coefficient, _ = pearsonr(x, y)
plt.figure(figsize=(10, 6))
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid()
plt.show()
Output –
| *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
| * *
|* *
|* *
+-------------------------------------------+
-4 -3 -2 -1 0 1 2 3 4
Q9. Program to compute correlation Coefficient.
import numpy as np
# Sample data
print(f"Data X: {data_x}")
print(f"Data Y: {data_y}")
print(f"P-value: {p_value:.4f}")
Output –
P-value: 0.0000
Q10. Program for simple linear Regression.
import numpy as np
# Sample data
y = np.array([2, 3, 5, 7, 11])
model = LinearRegression()
model.fit(X, y)
# Make predictions
y_pred = model.predict(X)
slope = model.coef_[0]
intercept = model.intercept_
print(f"Slope: {slope}")
print(f"Intercept: {intercept}")
plt.legend()
plt.show()
Output –
Slope: 2.2
Intercept: 0.39999999999999947
Q11. Create a numpy And Array object by using array Function ().
import numpy as np
list_data = [1, 2, 3, 4, 5]
array_from_list = np.array(list_data)
array_from_tuple = np.array(tuple_data)
Output –
import numpy as np
array_from_tuple = np.array(data_tuple)
Output –
import numpy as np
# Step 1: Create a nested tuple (or you can use a list of lists)
array_2d = np.array(data_2d)
print(array_2d)
Output –
[[1 2 3]
[4 5 6]]
Q14. Displaying the dimension array from 0 to 3.
import numpy as np
print("1-D Array:")
print(array_1d)
print(array_2d)
print(array_3d)
Output –
1-D Array:
[0 1 2 3]
[[0 1]
[2 3]]
[[[0]
[1]]
[[2]
[3]]]
Q15. Program for accessing array element by indexing & adding it.
import numpy as np
Output –
First Element: 10
Second Element: 20
import numpy as np
array = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print("Original Array:")
print(array)
print(sliced_array)
Output –
Original Array:
[ 10 20 30 40 50 60 70 80 90 100]
[20 30 40 50 60]
Q17. Print the shape of an array.
import numpy as np
array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # 3-D array
Output –
import numpy as np
Output –
Index 0: Value 10
Index 1: Value 20
Index 2: Value 30
Index 3: Value 40
Index 4: Value 50
Q19. Program to split the array in three parts.
import numpy as np
split_arrays = np.array_split(array, 3)
print("Original Array:")
print(array)
print("\nSplit Arrays:")
Output –
Original Array:
[ 1 2 3 4 5 6 7 8 9 10]
Split Arrays:
Part 1: [1 2 3 4]
Part 2: [5 6 7]
Part 3: [ 8 9 10]
Q20. Program to find indexes where the value is even.
import numpy as np
print("Original Array:")
print(array)
print(even_indexes)
Output –
Original Array:
[ 1 2 3 4 5 6 7 8 9 10]
[1 3 5 7 9]