0% found this document useful (0 votes)
14 views22 pages

Practical Python for Data Science

The document contains practical Python codes for data science, covering topics such as NumPy array creation, bivariate analysis using logistic regression, basic arithmetic operations, and descriptive analytics with Pandas. It includes examples of data visualization techniques like density plots, contour plots, and 3D plotting, along with statistical analyses on datasets like diabetes and iris. Additionally, it demonstrates file operations, data manipulation, and the creation of DataFrames from various data structures.

Uploaded by

sshivasreetha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

Practical Python for Data Science

The document contains practical Python codes for data science, covering topics such as NumPy array creation, bivariate analysis using logistic regression, basic arithmetic operations, and descriptive analytics with Pandas. It includes examples of data visualization techniques like density plots, contour plots, and 3D plotting, along with statistical analyses on datasets like diabetes and iris. Additionally, it demonstrates file operations, data manipulation, and the creation of DataFrames from various data structures.

Uploaded by

sshivasreetha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python for Data Science Practical Codes

Question 1.a:

# 1.a Creation of different types of NumPy arrays


import numpy as np

# 1D array
array_1d = [Link]([1, 2, 3, 4, 5])
print("1D Array:", array_1d)

# 2D array
array_2d = [Link]([[1, 2, 3], [4, 5, 6]])
print("2D Array:\n", array_2d)

# 3D array
array_3d = [Link]([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("3D Array:\n", array_3d)

# Using built-in functions


zeros = [Link]((2, 3))
ones = [Link]((3, 3))
arange_array = [Link](10)
linspace_array = [Link](0, 1, 5)

print("Zeros:\n", zeros)
print("Ones:\n", ones)
print("Arange:\n", arange_array)
print("Linspace:\n", linspace_array)
Python for Data Science Practical Codes

Question 1.b:

# 1.b Bivariate Analysis on Diabetes Data using Logistic Regression


import pandas as pd
import seaborn as sns
import [Link] as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from [Link] import classification_report, accuracy_score

# Replace with correct path or link


url = '[Link]'
data = pd.read_csv(url)

# Bivariate Analysis
[Link](data, hue='Outcome')
[Link]()

# Logistic Regression
X = [Link]('Outcome', axis=1)
y = data['Outcome']

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


random_state=42)
model = LogisticRegression(max_iter=1000)
[Link](X_train, y_train)
y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))


print(classification_report(y_test, y_pred))
Python for Data Science Practical Codes

Question 2.a:

# 2.a Basic Arithmetic Operations with NumPy Arrays


import numpy as np

a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Power:", a ** 2)
Python for Data Science Practical Codes

Question 2.b:

# 2.b Density and Contour Plots on Adult Dataset


import pandas as pd
import seaborn as sns
import [Link] as plt

# Replace with correct path or link


url = '[Link]'
data = pd.read_csv(url)

# Drop rows with missing values for plotting


data = [Link]()

# Convert categorical to numeric if needed


data['income'] = data['income'].astype('category').[Link]

# Density plot
[Link](data=data, x='age', hue='income', fill=True)
[Link]('Density Plot of Age by Income')
[Link]()

# Contour plot (using a sample)


[Link](data=data, x='age', y='hours-per-week', fill=True)
[Link]('Contour Plot of Age vs Hours-per-week')
[Link]()
Python for Data Science Practical Codes

Question 3.a:

# 3.a Creation of an Array using Built-In NumPy Functions


import numpy as np

zeros_array = [Link]((2, 2))


ones_array = [Link]((3, 3))
identity_matrix = [Link](4)
random_array = [Link](2, 3)

print("Zeros Array:\n", zeros_array)


print("Ones Array:\n", ones_array)
print("Identity Matrix:\n", identity_matrix)
print("Random Array:\n", random_array)
Python for Data Science Practical Codes

Question 3.b:

# 3.b Descriptive Analytics with Pandas on Iris Dataset


import pandas as pd

# Replace with correct path or link


url = '[Link]
data = pd.read_csv(url)

print("First 5 Rows:\n", [Link]())


print("\nSummary Statistics:\n", [Link]())
print("\nSpecies Count:\n", data['species'].value_counts())
Python for Data Science Practical Codes

Question 4.a:

# 4.a Creation of a DataFrame from Dictionary


import pandas as pd

data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}

df = [Link](data)
print(df)
Python for Data Science Practical Codes

Question 4.b:

# 4.b Descriptive Analytics on Iris Dataset from scikit-learn


from [Link] import load_iris
import pandas as pd

iris = load_iris()
df = [Link](data=[Link], columns=iris.feature_names)
df['target'] = [Link]

print("First 5 Rows:\n", [Link]())


print("\nSummary Statistics:\n", [Link]())
print("\nTarget Count:\n", df['target'].value_counts())
Python for Data Science Practical Codes

Question 5.a:

# 5.a Creation of a DataFrame from N-Dimensional Arrays


import numpy as np
import pandas as pd

array = [Link]([[1, 2, 3], [4, 5, 6]])


df = [Link](array, columns=['Column1', 'Column2', 'Column3'])
print(df)
Python for Data Science Practical Codes

Question 5.b:

# 5.b Univariate Statistical Analysis on Diabetes Data


import pandas as pd

# Replace with correct path or link


url = '[Link]'
data = pd.read_csv(url)

print([Link]())
print("\nOutcome Counts:\n", data['Outcome'].value_counts())
Python for Data Science Practical Codes

Question 6.a:

# 6.a Bivariate Analysis on Diabetes Data using Linear Regression


import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression

# Replace with correct path or link


url = '[Link]'
data = pd.read_csv(url)

# Example: BMI vs Glucose


[Link](x='BMI', y='Glucose', data=data)
[Link]('BMI vs Glucose')
[Link]()

X = data[['BMI']]
y = data['Glucose']

model = LinearRegression()
[Link](X, y)

print("Coefficient:", model.coef_)
print("Intercept:", model.intercept_)
Python for Data Science Practical Codes

Question 6.b:

# 6.b Creation of different types of NumPy arrays and displaying basic information
import numpy as np

a = [Link]([[1, 2, 3], [4, 5, 6]])


print("Array:\n", a)
print("Shape:", [Link])
print("Data Type:", [Link])
print("Size:", [Link])
print("Dimension:", [Link])
Python for Data Science Practical Codes

Question 7.a:

# 7.a NumPy File Operations


import numpy as np

array = [Link]([1, 2, 3, 4, 5])


[Link]('my_array.npy', array)

# Load the array


loaded_array = [Link]('my_array.npy')
print("Loaded Array:", loaded_array)
Python for Data Science Practical Codes

Question 7.b:

# 7.b Descriptive Analytics with Pandas on Iris Dataset (from path or web)
import pandas as pd

# Replace with correct path or link


url = '[Link]
df = pd.read_csv(url)

print([Link]())
print(df['species'].value_counts())
Python for Data Science Practical Codes

Question 8.a:

# 8.a 3D Plotting on Adult Dataset


import pandas as pd
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D

# Replace with correct path or link


url = '[Link]'
df = pd.read_csv(url)

fig = [Link]()
ax = fig.add_subplot(111, projection='3d')
[Link](df['age'], df['hours-per-week'], df['education-num'], c='red')
ax.set_xlabel('Age')
ax.set_ylabel('Hours per Week')
ax.set_zlabel('Education Num')
[Link]()
Python for Data Science Practical Codes

Question 8.b:

# 8.b Creation of a DataFrame from Series


import pandas as pd

s1 = [Link]([1, 2, 3], name="A")


s2 = [Link]([4, 5, 6], name="B")

df = [Link]([s1, s2], axis=1)


print(df)
Python for Data Science Practical Codes

Question 9.a:

# 9.a Histograms on Adult Dataset


import pandas as pd
import [Link] as plt

# Replace with correct path or link


url = '[Link]'
df = pd.read_csv(url)

df['age'].hist(bins=20)
[Link]('Histogram of Age')
[Link]('Age')
[Link]('Frequency')
[Link]()
Python for Data Science Practical Codes

Question 9.b:

# 9.b NumPy Built-in Array Creation and Operations


import numpy as np

array = [Link](1, 6)
print("Array:", array)
print("Squared:", array ** 2)
print("Mean:", [Link](array))
print("Standard Deviation:", [Link](array))
Python for Data Science Practical Codes

Question 10.a:

# 10.a Univariate Statistical Analysis on Diabetes Data


import pandas as pd

# Replace with correct path or link


url = '[Link]'
df = pd.read_csv(url)

print("Summary Statistics:\n", [Link]())


print("Outcome Distribution:\n", df['Outcome'].value_counts())
Python for Data Science Practical Codes

Question 10.b:

# 10.b Array Creation using Built-in NumPy Functions


import numpy as np

a = [Link](1, 10, 5)
b = [Link]((2, 2), 7)
print("Linspace Array:", a)
print("Full Array:\n", b)
Python for Data Science Practical Codes

Question 11.a:

# 11.a Normal Curves and Scatter Plots on UCI Dataset


import pandas as pd
import [Link] as plt
import seaborn as sns

# Replace with correct path or link


url = '[Link]'
df = pd.read_csv(url)

# Normal curve on 'age'


[Link](df['age'], fill=True)
[Link]("Normal Curve of Age")
[Link]()

# Scatter plot
[Link](data=df, x='age', y='hours-per-week')
[Link]("Scatter Plot: Age vs Hours-per-week")
[Link]()
Python for Data Science Practical Codes

Question 11.b:

# 11.b NumPy Array Types and Info


import numpy as np

arr = [Link]([[10, 20, 30], [40, 50, 60]])


print("Array:\n", arr)
print("Shape:", [Link])
print("Size:", [Link])
print("Datatype:", [Link])
print("Dimension:", [Link])

Common questions

Powered by AI

Normal curve plots visualize data distribution, highlighting deviations from a normal distribution and indicating skewness or kurtosis in the dataset . In contrast, scatter plots reveal potential linear or non-linear relationships and interactions between two continuous variables, providing insights into correlations that normal curves cannot elucidate . Both plots complement each other by offering a comprehensive view of data characteristics and relationships.

1D arrays are linear structures with a single dimension, capable of holding elements in a single row vector . 2D arrays contain elements arranged in rows and columns, forming matrix-like data structures . In contrast, 3D arrays extend this concept by incorporating a third depth layer, allowing the array to store matrices in each layer of depth, creating a cube-like structure .

Univariate statistical analysis typically presents summary statistics such as mean, standard deviation, minimum and maximum values, quartiles, and frequency counts of categorical variables . These measures are crucial for understanding the central tendency, variability, and distribution of individual variables, providing a foundational understanding of the dataset before further analysis .

Density and contour plots in bivariate analysis assist in visualizing the distribution and concentration of data points, highlighting how variables interrelate and cluster within the dataset. They offer insights into potential correlations, distribution spread, and the presence of patterns or trends that may not be immediately apparent through simple statistical measures alone .

Logistic regression is utilized for bivariate analysis on the Diabetes dataset to predict categorical outcomes (e.g., presence or absence of diabetes) based on continuous and categorical independent variables, providing output in terms of accuracy and classification reports . Linear regression, however, analyzes continuous outcomes, like the relationship between BMI and glucose levels, yielding coefficients and intercepts that describe the line of best fit .

Using pandas to create a DataFrame from a dictionary integrates robust data handling capabilities, such as easy manipulation, indexing, and analysis of tabular data . Pandas provide streamlined methods for handling missing data, performing aggregations, and allowing complex queries, making it an essential tool for data preprocessing and analysis .

Key considerations in data splitting include ensuring a representative sample in both training and test sets, maintaining the same distribution of classes or categories, and selecting an appropriate split ratio to balance model training with sufficient evaluation. Randomization helps avoid biases, but replicability requires setting seed values for consistent splits across different runs .

Evaluating a logistic regression model involves assessing its accuracy, precision, recall, F1-score, and area under the ROC curve (AUC) metrics. Accuracy provides an overall correct classification rate, while precision measures the correctness of positive predictions. Recall reflects the model's ability to identify all positive instances, and the F1-score balances precision and recall. AUC indicates the model's ability to distinguish between classes, with values closer to one signifying better performance .

Dropping missing values simplifies datasets by removing incomplete cases, ensuring that statistical calculations and visualizations reflect only valid observations. This preprocessing step prevents biases and distortions in resulting models or plots, ensuring accuracy in analysis outcomes. However, it can also reduce dataset size, potentially leading to loss of valuable information or representativeness .

Built-in NumPy functions such as zeros and ones simplify array creation by quickly generating arrays filled entirely with zeros or ones, which can be used for initializing weights in neural networks, creating masks, or setting a base state for iterative computations. These functions enhance efficiency by reducing complexity and ensuring arrays are created with consistent, predictable values .

You might also like