Week2-7Preprocessing

Convert the raw text to the format that is easier to process.

Text preprocessing

这里写图片描述

Type and tokens

Type is any sequence of characters that represent a specific word, token is any occurrence of type. So the type can only appear once in a document, whereas token can appear multiple times.

To be or not to be

  • 4 types, 6 tokens

Tokenization

这里写图片描述

Word segmentation

  • Arabic
  • Japanese
  • German
  • Chinese

Sentence boundary recognition

  • Decision tree
  • Features
    • punctuation
    • formatting
    • fonts
    • spacing
    • capitalization
    • case
    • use of abbreviations(Dr.)
好的,我可以帮你编写这段代码。假设我们有一个名为"weather.csv"的数据集,其中包含了日期、最高气温、最低气温、风速等信息。 ``` python import pandas as pd from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeRegressor from sklearn.ensemble import RandomForestRegressor from sklearn.preprocessing import OneHotEncoder # 读取数据 data = pd.read_csv("weather.csv") # 数据预处理:将week列进行One-Hot编码 encoder = OneHotEncoder(sparse=False) week_encoded = encoder.fit_transform(data[['week']]) week_encoded = pd.DataFrame(week_encoded, columns=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']) data = pd.concat([data, week_encoded], axis=1) # 特征选择 features = ['year', 'month', 'day', 'temp_2', 'temp_1', 'average', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] X = data[features] y = data['actual'] # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 决策树模型 dt_model = DecisionTreeRegressor(random_state=42) dt_model.fit(X_train, y_train) dt_score = dt_model.score(X_test, y_test) # 随机森林模型 rf_model = RandomForestRegressor(n_estimators=100, random_state=42) rf_model.fit(X_train, y_train) rf_score = rf_model.score(X_test, y_test) # 预测某一天的最高气温 prediction = rf_model.predict([[2022, 9, 14, 23, 25, 28, 0, 0, 0, 0, 1, 0, 0]]) print("预测的最高气温为:", prediction) ``` 这段代码中,我们首先导入了需要的库和数据集,然后使用One-Hot编码将week列进行了处理,将其转换为了七个二进制列。接着选择了多个特征作为输入X,真实的最高气温作为输出y。使用train_test_split函数将数据集划分为训练集和测试集,然后分别使用决策树和随机森林进行训练和测试,并计算了模型的得分。最后,使用随机森林模型预测了某一天的最高气温。 需要注意的是,这里预测时输入的特征必须与训练时使用的特征保持一致,否则会导致预测结果不准确。实际应用中还需要进行更多的数据预处理、特征工程和模型调参等步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值