Ai Programs
Ai Programs
import numpy as np
squared = data ** 2
mean_value = np.mean(data)
std_dev = np.std(data)
print("Mean:", mean_value)
print(reshaped_data)
# Final Output
print("Reshaped Array:")
print(reshaped_data)
OUTPUT:
Mean: 30.0
[[10]
[20]
[30]
[40]
[50]]
Second element: 20
Reshaped Array:
[[10]
[20]
[30]
[40]
[50]]
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.
7. Indexing and slicing: Demonstrates how to access specific elements and slices of the array.
EXAMPLE 2
import pandas as pd
data = {
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
print(df['Age'])
print(df)
print(filtered_df)
mean_age = df['Age'].mean()
mean_salary = df['Salary'].mean()
grouped = df.groupby('City')['Salary'].mean().reset_index()
print(grouped)
print(sorted_df)
# Final Output
print("\nFinal DataFrame:")
print(df)
OUTPUT:
Original DataFrame:
2 Charlie 35 Chicago
3 David 40 Houston
0 25
1 30
2 35
3 40
City Salary
0 Chicago 120000.0
1 Houston 100000.0
Final DataFrame:
1. Create a DataFrame: Initializes a DataFrame using a dictionary containing names, ages, and cities.
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.
import numpy as np
plt.figure(figsize=(10, 5))
plt.title('Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.grid()
plt.title('Bar Plot')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.grid(axis='y')
plt.title('Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid()
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. 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 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
data_size = 100
# a) Frequency Distribution
frequency_distribution = df['X'].value_counts().sort_index()
print("Frequency Distribution:")
print(frequency_distribution)
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}")
# c) Variability
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))
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.grid(axis='y')
plt.show()
plt.figure(figsize=(10, 6))
plt.xlabel('X')
plt.ylabel('Y')
plt.grid()
plt.show()
# f) Correlation Coefficient
# 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)
plt.figure(figsize=(10, 6))
plt.ylabel('Y')
plt.legend()
plt.grid()
plt.show()
print(f"Intercept: {model.intercept_:.2f}")
output:
Frequency Distribution:
1 2
2 3
3 1
4 3
...
100 3
Mean of X: 50.15
Mode of X: 1
Range of X: 99
Variance of X: 837.12
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
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
# Frequency
frequency = df['species'].value_counts()
print(frequency)
# Mean
mean_values = df.mean()
print(mean_values)
# Median
median_values = df.median()
print(median_values)
# Mode
print(mode_values)
# Variance
variance_values = df.var()
print(variance_values)
# Standard Deviation
std_dev_values = df.std()
print(std_dev_values)
# Skewness
skewness_values = df.skew()
print(skewness_values)
# Kurtosis
kurtosis_values = df.kurtosis()
print(kurtosis_values)
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)
# Predictions
y_pred = linear_model.predict(X_test)
# Evaluation
y_logistic = df['species']
y_logistic_encoded = pd.factorize(y_logistic)[0]
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
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")
print(report)
# Visualizations
plt.figure(figsize=(10, 6))
plt.show()
Output:
setosa 50
versicolor 50
virginica 50
dtype: float64
dtype: float64
species setosa
Name: 0, dtype: object
dtype: float64
dtype: float64
dtype: float64
dtype: float64
2. Bivariate Analysis:
Accuracy: 1.00
Classification Report:
accuracy 1.00 30
3. Visualization:
plt.figure(figsize=(10, 6))
plt.show()
6)Apply supervised learning algorithms and unsupervised learning algorithms on any data set
import pandas as pd
import numpy as np
iris = load_iris()
X = iris.data
y = iris.target
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
# Make predictions
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")
print(report)
kmeans.fit(X)
clusters = kmeans.predict(X)
# Visualize the clusters (using the first two features for 2D visualization)
plt.figure(figsize=(10, 6))
plt.legend()
plt.grid(True)
plt.show()
Output:
Accuracy: 1.00
Classification Report:
accuracy 1.00 30
import pandas as pd
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
sns.set(style="whitegrid")
# 1. Scatter Plot
plt.figure(figsize=(10, 6))
plt.show()
# 2. Pair Plot
plt.show()
# 3. Box Plot
plt.figure(figsize=(10, 6))
plt.show()
# 4. Violin Plot
plt.figure(figsize=(10, 6))
plt.show()
# 5. Histogram
plt.figure(figsize=(10, 6))
plt.ylabel('Frequency')
plt.show()
# 6. Heatmap
plt.figure(figsize=(10, 6))
correlation = df.corr()
plt.title('Correlation Heatmap')
plt.show()
Output:
import boto3
api_key = 'YOUR_API_KEY'
service_instance_id = 'YOUR_SERVICE_INSTANCE_ID'
resource_instance_id = 'YOUR_RESOURCE_INSTANCE_ID'
endpoint = 'YOUR_SERVICE_ENDPOINT'
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(bucket.name)
b)Firebase Example
import firebase_admin
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred)
# Initialize Firestore
db = firestore.client()
doc_ref = db.collection('users').document('user1')
doc_ref.set({
'email': '[email protected]',
'age': 30
})
user_doc = db.collection('users').document('user1').get()
if user_doc.exists:
Output:
my-bucket
another-bucket
test-bucket
b)Firebase Example