EXNO1
import numpy as np
# a) Convert an array to a float type
arr = np.array([1, 2, 3])
float_arr = arr.astype(float)
print("Float array:", float_arr)
# b) Create a 3x3 matrix with values ranging from 2 to 10
matrix = np.arange(2, 11).reshape(3, 3)
print("3x3 matrix:\n", matrix)
# c) Convert a list of numeric values into a one-dimensional NumPy array
num_list = [10, 20, 30, 40]
array_from_list = np.array(num_list)
print("1D NumPy array:", array_from_list)
# d) Convert a list and a tuple into arrays
list_data = [5, 10, 15]
tuple_data = (2, 4, 6)
array_from_list = np.array(list_data)
array_from_tuple = np.array(tuple_data)
print("Array from list:", array_from_list)
print("Array from tuple:", array_from_tuple)
OUTPUT
Float array: [1. 2. 3.]
3x3 matrix:
[[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]
1D NumPy array: [10 20 30 40]
Array from list: [ 5 10 15]
Array from tuple: [2 4 6]
[Program finished]
EXNO2
import numpy as np
# a) Append values to the end of an array
arr1 = np.array([1, 2, 3])
appended_arr = np.append(arr1, [4, 5])
print("a) Appended Array:", appended_arr)
# b) Find the real and imaginary parts of an array of complex numbers
complex_arr = np.array([1+2j, 3+4j, 5+6j])
print("b) Real parts:", np.real(complex_arr))
print(" Imaginary parts:", np.imag(complex_arr))
# c) List the second column elements from a shape (3,3) array
arr2 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
second_column = arr2[:, 1]
print("c) Second column elements:", second_column)
# d) Find the maximum and minimum value from a shape (3,3) array
arr3 = np.array([[4, 9, 1], [7, 3, 6], [8, 2, 5]])
print("d) Maximum value:", np.max(arr3))
print(" Minimum value:", np.min(arr3))
OUTPUT
a) Appended Array: [1 2 3 4 5]
b) Real parts: [1. 3. 5.]
Imaginary parts: [2. 4. 6.]
c) Second column elements: [2 5 8]
d) Maximum value: 9
Minimum value: 1
[Program finished]
EXNO3
import numpy as np
from scipy import stats
# Marks of 20 students
marks = [75, 80, 85, 90, 70, 88, 76, 92, 81, 78,
84, 79, 77, 83, 86, 82, 74, 89, 91, 87]
# Average we are testing against
expected_mean = 80
# Z-Test (if population standard deviation is known)
pop_std = 5 # assume population std is 5
sample_mean = np.mean(marks)
z = (sample_mean - expected_mean) / (pop_std / np.sqrt(len(marks)))
print("Z-Test Result:")
print("Z-Score =", z)
# T-Test (when population std is not known)
t, p = stats.ttest_1samp(marks, expected_mean)
print("\nT-Test Result:")
print("T-Score =", t)
print("P-Value =", p)
OUTPUT
Z-Test Result:
Z-Score = 2.1019038988497973
T-Test Result:
T-Score = 1.6948633480403503
P-Value = 0.10642890850820598
[Program finished]
EXNO4
from scipy.stats import f_oneway
# Test scores for each class
class_A = [8, 6, 8, 6]
class_B = [7, 9, 5, 10, 7, 4, 2]
class_C = [4, 3, 6, 2, 7, 5, 5, 4, 1, 3, 6, 1, 3, 5, 3, 4, 6, 5, 7, 3]
# Perform ANOVA
f_statistic, p_value = f_oneway(class_A, class_B, class_C)
# Output results
print("ANOVA Test Results:")
print(f"F-statistic: {f_statistic:.3f}")
print(f"P-value: {p_value:.5f}")
# Interpretation
if p_value < 0.05:
print("Result: Significant difference between at least two groups.")
else:
print("Result: No significant difference between groups.")
OUTPUT
ANOVA Test Results:
F-statistic: 5.254
P-value: 0.01155
Result: Significant difference between at least two groups.
[Program finished]
EXNO5
import pandas as pd
import numpy as np
# 1. Create DataFrame from NumPy array
array_data = np.array([[1, 2, 3], [4, 5, 6]])
df_array = pd.DataFrame(array_data, columns=['A', 'B', 'C'])
print("DataFrame from NumPy array:")
print(df_array)
# 2. Create DataFrame from dictionary
dict_data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
}
df_dict = pd.DataFrame(dict_data)
print("\nDataFrame from dictionary:")
print(df_dict)
# 3. Create DataFrame from Series
series_data = pd.Series([100, 200, 300], index=['X', 'Y', 'Z'])
df_series = pd.DataFrame(series_data, columns=['Values'])
print("\nDataFrame from Series:")
print(df_series)
OUTPUT
DataFrame from NumPy array:
A B C
0 1 2 3
1 4 5 6
DataFrame from dictionary:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
DataFrame from Series:
Values
X 100
Y 200
Z 300
[Program finished]
EXNO6
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a 2x2 subplot grid
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# Line Plot
axs[0, 0].plot(x, y)
axs[0, 0].set_title("Line Plot")
# Bar Chart
axs[0, 1].bar(x, y)
axs[0, 1].set_title("Bar Chart")
# Scatter Plot
axs[1, 0].scatter(x, y)
axs[1, 0].set_title("Scatter Plot")
# Pie Chart (only in one subplot, takes whole axes)
axs[1, 1].pie(y, labels=x, autopct='%1.1f%%')
axs[1, 1].set_title("Pie Chart")
# Adjust layout
plt.tight_layout()
plt.show()
OUTPUT
EXNO7
import numpy as np
import pandas as pd
def calculate_variance(data):
# Convert input to NumPy array for consistency
data_array = np.array(data, dtype=float)
return np.var(data_array, ddof=1) # ddof=1 for sample variance
# Different types of data samples
list_sample = [10, 12, 23, 23, 16, 23, 21, 16]
numpy_sample = np.array([4.5, 6.7, 8.9, 5.5, 7.3])
pandas_sample = pd.Series([100, 102, 98, 97, 105, 100])
# Calculate and display variances
print("Variance from list:", calculate_variance(list_sample))
print("Variance from NumPy array:", calculate_variance(numpy_sample))
print("Variance from Pandas Series:", calculate_variance(pandas_sample))
OUTPUT
Variance from list: 27.428571428571427
Variance from NumPy array: 2.8520000000000003
Variance from Pandas Series: 8.266666666666667
[Program finished]
EXNO8
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
# Your data
data = [5, 7, 8, 9, 10, 11, 12, 13, 15, 18]
mean = np.mean(data)
std = np.std(data)
x = np.linspace(min(data), max(data), 100)
y = norm.pdf(x, mean, std)
plt.plot(x, y)
plt.title('Normal Curve from Your Data')
plt.show()
OUTPUT
EXNO9
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(50)
y = [x*5+3, -5*x, np.random.randn(50)]
c = ['green', 'red', 'blue']
l = ['Positive', 'Negative', 'Zero']
plt.figure(figsize=(10, 8), dpi=100)
for yi, ci, li in zip(y, c, l):
plt.scatter(x, yi, color=ci, label=f"{li} correlation (r = {np.corrcoef(x, yi)[0,1]:.1f})")
plt.xlabel("X-axis"); plt.ylabel("Y-axis")
plt.title("Correlation and Scatter Plots")
plt.legend()
plt.grid()
plt.show()
OUTPUT
EXNO10
import numpy as np
# Given data
X = [15, 18, 21, 24, 27]
Y = [25, 25, 27, 31, 32]
# Calculate correlation coefficient
correlation_matrix = np.corrcoef(X, Y)
correlation_coefficient = correlation_matrix[0, 1]
print("Correlation Coefficient:", correlation_coefficient)
OUTPUT
Correlation Coefficient: 0.9534625892455922
[Program finished]
EXNO11
import numpy as np
import matplotlib.pyplot as plt
# Data
X = np.array([1, 2, 3.4, 5.6, 7, 8, 9, 10])
Y = np.array([3, 2, 5, 7, 8, 8, 9, 10])
# Linear regression calculation
m, b = np.polyfit(X, Y, 1) # slope and intercept
# Predicted Y values
Y_pred = m * X + b
# Plot
plt.scatter(X, Y, color='blue', label='Data')
plt.plot(X, Y_pred, color='red', label='Regression Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Simple Linear Regression')
plt.legend()
plt.grid(True)
plt.show()
OUTPUT
EXNO12
from sklearn.linear_model import LinearRegression
# Sample data: X = input (hours), y = output (marks)
X = [[1], [2], [3], [4], [5]]
y = [10, 20, 30, 40, 50]
# Create and train model
model = LinearRegression()
model.fit(X, y)
# Predict
prediction = model.predict([[6]])
print("Predicted value for 6:", prediction[0])
# Model details
print("Slope:", model.coef_[0])
print("Intercept:", model.intercept_)
OUTPUT
Predicted value for 6: 60.00000000000001
Slope: 10.000000000000002
Intercept: -7.105427357601002e-15
[Program finished]
EXNO13
import pandas as pd
import numpy as np
# Corrected dictionary
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 for index
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
# Create DataFrame
df = pd.DataFrame(exam_data, index=labels)
# Display the DataFrame
print(df)
OUTPUT
name score attempts qualify
a Anastasia 12.5 1 yes
b Dima 9.0 3 no
c Katherine 16.5 2 yes
d James NaN 3 no
e Emily 9.0 2 no
f Michael 20.0 3 yes
g Matthew 14.5 1 yes
h Laura NaN 1 no
i Kevin 8.0 2 no
j Jonas 19.0 1 yes
[Program finished]
EXNO14
import numpy as np
# Original dictionary
data = {
'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}
}
print("Original dictionary:")
print(data)
print("Type:", type(data))
# Extract keys to keep consistent order
keys = ['a', 'b', 'c', 'd']
# Build list of lists: rows correspond to keys, columns correspond to the dictionary keys
array_data = [[data[col][k] for col in data] for k in keys]
# Convert to NumPy array
nd_array = np.array(array_data)
print("\nndarray:")
print(nd_array)
print("Type:", type(nd_array))
OUTPUT
Original dictionary:
{'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}}
Type: <class 'dict'>
ndarray:
[[ 1. 3. 4. 3.]
[ 0. 1. 1. -1.]
[ 0. 0. 5. -1.]
[ 2. -1. -1. -1.]]
Type: <class 'numpy.ndarray'>
[Program finished]
EXNO15
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
X = [[2,3],[1,5],[2,8],[5,2],[6,1],[7,3],[8,5],[7,7],[8,8],[9,7]]
y = [0,0,0,1,1,1,1,1,1,1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LogisticRegression().fit(X_train, y_train)
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%")
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
OUTPUT
Accuracy: 100.00%
Classification Report:
precision recall f1-score support
0 1.00 1.00 1.00 1
1 1.00 1.00 1.00 1
accuracy 1.00 2
macro avg 1.00 1.00 1.00 2
weighted avg 1.00 1.00 1.00 2
[Program finished]
EXNO16
import pandas as pd
# Sample DataFrame
data = {
'col1': [0, 1, 2, 3, 4],
'col2': [1, 2, 3, 4, 7],
'col3': [4, 5, 6, 9, 1]
}
df = pd.DataFrame(data)
print("Original DataFrame")
print(df)
# Select all columns except 'col3'
df_except_col3 = df.loc[:, df.columns != 'col3']
print("\nAll columns except col3:")
print(df_except_col3)
OUTPUT
Original DataFrame
col1 col2 col3
0 0 1 4
1 1 2 5
2 2 3 6
3 3 4 9
4 4 7 1
All columns except col3:
col1 col2
0 0 1
1 1 2
2 2 3
3 3 4
4 4 7
[Program finished]
EXNO17
import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.pyplot as plt
# Sample time series data (e.g., 12 monthly values)
data = [10, 12, 13, 15, 16, 18, 20, 21, 22, 24, 25, 27]
# Prepare the data: X = time steps, y = values
X = np.array(range(len(data))).reshape(-1, 1) # [[0],[1],...]
y = np.array(data)
# Fit a simple linear regression model
model = LinearRegression()
model.fit(X, y)
# Predict the next 3 future values
future_steps = np.array(range(len(data), len(data)+3)).reshape(-1, 1)
future_preds = model.predict(future_steps)
# Plot the original data and predictions
plt.plot(X, y, label='Original Data')
plt.plot(future_steps, future_preds, 'ro--', label='Forecast')
plt.xlabel('Time')
plt.ylabel('Value')
plt.legend()
plt.show()
print("Future predicted values:", future_preds)
OUTPUT
EXNO18
import pandas as pd
import matplotlib.pyplot as plt
# Sample data
data = [12, 15, 14, 15, 19, 21, 18, 17, 16, 15, 14, 19, 22, 24, 25, 21, 23, 20]
# Create a DataFrame
df = pd.DataFrame(data, columns=['Values'])
# Plot histogram (frequency distribution)
plt.figure(figsize=(8, 4))
plt.hist(df['Values'], bins=5, edgecolor='black')
plt.title('Frequency Distribution of Sample Data')
plt.xlabel('Value Ranges')
plt.ylabel('Frequency')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
OUTPUT
EXNO19
import numpy as np
# Define three NumPy arrays with the same shape
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
array3 = np.array([[9, 10], [11, 12]])
# Merge the arrays along a new axis (depth-wise)
merged_array = np.stack((array1, array2, array3), axis=0)
print("Merged Array:\n", merged_array)
OUTPUT
Merged Array:
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
[Program finished]
EXNO20
import pandas as pd
import numpy as np
data = {
'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily',
'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9.0, 16.5, np.nan, 9.0, 20.0, 14.5, np.nan, 8.0, 19.0],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']
}
df = pd.DataFrame(data, index=list('abcdefghij'))
print("Number of attempts in the examination is greater than 2:")
print(df[df.attempts > 2])
OUTPUT
Number of attempts in the examination is greater than 2:
name score attempts qualify
b Dima 9.0 3 no
d James NaN 3 no
f Michael 20.0 3 yes
[Program finished]