0% found this document useful (0 votes)
8 views

Ai Programs

Recharge panitiya ilaynu therila solanum thonuchi so much for your response and I am Thirumalai R u there in 7wyshs ok va va
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Ai Programs

Recharge panitiya ilaynu therila solanum thonuchi so much for your response and I am Thirumalai R u there in 7wyshs ok va va
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EXAMPLE 1

import numpy as np

# Step 1: Create a NumPy array

data = np.array([10, 20, 30, 40, 50])

print("Original array:", data)

# Step 2: Perform element-wise operations

squared = data ** 2

print("Squared values:", squared)

# Step 3: Calculate mean and standard deviation

mean_value = np.mean(data)

std_dev = np.std(data)

print("Mean:", mean_value)

print("Standard Deviation:", std_dev)

# Step 4: Reshape the array

reshaped_data = data.reshape((5, 1))

print("Reshaped array (5x1):")

print(reshaped_data)

# Step 5: Create another array and perform operations

other_data = np.array([1, 2, 3, 4, 5])

sum_array = data + other_data

print("Sum of original and other array:", sum_array)

# Step 6: Calculate dot product

dot_product = np.dot(data, other_data)

print("Dot product:", dot_product)

# Step 7: Accessing elements and slicing

print("Second element:", data[1])

print("Elements from index 1 to 3:", data[1:4])

# Final Output

print("Final output of calculations:")

print("Original Array:", data)

print("Squared Values:", squared)

print("Mean Value:", mean_value)

print("Standard Deviation:", std_dev)

print("Reshaped Array:")
print(reshaped_data)

print("Sum Array:", sum_array)

print("Dot Product:", dot_product)

OUTPUT:

Original array: [10 20 30 40 50]

Squared values: [ 100 400 900 1600 2500]

Mean: 30.0

Standard Deviation: 14.142135623730951

Reshaped array (5x1):

[[10]

[20]

[30]

[40]

[50]]

Sum of original and other array: [11 22 33 44 55]

Dot product: 550

Second element: 20

Elements from index 1 to 3: [20 30 40]

Final output of calculations:

Original Array: [10 20 30 40 50]

Squared Values: [ 100 400 900 1600 2500]

Mean Value: 30.0

Standard Deviation: 14.142135623730951

Reshaped Array:

[[10]

[20]

[30]

[40]

[50]]

Sum Array: [11 22 33 44 55]

Dot Product: 550

Explanation of the Steps

1. Create a NumPy array: Initializes the array with given values.

2. Element-wise operations: Squares each element in the array.


3. Calculate statistics: Computes the mean and standard deviation.

4. Reshape the array: Changes the shape of the array to a 5x1 format.

5. Operations with another array: Adds the original array to another array.

6. Dot product: Computes the dot product of the two arrays.

7. Indexing and slicing: Demonstrates how to access specific elements and slices of the array.

EXAMPLE 2

WORKING WITH PANDAS DATA FRAMES WITH EXAMPLE AND OUTPUT

import pandas as pd

# Step 1: Create a DataFrame

data = {

'Name': ['Alice', 'Bob', 'Charlie', 'David'],

'Age': [25, 30, 35, 40],

'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']

df = pd.DataFrame(data)

print("Original DataFrame:")

print(df)

# Step 2: Accessing Data

print("\nAccessing the 'Age' column:")

print(df['Age'])

# Step 3: Adding a new column

df['Salary'] = [70000, 80000, 120000, 100000]

print("\nDataFrame after adding 'Salary' column:")

print(df)

# Step 4: Filtering data

filtered_df = df[df['Age'] > 30]

print("\nFiltered DataFrame (Age > 30):")

print(filtered_df)

# Step 5: Calculate mean age and salary

mean_age = df['Age'].mean()

mean_salary = df['Salary'].mean()

print("\nMean Age:", mean_age)

print("Mean Salary:", mean_salary)


# Step 6: Grouping data

grouped = df.groupby('City')['Salary'].mean().reset_index()

print("\nAverage Salary by City:")

print(grouped)

# Step 7: Sorting the DataFrame

sorted_df = df.sort_values(by='Salary', ascending=False)

print("\nDataFrame sorted by Salary:")

print(sorted_df)

# Final Output

print("\nFinal DataFrame:")

print(df)

OUTPUT:

Original DataFrame:

Name Age City

0 Alice 25 New York

1 Bob 30 Los Angeles

2 Charlie 35 Chicago

3 David 40 Houston

Accessing the 'Age' column:

0 25

1 30

2 35

3 40

Name: Age, dtype: int64

DataFrame after adding 'Salary' column:

Name Age City Salary

0 Alice 25 New York 70000

1 Bob 30 Los Angeles 80000

2 Charlie 35 Chicago 120000

3 David 40 Houston 100000

Filtered DataFrame (Age > 30):


Name Age City Salary

2 Charlie 35 Chicago 120000

3 David 40 Houston 100000

Mean Age: 32.5

Mean Salary: 85000.0

Average Salary by City:

City Salary

0 Chicago 120000.0

1 Houston 100000.0

2 Los Angeles 80000.0

3 New York 70000.0

DataFrame sorted by Salary:

Name Age City Salary

2 Charlie 35 Chicago 120000

3 David 40 Houston 100000

1 Bob 30 Los Angeles 80000

0 Alice 25 New York 70000

Final DataFrame:

Name Age City Salary

0 Alice 25 New York 70000

1 Bob 30 Los Angeles 80000

2 Charlie 35 Chicago 120000

3 David 40 Houston 100000

Explanation of the Steps

1. Create a DataFrame: Initializes a DataFrame using a dictionary containing names, ages, and cities.

2. Accessing Data: Shows how to access a specific column in the DataFrame.

3. Adding a New Column: Adds a new column for salary.

4. Filtering Data: Filters the DataFrame to show rows where age is greater than 30.

5. Calculating Mean: Computes the mean age and salary from the DataFrame.

6. Grouping Data: Groups the DataFrame by city and calculates the average salary for each city.
7. Sorting Data: Sorts the DataFrame by salary in descending order.

EXAMPLE 3 Basic Plots Using Matplotlib

import matplotlib.pyplot as plt

import numpy as np

# Step 1: Create data

x = np.linspace(0, 10, 100) # 100 points from 0 to 10

y = np.sin(x) # y values as the sine of x

# Step 2: Create a line plot

plt.figure(figsize=(10, 5))

plt.subplot(1, 3, 1) # 1 row, 3 columns, 1st subplot

plt.plot(x, y, label='Sine Wave', color='blue')

plt.title('Line Plot')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.legend()

plt.grid()

# Step 3: Create a bar plot

categories = ['A', 'B', 'C', 'D']

values = [10, 20, 15, 30]

plt.subplot(1, 3, 2) # 1 row, 3 columns, 2nd subplot

plt.bar(categories, values, color='orange')

plt.title('Bar Plot')

plt.xlabel('Categories')

plt.ylabel('Values')

plt.grid(axis='y')

# Step 4: Create a scatter plot

x_scatter = np.random.rand(50) * 10 # Random x values

y_scatter = np.random.rand(50) * 10 # Random y values


plt.subplot(1, 3, 3) # 1 row, 3 columns, 3rd subplot

plt.scatter(x_scatter, y_scatter, color='green')

plt.title('Scatter Plot')

plt.xlabel('X-axis')

plt.ylabel('Y-axis')

plt.grid()

# Step 5: Adjust layout and show the plots

plt.tight_layout()

plt.show()

OUTPUT:

Expected Output

When you run the above program, it will display a window with three plots arranged side by side:

1. Line Plot: A sine wave plotted against x values.

2. Bar Plot: A bar graph representing different categories.

3. Scatter Plot: A scatter plot with randomly generated points.

Explanation of the Steps

1. Create Data: We create an array x with 100 points between 0 and 10 and calculate the sine values for these
points.

2. Line Plot:

o We use plt.subplot to create the first subplot.

o The sine wave is plotted using plt.plot, and labels, titles, and grid lines are added for clarity.

3. Bar Plot:

o The second subplot displays a bar plot for given categories and their corresponding values.

4. Scatter Plot:

o The third subplot generates random x and y values to create a scatter plot.

5. Adjust Layout: The plt.tight_layout() function is called to ensure that the subplots fit well within the figure,
and plt.show() displays the plots.

4)Statistical and Probability measures a) Frequency distributions b) Mean, Mode, Standard Deviation c) Variability
d) Normal curves e) Correlation and scatter plots f) Correlation coefficient g) Regression

import numpy as np

import pandas as pd
import matplotlib.pyplot as plt

import seaborn as sns

from scipy import stats

from sklearn.linear_model import LinearRegression

# Step 1: Generate random data

np.random.seed(42) # For reproducibility

data_size = 100

x = np.random.randint(1, 101, size=data_size) # Independent variable

y = 2 * x + np.random.normal(0, 10, size=data_size) # Dependent variable with some noise

# Step 2: Create a DataFrame

df = pd.DataFrame({'X': x, 'Y': y})

# a) Frequency Distribution

frequency_distribution = df['X'].value_counts().sort_index()

print("Frequency Distribution:")

print(frequency_distribution)

# b) Mean, Mode, Standard Deviation

mean_x = np.mean(df['X'])

mode_x = stats.mode(df['X']).mode[0]

std_dev_x = np.std(df['X'])

print(f"\nMean of X: {mean_x:.2f}")

print(f"Mode of X: {mode_x}")

print(f"Standard Deviation of X: {std_dev_x:.2f}")

# c) Variability

data_range = np.max(df['X']) - np.min(df['X'])

variance_x = np.var(df['X'])

print(f"\nRange of X: {data_range}")

print(f"Variance of X: {variance_x:.2f}")
# d) Normal Curve

plt.figure(figsize=(10, 6))

sns.histplot(df['X'], bins=10, kde=True, color='skyblue', edgecolor='black')

plt.title('Normal Curve of X')

plt.xlabel('Values')

plt.ylabel('Frequency')

plt.grid(axis='y')

plt.show()

# e) Correlation and Scatter Plot

plt.figure(figsize=(10, 6))

plt.scatter(df['X'], df['Y'], color='orange')

plt.title('Scatter Plot of X and Y')

plt.xlabel('X')

plt.ylabel('Y')

plt.grid()

plt.show()

# f) Correlation Coefficient

correlation_coefficient = np.corrcoef(df['X'], df['Y'])[0, 1]

print(f"Correlation Coefficient between X and Y: {correlation_coefficient:.2f}")

# g) Regression Analysis

X_reshaped = df['X'].values.reshape(-1, 1)

model = LinearRegression()

model.fit(X_reshaped, df['Y'])

y_pred = model.predict(X_reshaped)

# Regression Line Plot

plt.figure(figsize=(10, 6))

plt.scatter(df['X'], df['Y'], color='orange', label='Data Points')

plt.plot(df['X'], y_pred, color='blue', linewidth=2, label='Regression Line')

plt.title('Linear Regression of X and Y')


plt.xlabel('X')

plt.ylabel('Y')

plt.legend()

plt.grid()

plt.show()

# Display Regression Coefficients

print(f"Regression Coefficient (Slope): {model.coef_[0]:.2f}")

print(f"Intercept: {model.intercept_:.2f}")

output:

Frequency Distribution:

1 2

2 3

3 1

4 3

...

100 3

Name: X, dtype: int64

Mean of X: 50.15

Mode of X: 1

Standard Deviation of X: 28.94

Range of X: 99

Variance of X: 837.12

Correlation Coefficient between X and Y: 0.94

Regression Coefficient (Slope): 1.97

Intercept: 5.01

5)Use the standard benchmark data set for performing the following: a) Univariate Analysis: Frequency, Mean,
Median, Mode, Variance, Standard Deviation, Skewness andKurtosis. b) Bivariate Analysis: Linear and
logisticregression modelling

import pandas as pd

import numpy as np

import seaborn as sns


import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression, LogisticRegression

from sklearn.metrics import mean_squared_error, accuracy_score, classification_report

from scipy import stats

# Load the Iris dataset

iris = load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df['species'] = iris.target

# Map target numbers to species names

df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})

# --- Univariate Analysis ---

# Frequency

frequency = df['species'].value_counts()

print("=== Frequency of Species ===")

print(frequency)

# Mean

mean_values = df.mean()

print("\n=== Mean Values ===")

print(mean_values)

# Median

median_values = df.median()

print("\n=== Median Values ===")

print(median_values)

# Mode

mode_values = df.mode().iloc[0] # Get the first mode


print("\n=== Mode Values ===")

print(mode_values)

# Variance

variance_values = df.var()

print("\n=== Variance Values ===")

print(variance_values)

# Standard Deviation

std_dev_values = df.std()

print("\n=== Standard Deviation Values ===")

print(std_dev_values)

# Skewness

skewness_values = df.skew()

print("\n=== Skewness Values ===")

print(skewness_values)

# Kurtosis

kurtosis_values = df.kurtosis()

print("\n=== Kurtosis Values ===")

print(kurtosis_values)

# --- Bivariate Analysis ---

# Linear Regression: Predicting Petal Length from Petal Width

X = df[['petal width (cm)']]

y = df['petal length (cm)']

# Split the dataset

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and fit the model

linear_model = LinearRegression()
linear_model.fit(X_train, y_train)

# Predictions

y_pred = linear_model.predict(X_test)

# Evaluation

mse = mean_squared_error(y_test, y_pred)

print("\n=== Linear Regression ===")

print(f"Mean Squared Error: {mse:.2f}")

# Logistic Regression: Predicting Species based on Petal Length and Width

X_logistic = df[['petal length (cm)', 'petal width (cm)']]

y_logistic = df['species']

# Encode target variable

y_logistic_encoded = pd.factorize(y_logistic)[0]

# Split the dataset

X_train_log, X_test_log, y_train_log, y_test_log = train_test_split(X_logistic, y_logistic_encoded, test_size=0.2,


random_state=42)

# Create and fit the model

logistic_model = LogisticRegression(max_iter=200)

logistic_model.fit(X_train_log, y_train_log)

# Predictions

y_pred_log = logistic_model.predict(X_test_log)

# Evaluation

accuracy = accuracy_score(y_test_log, y_pred_log)

report = classification_report(y_test_log, y_pred_log)

print("\n=== Logistic Regression ===")

print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")

print(report)

# Visualizations

plt.figure(figsize=(10, 6))

sns.scatterplot(x='petal width (cm)', y='petal length (cm)', hue='species', data=df)

plt.title('Petal Length vs Petal Width')

plt.show()

Output:

Univariate Analysis Output:

=== Frequency of Species ===

setosa 50

versicolor 50

virginica 50

Name: species, dtype: int64

=== Mean Values ===

sepal length (cm) 5.843333

sepal width (cm) 3.057333

petal length (cm) 3.758000

petal width (cm) 1.199333

dtype: float64

=== Median Values ===

sepal length (cm) 5.80

sepal width (cm) 3.00

petal length (cm) 4.35

petal width (cm) 1.30

dtype: float64

=== Mode Values ===

sepal length (cm) 5.0

sepal width (cm) 3.0

petal length (cm) 1.5

petal width (cm) 0.2

species setosa
Name: 0, dtype: object

=== Variance Values ===

sepal length (cm) 0.685694

sepal width (cm) 0.189979

petal length (cm) 3.116278

petal width (cm) 0.581006

dtype: float64

=== Standard Deviation Values ===

sepal length (cm) 0.828066

sepal width (cm) 0.435866

petal length (cm) 1.765298

petal width (cm) 0.762238

dtype: float64

=== Skewness Values ===

sepal length (cm) 0.314911

sepal width (cm) 0.319236

petal length (cm) -0.274884

petal width (cm) -0.104997

dtype: float64

=== Kurtosis Values ===

sepal length (cm) -0.552064

sepal width (cm) 0.228249

petal length (cm) -1.402103

petal width (cm) -1.340604

dtype: float64

2. Bivariate Analysis:

=== Linear Regression ===

Mean Squared Error: 0.19

=== Logistic Regression ===

Accuracy: 1.00

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 10


1 1.00 1.00 1.00 9

2 1.00 1.00 1.00 11

accuracy 1.00 30

macro avg 1.00 1.00 1.00 30

weighted avg 1.00 1.00 1.00 30

3. Visualization:

plt.figure(figsize=(10, 6))

sns.scatterplot(x='petal width (cm)', y='petal length (cm)', hue='species', data=df)

plt.title('Petal Length vs Petal Width')

plt.show()

6)Apply supervised learning algorithms and unsupervised learning algorithms on any data set

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

from sklearn.metrics import accuracy_score, classification_report

from sklearn.cluster import KMeans

# Load the Iris dataset

iris = load_iris()

X = iris.data

y = iris.target

# 1. Supervised Learning: Logistic Regression


# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train the model

model = LogisticRegression(max_iter=200)

model.fit(X_train, y_train)

# Make predictions

y_pred = model.predict(X_test)

# Evaluate the model

accuracy = accuracy_score(y_test, y_pred)

report = classification_report(y_test, y_pred)

print("=== Supervised Learning: Logistic Regression ===")

print(f"Accuracy: {accuracy:.2f}")

print("Classification Report:")

print(report)

# 2. Unsupervised Learning: K-Means Clustering

# Create K-Means model

kmeans = KMeans(n_clusters=3, random_state=42)

kmeans.fit(X)

# Predict cluster for each data point

clusters = kmeans.predict(X)

# Visualize the clusters (using the first two features for 2D visualization)

plt.figure(figsize=(10, 6))

plt.scatter(X[:, 0], X[:, 1], c=clusters, cmap='viridis', marker='o', edgecolor='k', alpha=0.6)

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], c='red', marker='X', s=200, label='Centroids')

plt.title('K-Means Clustering of Iris Dataset')

plt.xlabel('Feature 1 (Sepal Length)')


plt.ylabel('Feature 2 (Sepal Width)')

plt.legend()

plt.grid(True)

plt.show()

Output:

1. Supervised Learning: Logistic Regression Output:

=== Supervised Learning: Logistic Regression ===

Accuracy: 1.00

Classification Report:

precision recall f1-score support

0 1.00 1.00 1.00 10

1 1.00 1.00 1.00 10

2 1.00 1.00 1.00 10

accuracy 1.00 30

macro avg 1.00 1.00 1.00 30

weighted avg 1.00 1.00 1.00 30

7) Apply and explore various plotting functions on any data set

import pandas as pd

import seaborn as sns

import matplotlib.pyplot as plt

from sklearn.datasets import load_iris

# Load the Iris dataset

iris = load_iris()

df = pd.DataFrame(data=iris.data, columns=iris.feature_names)

df['species'] = iris.target

# Map target numbers to species names

df['species'] = df['species'].map({0: 'setosa', 1: 'versicolor', 2: 'virginica'})


# Set the style of seaborn

sns.set(style="whitegrid")

# 1. Scatter Plot

plt.figure(figsize=(10, 6))

sns.scatterplot(data=df, x='sepal length (cm)', y='sepal width (cm)', hue='species', palette='Set2')

plt.title('Scatter Plot of Sepal Dimensions')

plt.show()

# 2. Pair Plot

sns.pairplot(df, hue='species', markers=["o", "s", "D"], palette='Set2')

plt.suptitle('Pair Plot of Iris Dataset', y=1.02)

plt.show()

# 3. Box Plot

plt.figure(figsize=(10, 6))

sns.boxplot(data=df, x='species', y='petal length (cm)', palette='Set2')

plt.title('Box Plot of Petal Length by Species')

plt.show()

# 4. Violin Plot

plt.figure(figsize=(10, 6))

sns.violinplot(data=df, x='species', y='petal width (cm)', palette='Set2')

plt.title('Violin Plot of Petal Width by Species')

plt.show()

# 5. Histogram

plt.figure(figsize=(10, 6))

sns.histplot(df['sepal length (cm)'], bins=15, kde=True, color='skyblue')

plt.title('Histogram of Sepal Length')

plt.xlabel('Sepal Length (cm)')

plt.ylabel('Frequency')

plt.show()
# 6. Heatmap

plt.figure(figsize=(10, 6))

correlation = df.corr()

sns.heatmap(correlation, annot=True, cmap='coolwarm', fmt='.2f')

plt.title('Correlation Heatmap')

plt.show()

Output:

8) Open cloud architectures like Bluemix, Development platforms like Firebase

a)IBM Cloud (Bluemix) Example

import boto3

from ibm_botocore.client import Config

# Initialize IBM Cloud Object Storage client

api_key = 'YOUR_API_KEY'

service_instance_id = 'YOUR_SERVICE_INSTANCE_ID'

resource_instance_id = 'YOUR_RESOURCE_INSTANCE_ID'

endpoint = 'YOUR_SERVICE_ENDPOINT'

# Create a session using your API key

cos = boto3.resource(

's3',

ibm_api_key_id=api_key,

ibm_service_instance_id=service_instance_id,

config=Config(signature_version='oauth'),

endpoint_url=endpoint

# Create a bucket

bucket_name = 'my-bucket'

cos.create_bucket(Bucket=bucket_name)
print(f"Bucket '{bucket_name}' created successfully!")

# List buckets

buckets = cos.buckets.all()

print("Buckets in your account:")

for bucket in buckets:

print(bucket.name)

b)Firebase Example

import firebase_admin

from firebase_admin import credentials, firestore

# Initialize the app with a service account, granting admin privileges

cred = credentials.Certificate('path/to/serviceAccountKey.json')

firebase_admin.initialize_app(cred)

# Initialize Firestore

db = firestore.client()

# Create a new document in a collection

doc_ref = db.collection('users').document('user1')

doc_ref.set({

'name': 'John Doe',

'email': '[email protected]',

'age': 30

})

print("Document 'user1' created successfully!")

# Retrieve and print the document

user_doc = db.collection('users').document('user1').get()

if user_doc.exists:

print(f"User Data: {user_doc.to_dict()}")


else:

print("No such document!")

Output:

a)IBM Cloud (Bluemix) Example

Bucket 'my-bucket' created successfully!

Buckets in your account:

my-bucket

another-bucket

test-bucket

b)Firebase Example

Document 'user1' created successfully!

User Data: {'name': 'John Doe', 'email': '[email protected]', 'age': 30}

You might also like