Untitled8 (2)
Untitled8 (2)
# a. Basic ndarray
a = np.array([1, 2, 3, 4, 5])
print("Basic ndarray:", a)
Basic ndarray: [1 2 3 4 5]
Array of zeros:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Array of ones:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Random numbers:
[[0.95286609 0.88055076 0.84394613]
[0.05645231 0.58839302 0.30319334]
[0.60025703 0.63418513 0.869445 ]]
Custom array:
[[10 20]
[30 40]]
Identity Matrix:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Dimensions of array: 1
Size of array: 9
Reshaped array:
[[1]
[2]
[3]
[4]
[5]]
Transposed array:
[[0.95286609 0.05645231 0.60025703]
[0.88055076 0.58839302 0.63418513]
[0.84394613 0.30319334 0.869445 ]]
Expanded array:
[[1 2 3 4 5]]
Shape: (1, 5)
Squeezed array:
[1 2 3 4 5]
Shape: (5,)
Sorted array: [1 1 2 3 4 5 6 9]
[[ 9 10]
[12 13]]]
Broadcasted Addition:
[[11 22]
[13 24]]
DataFrame:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
Concatenated DataFrame:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
3 David 40
4 Eve 45
df = pd.DataFrame(data)
print(df_filled)
df = pd.DataFrame(data)
Department
Finance 63500.0
HR 52500.0
IT 65000.0
Name: Salary, dtype: float64
# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
# Sample data
data = {'Name': 'Alice', 'Age': 25, 'City': 'New York'}
print(loaded_data)
# Reading files
for file in txt_files:
with open(file, 'r') as f:
print(f.read())
['data.txt', 'test.txt']
Name Age City
Alice 25 New York
Bob 30 Los Angeles
Charlie 35 Chicago
import java.utile;
class test{
System.out.println("hello world")}
Requirement already satisfied: beautifulsoup4 in c:\users\khadersha\anaconda3\lib\site-packages (4.12.3)Note: you may need to restart the kernel to use updated packages.
Example Domain
print(df[['LoanAmount', 'ApplicantIncome']].head())
LoanAmount ApplicantIncome
0 NaN 0.070489
1 0.172214 0.054830
2 0.082489 0.035250
3 0.160637 0.030093
4 0.191027 0.072356
# Apply standardization
df[['LoanAmount', 'ApplicantIncome']] = scaler.fit_transform(df[['LoanAmount', 'ApplicantIncome']])
print(df[['LoanAmount', 'ApplicantIncome']].head())
LoanAmount ApplicantIncome
0 NaN 0.072991
1 -0.215309 -0.134412
2 -0.940328 -0.393747
3 -0.308860 -0.462062
4 -0.063289 0.097728
print(df[['Loan_Status']].head())
Loan_Status
0 1
1 0
2 1
3 1
4 1
df = pd.DataFrame(data)
plt.tight_layout()
plt.show()
# Download popular NLTK datasets and corpora (like stopwords, punkt tokenizer, etc.)
nltk.download('punkt') # Tokenizer models
nltk.download('stopwords') # Common stopwords
nltk.download('wordnet') # WordNet corpus for lemmatization
print(tokens)
In [97]: nltk.download('movie_reviews')
def preprocess_text(text):
# Tokenize the text
tokens = word_tokenize(text)
# Remove stopwords
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
# Apply preprocessing
X_train_processed = [preprocess_text(text) for text in X_train]
X_test_processed = [preprocess_text(text) for text in X_test]
Out[101… ▾ MultinomialNB i ?
MultinomialNB()
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
Accuracy: 81.00%
def preprocess_text(text):
tokens = word_tokenize(text)
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
return ' '.join(filtered_tokens)
# Apply preprocessing
X_train_processed = [preprocess_text(text) for text in X_train]
X_test_processed = [preprocess_text(text) for text in X_test]
# Make predictions
y_pred = model.predict(X_test_vec)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")
In [ ]: import nltk
import spacy
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from spacy import displacy
# Visualize the named entities (Uncomment to run in a Jupyter Notebook or similar environment)
# displacy.render(doc, style="ent")
Collecting pydantic==1.8.2
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
openai 1.64.0 requires pydantic<3,>=1.9.0, but you have pydantic 1.8.2 which is incompatible.
Downloading pydantic-1.8.2-py3-none-any.whl.metadata (103 kB)
---------------------------------------- 0.0/103.1 kB ? eta -:--:--
--- ------------------------------------ 10.2/103.1 kB ? eta -:--:--
--- ------------------------------------ 10.2/103.1 kB ? eta -:--:--
--- ------------------------------------ 10.2/103.1 kB ? eta -:--:--
----------- ------------------------- 30.7/103.1 kB 131.3 kB/s eta 0:00:01
----------- ------------------------- 30.7/103.1 kB 131.3 kB/s eta 0:00:01
-------------- ---------------------- 41.0/103.1 kB 140.9 kB/s eta 0:00:01
----------------------------- ------- 81.9/103.1 kB 241.3 kB/s eta 0:00:01
----------------------------------- 102.4/103.1 kB 295.4 kB/s eta 0:00:01
------------------------------------ 103.1/103.1 kB 270.8 kB/s eta 0:00:00
Requirement already satisfied: typing-extensions>=3.7.4.3 in c:\users\khadersha\anaconda3\lib\site-packages (from pydantic==1.8.2) (4.12.2)
Downloading pydantic-1.8.2-py3-none-any.whl (126 kB)
---------------------------------------- 0.0/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
--------- ------------------------------ 30.7/126.0 kB ? eta -:--:--
------------------- ------------------- 61.4/126.0 kB 172.4 kB/s eta 0:00:01
------------------- ------------------- 61.4/126.0 kB 172.4 kB/s eta 0:00:01
------------------- ------------------- 61.4/126.0 kB 172.4 kB/s eta 0:00:01
------------------- ------------------- 61.4/126.0 kB 172.4 kB/s eta 0:00:01
------------------------------------- 122.9/126.0 kB 248.7 kB/s eta 0:00:01
-------------------------------------- 126.0/126.0 kB 231.6 kB/s eta 0:00:00
Installing collected packages: pydantic
Attempting uninstall: pydantic
Found existing installation: pydantic 2.10.6
Uninstalling pydantic-2.10.6:
Successfully uninstalled pydantic-2.10.6
Successfully installed pydantic-1.8.2
In [ ]: